我按照许多教程和SO的一些错误问题制作了ExpandableListView。父列表由两个TextView和一个具有标准布局的ImageButton组成。子列表由单个TextView组成。数据源来自活动的SQLite数据库,并生成到ArrayList。
问题:
即使按一下按钮,父组列表也可以正常工作,但是当我尝试扩展时,它不会扩展。因此,我尝试放置一些Toast消息,但是在打开活动之后,但看起来getChildView()
方法根本没有触发,因为Toast消息根本没有显示。
ExpandableListAdapter类就是这样
public class InventoryListExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<Inventory> data;
private HashMap<String, ArrayList<String>> list_data;
public InventoryListExpandableListAdapter(Context context, ArrayList<Inventory> data, HashMap<String, ArrayList<String>> list_data) {
this.context = context;
this.data = data;
this.list_data = list_data;
}
@Override
public int getGroupCount() {return data.size();}
@Override
public int getChildrenCount(int groupPosition) {return list_data.get(data.get(groupPosition)).size();}
@Override
public Inventory getGroup(int groupPosition) {return data.get(groupPosition);}
@Override
public Object getChild(int groupPosition, int childPosition) {return list_data.get(data.get(groupPosition).getCode()).get(childPosition);}
@Override
public long getGroupId(int groupPosition) {return groupPosition;}
@Override
public long getChildId(int groupPosition, int childPosition) {return childPosition;}
@Override
public boolean hasStableIds() {return true;}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
final Inventory inventory = getGroup(groupPosition);
convertView = View.inflate(context, R.layout.listview_layout_two_row_delete, null);
ImageButton btnDelete = (ImageButton)convertView.findViewById(R.id.btnDelete);
TextView text1 = (TextView)convertView.findViewById(R.id.text1);
TextView text2 = (TextView)convertView.findViewById(R.id.text2);
text1.setText(inventory.getCode());
text2.setText(inventory.getItem());
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "DELETED", Toast.LENGTH_SHORT).show();
}
});
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String)getChild(groupPosition, childPosition);
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = infalInflater.inflate(R.layout.listview_layout_single_text, null);
}
Toast.makeText(context, "Make Child number "+String.valueOf(childPosition), Toast.LENGTH_SHORT).show();
TextView text1 = (TextView)convertView.findViewById(R.id.text1);
text1.setText(childText);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
感谢您提供的任何帮助。谢谢。
我花了数小时试图找到相同的问题,才意识到这对我来说简直是一件简直很简单的事情:
android:focusable =“ false”android:focusableInTouchMode =“ false”
尝试调用expandableListView.expandGroup(0 ... n)进行手动扩展,并查看它是否在适配器中调用了与子相关的方法
我发现了在ExpandedListAdapter类中没有自动调用getChildView()的另一种方法。这是一种捷径,似乎可以解决该问题。我有一个扩展的列表视图,其中父组项是复选框列表。单击复选框将显示子项。这是我编码getGroupView()函数的方式。
更改hasStableIds方法
检查列表项布局的根布局可点击属性。