android - 当我在 EditText 中编辑数据时,BaseExpandableListAdapter 调用 getgroupview()

标签 android

我在 Android 中使用 ExpandableListView。

我有一个对话框使这个 View 膨胀:

<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() 被调用多次。在上面的示例中, ListView 显然被重绘了八次(我在该示例的列表中有 2 个组 View 和一个 subview 。)

我错过了什么?当我在编辑文本中输入内容时,甚至不需要重新绘制 ListView 。即使正在重绘,为什么会有八次调用不同的 getview 方法?

编辑: 我现在看到,即使当我折叠和/或展开组 View 时,也有大量调用 getgroupview 和 getchildview 方法。这是应该的样子吗?

编辑2: 现在在正常 Activity 中使用带有 edittext 的列表进行了一些测试,看起来列表没有那么频繁地重新绘制。所以问题似乎在于我在对话框中展示了这个。不明白为什么它甚至需要重新绘制。

最佳答案

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

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

你让他需要重新绘制它的子元素以了解它的大小以适应它的内容,而它不需要包装它的内容,因为它是一个可滚动的布局。

尝试将其设置为 0dip,使用 layout_weight="1" 以便为 TextView 留出空间:

<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>

关于android - 当我在 EditText 中编辑数据时,BaseExpandableListAdapter 调用 getgroupview(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15433456/

相关文章:

android - 无法制作仅限平板电脑的应用程序

android - 原始数组大小和 list.size 返回不同的值

java - Android Volley 空指针异常

java - 观察应用程序所做的音量变化

android - 从 ActionBar 迁移到 ToolBar

市场上的安卓应用

android - AdMob 集成到 Android 版 Adob​​eAIR 应用程序中

java - TextView 没有得到更新

java - 错误 - 无法解析导入 com.google.android.gms.ads

java - 将字符串内部的数组解析为字符串数组