GROUP_1 subGroup_1 - >网络呼叫,填充数据。 (onClick事件打开另一个列表)(ListView) - >网络呼叫,填充数据(ListView) subGroup_2 - >网络呼叫,填充数据。 (onClick事件另一个列表打开) - >网络电话,填充数据
SubGroup_1和SubGroup_2是硬编码的。单击SubGroup_1进行网络调用并填充列表,然后填写List_dataset_1 再次进行List_dataset_1的onClick事件,进行另一次网络调用,并填充另一个列表视图。
我尝试使用可扩展的ListView但没有成功。请建议我实现此类视图的方法,或者可以使用Expandable list view来实现此结果?
答案 0 :(得分:0)
我收到了你的问题。我只是给你一个关于如何实现上述场景的想法。以下代码只是演示。你必须实现自己的。请参阅以下代码:
这是GroupExpandableAdapter:
public class GroupExpandableAdapter extends BaseExpandableListAdapter
{
private ArrayList<Group> mGroupList;
private Context mContext;
private LayoutInflater inflater;
private SubGroupExpandableAdapter mSubGroupExpandableAdapter;
public GroupExpandableAdapter(Context context, ArrayList<Group> groupList)
{
this.mGroupList = groupList;
this.mContext = context;
}
@Override
public int getGroupCount()
{
return mGroupList.size();
}
@Override
public int getChildrenCount(int groupPosition)
{
return 1; // for child expandable list view
}
@Override
public Object getGroup(int groupPosition)
{
return mGroupList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
return null;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return 0;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
// implement your groupView code that you want to show
return null;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
ChildHolder childHolder;
if (convertView == null)
{
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.your_sub_group_expandable_view, null);
childHolder = new ChildHolder();
childHolder.subGroupExpandableListView = (ExpandableListView) convertView.findViewById(R.id.sub_group_expandable_view);
convertView.setTag(childHolder);
}
else
{
childHolder = (ChildHolder) convertView.getTag();
}
initializeSubGroupAdapter(groupPosition);
childHolder.subGroupExpandableListView.setAdapter(mSubGroupExpandableAdapter)
return convertView;
}
private void initializeSubGroupAdapter(int groupPosition)
{
mSubGroupExpandableAdapter = new SubGroupExpandableAdapter(mContext, mGroupList.get(groupPosition))
}
private static class ChildHolder
{
ExpandableListView subGroupExpandableListView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
以下代码适用于您的子组:
public class SubGroupExpandableAdapter extends BaseExpandableListAdapter
{
private ArrayList<SubGroup> mSubGroupList;
private Context mContext;
private Group mGroup;
public SubGroupExpandableAdapter(Context context, Group group)
{
this.mContext = context;
this.mGroup = group;
this.mSubGroupList = group.getSubGroupList();
}
@Override
public int getGroupCount()
{
return mSubGroupList.size();
}
@Override
public int getChildrenCount(int groupPosition)
{
return mSubGroupList.get(groupPosition).getItemOfSubGroup().size();
}
@Override
public Object getGroup(int groupPosition)
{
return mSubGroupList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mSubGroupList.get(groupPosition).getItemOfSubGroup().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(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
// Implement your subGroup view here
return null;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
// Implement itemOfSubGroup view here
return null;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
这是你的MainActivity,你必须传递模型数据:
public class MainActivity extends Activity
{
private ExpandableListView mGroupExpandableListView;
private GroupExpandableAdapter mGroupExpandableAdapter;
private ArrayList<Group> mGroupList;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView()
{
mGroupList = new ArrayList<Group>(); // fill your modeldata from server
mGroupExpandableListView = (ExpandableListView) findViewById(R.id.groupExpandableListView);
initializeAdapter();
}
private void initializeAdapter()
{
mGroupExpandableAdapter = new GroupExpandableAdapter(MainActivity.this, groupList)
mGroupExpandableListView.setAdapter(mGroupExpandableAdapter);
}
}
希望它可以帮到你。