最好的解决方案是切换回 aListView
并使用ExpandableListView
接口,或者自己实现它RecyclerView
。
正如您所提到的 - 列出滚动组件从来都不是一个好的解决方案。这是一个示例可扩展列表视图适配器,因此您知道需要什么:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
...
@Override
public Object getChild(int listPosition, int childListPosition) {
//return an item that would have been in one of the nested recyclers
//(listPosition = parent, childListPosition = nested item number)
return getGroup(listPosition).getChildren().get(childListPosition);
}
@Override
public int getChildrenCount(int listPosition) {
//presuming the parent items contain the children
return getGroup(listPosition).getChildren().size();
}
@Override
public Object getGroup(int listPosition) {
//group is the parent items (the tope level recycler view items)
return mData.get(listPosition);
}
@Override
public int getGroupCount() {
return mData.size();
}
@Override
public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, parent, false);
}
MyDataType item = getGroup(listPosition);
//set the fields (or better yet, use viewholder pattern)
return convertView;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, parent, false);
}
MyDataType item = getChild(listPosition, expandedListPosition);
//set the fields (or better yet, use viewholder pattern)
return convertView;
}
}