我在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>
Run Code Online (Sandbox Code Playgroud)
我的BaseExpandableListAdapter对于getgroupview() - 方法看起来像这样:
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;
}
Run Code Online (Sandbox Code Playgroud)
对我的问题:
当我在我的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()
Run Code Online (Sandbox Code Playgroud)
getGroupView()和getChildView()为每个输入的字母调用MULTIPLE次.在上面的例子中,listview显然被重绘了八次(我在该列表的列表中有2个组视图和一个子视图.)
我错过了什么?当我在edittext中输入内容时,甚至不需要重新绘制列表视图.即使重新绘制,为什么有八个调用不同的getview-methods?
编辑:我现在看到,即使我崩溃和/或扩展了群视图,也有大量的getgroupview和getchildview-methods调用.这是怎么回事?
编辑2:现在进行了一些测试,在正常活动中使用带有edittext的列表,似乎列表不会经常重新绘制.所以问题似乎在于我在对话中提出这个问题.不知道为什么它甚至需要重新绘制.
你永远无法分辨何时getView调用方法(也是如此getGroupView)和多少次.
但我确实看到了一些可以让它调用超出需要的东西:在你的布局中,你将ExpandableListView高度设置为wrap_content.我认为这不是一件好事:
你让他需要重新绘制它的子,知道它的大小,以便适合它的内容,而它不需要包装它的内容,因为它是一个可滚动的布局.
尝试将其设置为0dipa 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3386 次 |
| 最近记录: |