Android 自定义视图和 BaseExpandableListAdapter

发布于 2024-10-18 00:02:22 字数 2723 浏览 3 评论 0原文

我想使用 BaseExpandableListAdapter 将自定义视图添加到组视图,

这是代码:

自定义视图和layout.xml

public class InfoView extends View{
    private EditText nameEditText;
    private EditText descriptionEditText;
    private Spinner goalTypeSpinner;
    private Spinner categorySpinner;



    Button addGoalCategoryButton;
    public InfoView(Context context) {
        super(context);
        inflate(context, R.layout.info_panel, parent);
        nameEditText = (EditText) findViewById(R.id.nameEditText);

        descriptionEditText = (EditText) findViewById(R.id.descriptionEditText);

        goalTypeSpinner = (Spinner) 
                findViewById(R.id.goalTypeSpinner);

        categorySpinner = (Spinner) 
                findViewById(R.id.categorySpinner);
    }
}

infopanel.xml

<LinearLayout 
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" >
    <Spinner android:id="@+id/goalTypeSpinner" android:prompt="@string/goalType"
            android:entries="@array/goalTypeList" style="@style/commonText.commonSpinner" >
    </Spinner>
    <TableRow style="@style/commonText.commonTableRow">

            <Spinner android:id="@+id/categorySpinner" style="@style/commonText.commonSpinner.right"
                android:prompt="@string/goalType">
            </Spinner>
            <Button android:id="@+id/newCategoryButton" style="@style/commonText.commonButton"
                android:text="add category" />
    </TableRow> 
    <EditText android:id="@+id/nameEditText" android:hint="@string/nameEditTextHint"
        style="@style/commonText.commonEditText" >
    </EditText>
    <EditText android:id="@+id/descriptionEditText" android:hint="@string/descriptionEditTextHint"
        style="@style/commonText.commonTextArea">
    </EditText>

</LinearLayout>

在 BaseExpandableListAdapter 中:

..............
InfoView infoView;
public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        if (group.equals(INFO) && child.equals(INFO)) {
            Log.i(TAG, "group INFO(" + group + ")");

            if (infoView == null) {

                // i don't want to recreate everytime 
                // the view so i check if is null         

                infoView = new InfoView(context);
            }

            return infoView;
        }
........

代码有效(即不会抛出任何异常)异常)但视图未显示。

你知道为什么吗?

谢谢和问候

I want to add a custom view to a group view with a BaseExpandableListAdapter

This is The code:

The custom view and layout.xml

public class InfoView extends View{
    private EditText nameEditText;
    private EditText descriptionEditText;
    private Spinner goalTypeSpinner;
    private Spinner categorySpinner;



    Button addGoalCategoryButton;
    public InfoView(Context context) {
        super(context);
        inflate(context, R.layout.info_panel, parent);
        nameEditText = (EditText) findViewById(R.id.nameEditText);

        descriptionEditText = (EditText) findViewById(R.id.descriptionEditText);

        goalTypeSpinner = (Spinner) 
                findViewById(R.id.goalTypeSpinner);

        categorySpinner = (Spinner) 
                findViewById(R.id.categorySpinner);
    }
}

infopanel.xml

<LinearLayout 
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" >
    <Spinner android:id="@+id/goalTypeSpinner" android:prompt="@string/goalType"
            android:entries="@array/goalTypeList" style="@style/commonText.commonSpinner" >
    </Spinner>
    <TableRow style="@style/commonText.commonTableRow">

            <Spinner android:id="@+id/categorySpinner" style="@style/commonText.commonSpinner.right"
                android:prompt="@string/goalType">
            </Spinner>
            <Button android:id="@+id/newCategoryButton" style="@style/commonText.commonButton"
                android:text="add category" />
    </TableRow> 
    <EditText android:id="@+id/nameEditText" android:hint="@string/nameEditTextHint"
        style="@style/commonText.commonEditText" >
    </EditText>
    <EditText android:id="@+id/descriptionEditText" android:hint="@string/descriptionEditTextHint"
        style="@style/commonText.commonTextArea">
    </EditText>

</LinearLayout>

In the BaseExpandableListAdapter :

..............
InfoView infoView;
public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        if (group.equals(INFO) && child.equals(INFO)) {
            Log.i(TAG, "group INFO(" + group + ")");

            if (infoView == null) {

                // i don't want to recreate everytime 
                // the view so i check if is null         

                infoView = new InfoView(context);
            }

            return infoView;
        }
........

The code works (ie doesn't throw any exception) but the view isn't displayed.

Do you know Why ?

thanks and regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风吹 2024-10-25 00:02:22

实现getChildView的正确方法是使用提供的convertView。不要保留在 getChildView 中创建的任何视图的引用。

public View getChildView(int groupPosition, int childPosition,
                        boolean isLastChild, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = new InfoView(context);
    }
    return convertView;
}

The proper way to implement getChildView is to use the provided convertView. Don't keep reference of any Views created in getChildView.

public View getChildView(int groupPosition, int childPosition,
                        boolean isLastChild, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = new InfoView(context);
    }
    return convertView;
}
傾城如夢未必闌珊 2024-10-25 00:02:22

您发布的代码中缺少很多内容:infopanel.xml 在哪里使用?如何用数据填充 InfoView 的子视图?等等。

但是从您发布的内容来看,您似乎正在尝试创建所谓的 复合控件。此类控件(显然)需要从适当管理子视图的布局(例如 LinearLayout)派生。仅当您向 View 类添加了此处未显示的更多代码时,像您所做的那样从 View 派生才有效 - 例如,onMeasureonDraw 的实现

我建议您阅读复合控件上的文档,如果你还没有。

gngr44 的答案也是正确的 - 您不应该尝试以这种方式重用 InfoView。相信 Android 代码会为您回收视图,如果 convertView 不为 null,则使用它。

There's a lot missing from the code you've posted: Where is infopanel.xml used? How do you populate InfoView's child views with data? Etc.

But going from what you have posted, it looks like you're trying to create what's called a Compound Control. Such controls (apparently) need to derive from a Layout, such as LinearLayout, that manages the child views appropriately. Deriving from View as you've done would work only if you had added a lot more code to your View class that you haven't shown here -- for example, implementations of onMeasure and onDraw.

I suggest you read the docs on Compound Controls, if you haven't already.

gngr44's answer is also correct -- you shouldn't be trying to reuse an InfoView in that way. Trust the Android code to recycle the views for you, and use the convertView if it's not null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文