【问题标题】:Adding a GridView inside an ExpandableListView在 ExpandableListView 中添加 GridView
【发布时间】:2019-04-30 00:32:45
【问题描述】:

第一次,只有 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

事实上,Sport item 会显示 3 个项目,例如 Swimming 、 Biking 和 Hiking, 并非全部显示在同一项目中。我不知道如何解决它,请帮助我,谢谢。

【问题讨论】:

    标签: android arraylist gridview imageview expandablelistview


    【解决方案1】:

    您可以使用下面的库或从中获取一些想法来为ExpandableListViewGridView 制作自己的实现。

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

    通过GridLayoutManager实现功能。

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

    创建的助手类允许添加/删除整个部分,还提供了从现有部分添加/删除单个项目的功能

    SectionedExpandableGridRecyclerView-master

    【讨论】:

      【解决方案2】:

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

      但使用 GridLayout/GridView/ViewPager 作为子视图,则有 ONLY ONE 子视图 [因此 getChildrenCount() 返回 1],因此 getChildView() 处理 WHOLE CHILD LIST OF一个组

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

      希望有帮助!

      【讨论】:

      • 非常感谢。最后,你解决了我的问题。再次感谢。
      • 我可以问你一个问题吗?如果我想 R.drawable.xxx 可以被点击,我该怎么办?例如: gv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView> parent, View view, int position, long id) switch(xxx) { case R.drawable.ic_swimming: } }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      相关资源
      最近更新 更多