在 ExpandableListView 中添加 GridView

Adding a GridView inside an ExpandableListView

第一次,只有 ExpandableListView,但我的客户要求我在 ExpandableListView 中添加 GridView,所以这是我的代码:

  private List<String> Group;
  private List<List<String>> Child;
  private List<String> Child_1;
  private List<String> Child_2;
  private List<String> Child_3;
  private List<List<Integer>> ChildPicture;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Group = new ArrayList<String>();
    Group.add("SPORT");
    Group.add("Healthy");
    Group.add("Measure");

    Child_1 = new ArrayList<String>();
    Child_1.add("Swimming");
    Child_1.add("Biking");
    Child_1.add("Hiking");

    Child_2 = new ArrayList<String>();
    Child_2.add("Walking");
    Child_2.add("Running");

    Child_3 =new ArrayList<String>();
    Child_3.add("Measure");

    Child = new ArrayList<List<String>>();
    Child.add(Child_1);
    Child.add(Child_2);
    Child.add(Child_3);

    List<Integer> ChildPic_1 = new ArrayList<Integer>();
    ChildPic_1.add(R.drawable.ic_swimming);
    ChildPic_1.add(R.drawable.ic_bike);
    ChildPic_1.add(R.drawable.ic_hiking);

    List<Integer> ChildPic_2 = new ArrayList<Integer>();
    ChildPic_2.add(R.drawable.ic_walk);
    ChildPic_2.add(R.drawable.ic_running);

    List<Integer> ChildPic_3 = new ArrayList<Integer>();
    ChildPic_3.add(R.drawable.ic_matters);

    ChildPicture = new ArrayList<List<Integer>>();
    ChildPicture.add(ChildPic_1);
    ChildPicture.add(ChildPic_2);
    ChildPicture.add(ChildPic_3);

    ExpandableListView elv = findViewById(R.id.expenview);
    elv.setAdapter(new MyExpandableAdapter(Main2Activity.this));
    elv.setGroupIndicator(null);
   }

   class MyExpandableAdapter extends BaseExpandableListAdapter{
    private Context context;
    private int[] groupLogo = new int[] { R.drawable.fitness, R.drawable.walking, R.drawable.temp };

    public MyExpandableAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return Group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return Child.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return Group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return Child.get(groupPosition).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) {
        GroupHolder groupHolder = null;

        if (convertView == null){
            convertView = getLayoutInflater().inflate(R.layout.expandablelist_group, null);
        }
        groupHolder = new GroupHolder();
        groupHolder.text = (TextView)convertView.findViewById(R.id.group_name);
        groupHolder.imageview = (ImageView) convertView.findViewById(R.id.group_icon);
        groupHolder.text.setText(Group.get(groupPosition));
        groupHolder.imageview.setImageResource(groupLogo[groupPosition]);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.gridview_item, null);
        }
        GridView gv = (GridView) convertView;
        gv.setAdapter(new ImageViewAdapter(Main2Activity.this, Child, ChildPicture));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

class GroupHolder {
    public TextView text;
    public ImageView imageview;
}

class ImageViewAdapter extends BaseAdapter{
    private Context context;
    private List<List<String>> Child;
    private List<List<Integer>> ChildPicture;

    public ImageViewAdapter(Context context, List<List<String>> child, List<List<Integer>> childPicture) {
        this.context = context;
        Child = child;
        ChildPicture = childPicture;
    }

    @Override
    public int getCount() {
        return Child.size();
    }

    @Override
    public Object getItem(int position) {
        return Child.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.gridview_item, null);
        }
        TextView itemHolderText = (TextView) convertView.findViewById(R.id.item_name);
        ImageView itemHolderImg = (ImageView) convertView.findViewById(R.id.item_icon);
        itemHolderText.setText(Child.get(position).toString()); //Strong?
        return convertView;
    }
}

然后我得到这样的结果:

GridView with text and ImageView

事实上,运动项目会显示 3 个项目,如游泳、骑自行车和徒步旅行, 并非全部显示在同一项目中。不知道怎么解决,求大神指教,谢谢。

您可以使用 Below Library 或从中获取一些想法来为 ExpandableListViewGridView 实现您自己的实现。

这个示例项目是对分段的、可扩展的、网格的简单实现的尝试RecyclerView

通过使用 GridLayoutManager 实现功能。

SectionedExpandableLayoutHelper class 获取数据,将其按照要求的格式传递给 SectionedExpandableGridAdapter

创建的助手 class 允许 addition/removal 整个部分,还提供了从现有部分 add/remove 单个项目的工具

SectionedExpandableGridRecyclerView-master

对于带有 ListView children 的 ExpandableListView,它使用 getChildrenCount() 来了解需要创建多少 child 个视图和 getChildView() 为组中的单个 child 创建单个项目视图。

但是 GridLayout/GridView/ViewPager 作为 child 视图,那么只有 只有一个 child 视图 [所以 getChildrenCount() return 1] 因此 getChildView() 处理 整个 CHILD 组列表 .

为您的 ExpandableListView 和 GridView 尝试以下适配器:

class MyExpandableAdapter extends BaseExpandableListAdapter {
    private Context context;
    private int[] groupLogo = new int[] { R.drawable.fitness, R.drawable.walking, R.drawable.temp };

    public MyExpandableAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return Group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1; //Changed
    }

    @Override
    public Object getGroup(int groupPosition) {
        return Group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return Child.get(groupPosition).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) {
        GroupHolder groupHolder = null;

        if (convertView == null){
            convertView = getLayoutInflater().inflate(R.layout.expandablelist_group, null);
        }
        groupHolder = new GroupHolder();
        groupHolder.text = (TextView)convertView.findViewById(R.id.group_name);
        groupHolder.imageview = (ImageView) convertView.findViewById(R.id.group_icon);
        groupHolder.text.setText(Group.get(groupPosition));
        groupHolder.imageview.setImageResource(groupLogo[groupPosition]);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.gridview_item, null);
        }
        GridView gv = (GridView) convertView;
        gv.setAdapter(new ImageViewAdapter(Main2Activity.this, Child.get(groupPosition), ChildPicture.get(groupPosition))); //Changed
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

class GroupHolder {
    public TextView text;
    public ImageView imageview;
}

class ImageViewAdapter extends BaseAdapter {
    private Context context;
    private List<String> Child; //Changed
    private List<Integer> ChildPicture; //Changed

    public ImageViewAdapter(Context context, List<String> child, List<Integer> childPicture) { //Changed
        this.context = context;
        this.Child = child; //Changed
        this.ChildPicture = childPicture; //Changed
    }

    @Override
    public int getCount() {
        return Child.size();
    }

    @Override
    public Object getItem(int position) {
        return Child.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.gridview_item, null);
        }
        TextView itemHolderText = (TextView) convertView.findViewById(R.id.item_name);
        ImageView itemHolderImg = (ImageView) convertView.findViewById(R.id.item_icon);
        itemHolderText.setText(Child.get(position).toString()); //Strong?
        return convertView;
    }
}

希望对您有所帮助!