4

我正在使用 Android 中的 ExpandableListView。

我有一个夸大这个视图的对话框:

<ExpandableListView
    android:id="@+id/comments_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/editTextNewComment"
    android:layout_alignParentLeft="true" >
</ExpandableListView>

<EditText
    android:id="@+id/editTextNewComment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/imageButtonPostNewComment"
    android:ems="10"
    android:singleLine="true" >

    <requestFocus />
</EditText>

对于 getgroupview() 方法,我的 BaseExpandableListAdapter 看起来像这样:

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
        ViewGroup parent) {

    View row = convertView;
    ViewHolder holder = null;

    Log.d("LIST", "getGroupView() groupPosition " + groupPosition + "  isExpanded " + isExpanded + " row " + row);

    if(row==null) {
        LayoutInflater infl = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = infl.inflate(R.layout.list_item_comment,parent,false);
        holder = new ViewHolder(row);
        holder.imbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Integer pos = (Integer) v.getTag();
                //Do something
            }
        });
        row.setTag(holder);
    }
    else {
        holder = (ViewHolder) row.getTag();
    }
    holder.imbutton.setTag(Integer.valueOf(groupPosition));
    holder.header.setText(""+ (groupPosition+1) + " - " + comments[groupPosition].getOwner());
            //.... etc - setting all the textviews accordingly

    return row;
}

对于我的问题:

当我在我的edittext中写东西时,getgroupview一直被调用!这是仅在编辑文本中添加一个字母的日志输出:

03-15 14:07:51.072: D/LIST(4176): getGroupView() groupPosition 0  isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.080: D/LIST(4176): getGroupView() groupPosition 1  isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.088: D/LIST(4176): getChildView()
03-15 14:07:51.096: D/LIST(4176): getGroupView() groupPosition 0  isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.104: D/LIST(4176): getGroupView() groupPosition 1  isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.111: D/LIST(4176): getChildView()
03-15 14:07:51.119: D/LIST(4176): getGroupView() groupPosition 0  isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.119: D/LIST(4176): getGroupView() groupPosition 1  isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.127: D/LIST(4176): getChildView()
03-15 14:07:51.135: D/LIST(4176): getGroupView() groupPosition 0  isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.135: D/LIST(4176): getGroupView() groupPosition 1  isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.143: D/LIST(4176): getChildView()
03-15 14:07:51.151: D/LIST(4176): getGroupView() groupPosition 0  isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.151: D/LIST(4176): getGroupView() groupPosition 1  isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.158: D/LIST(4176): getChildView()
03-15 14:07:51.205: D/LIST(4176): getGroupView() groupPosition 0  isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.205: D/LIST(4176): getGroupView() groupPosition 1  isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.213: D/LIST(4176): getChildView()
03-15 14:07:51.213: D/LIST(4176): getGroupView() groupPosition 0  isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.213: D/LIST(4176): getGroupView() groupPosition 1  isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.221: D/LIST(4176): getChildView()
03-15 14:07:51.221: D/LIST(4176): getGroupView() groupPosition 0  isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.221: D/LIST(4176): getGroupView() groupPosition 1  isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0}
03-15 14:07:51.229: D/LIST(4176): getChildView()

对于输入的每个字母,getGroupView() 和 getChildView() 都会被调用多次。在上面的示例中,列表视图显然被重绘了八次(对于该示例,我在列表中有 2 个组视图和一个子视图。)

我错过了什么?当我在编辑文本中键入内容时,甚至不需要重绘列表视图。即使它被重绘,为什么会有八次调用不同的 getview 方法?

编辑:我现在看到,即使我折叠和/或展开组视图,也有很多调用 getgroupview 和 getchildview-methods。这是应该的吗?

EDIT2:现在在正常活动中使用带有edittext的列表进行了一些测试,似乎该列表没有那么频繁地重绘。所以问题似乎在于我在对话框中提出这个问题。看不出为什么它甚至需要重绘。

4

1 回答 1

7

您永远无法知道何时getView调用方法(对于 也是如此getGroupView)以及调用多少次。

但我确实看到了一些可以使其调用超过需要的东西:在您的布局中,您将ExpandableListView高度设置为wrap_content。我不认为这是一件好事:

你让他需要重绘它的孩子以知道它的大小以适应它的内容,而它不需要包装它的内容,因为它是一个可滚动的布局。

尝试将其设置为0diplayout_weight="1"以便为您的文本视图留出空间:

<ExpandableListView
    android:id="@+id/comments_list"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:layout_above="@+id/editTextNewComment"
    android:layout_alignParentLeft="true" >
</ExpandableListView>
于 2013-03-15T13:26:55.603 回答