在ExpandableListView内添加GridView

时间:2018-11-28 06:51:36

标签: android arraylist gridview imageview 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个项目,例如“游泳”,“骑自行车”和“徒步旅行”, 并非所有项目都显示在同一项目中。我不知道如何解决它,请帮助我,谢谢。

2 个答案:

答案 0 :(得分:0)

您可以使用“下面的库”或从中获取一些想法,以使用ExpandableListViewGridView进行自己的实现。

此示例项目是对分段可扩展网格RecyclerView

的简单实现的尝试。

通过使用GridLayoutManager来实现功能。

SectionedExpandableLayoutHelper类获取数据,将其放入所需格式并将其传递给SectionedExpandableGridAdapter

创建的Helper类允许完全添加/删除整个节,并且还提供了从现有节中添加/删除单个项的功能

SectionedExpandableGridRecyclerView-master

答案 1 :(得分:0)

对于具有ListView子级的ExpandableListView,它使用 getChildrenCount()知道需要创建多少子级视图,并且 getChildView()为一个子项的单个子级创建单个项目视图组。

但是将GridLayout / GridView / ViewPager作为子视图,则只有一个子视图[因此getChildrenCount()返回1],因此getChildView()处理整个孩子列表一组

为您的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;
    }
}

希望有帮助!