我根据许多教程和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;
}
}
感谢您提供的任何帮助。谢谢。
答案 0 :(得分:2)
我花了好几个小时试图找到同样的问题,只是意识到这对我来说很简单:
机器人:可聚焦= “假” 机器人:focusableInTouchMode = “假”
尝试调用expandableListView.expandGroup(0 ... n)手动展开它,看看它是否调用了适配器中与子项相关的方法
答案 1 :(得分:1)
我发现了一种不同的方法让getChildView()在ExpandedListAdapter类中没有自动调用时被调用。它是一个似乎可以解决问题的快捷方式。我有一个扩展列表视图,父组项是复选框列表。单击复选框可显示子项。这是我编写getGroupView()函数的方式。
@Override
public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, final ViewGroup viewGroup)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) m_context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.expandable_list_milk_type,null);
}
final CheckBox cbMilkType = (CheckBox) convertView.findViewById(R.id.cb_milk_type);
cbMilkType.setText(m_listMilktype.get(groupPosition));
cbMilkType.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ExpandableListView expandableListView = (ExpandableListView)viewGroup;
cbMilkType.toggle();
if (!isExpanded) {
expandableListView.expandGroup(groupPosition);
}
else {
expandableListView.collapseGroup(groupPosition);
}
}
});
return convertView;
}
答案 2 :(得分:0)
更改hasStableIds方法
@Override
public boolean hasStableIds() {return false;}
**请更改你的getGroupView方法:: **
@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;
}