我正在使用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会一直被调用!这是日志的输出,只是在edittext中添加一个字母:
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()为每个输入的字母调用MULTIPLE次。在上面的示例中,listview显然被重绘了八次(我在该示例的列表中有2个组视图和一个子视图。)
我错过了什么?当我在edittext中输入内容时,甚至不需要重新绘制列表视图。即使重新绘制,为什么有八个调用不同的getview-methods?
编辑: 我现在看到,即使我崩溃和/或扩展了群视图,也会对getgroupview和getchildview-methods进行大量调用。这是怎么回事?
EDIT2: 现在进行了一些测试,在正常活动中使用带有edittext的列表,似乎列表不会经常重新绘制。所以问题似乎在于我在对话中提出这个问题。看不出为什么它甚至需要重新绘制。
答案 0 :(得分:7)
你永远不知道何时调用getView
方法(getGroupView
也是如此)和多少次。
但是我确实看到了一些可以让它调用超出需要的东西:在你的布局中,你将ExpandableListView
高度设置为 wrap_content 。我认为这不是一件好事:
你让他需要重新绘制它的子,知道它的大小以便适合它的内容,而它不需要包装它的内容,因为它是一个可滚动的布局。
尝试使用0dip
将其设置为layout_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>