В первый раз только ExpandableListView, но мой Клиент просит меня добавить GridView внутри ExpandableListView, так что это мой код:
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 с текстом и ImageView
Фактически, пункт «Спорт» будет отображать 3 элемента, например, «Плавание», «Езда на велосипеде» и «Пеший туризм», не все из них отображаются в одном и том же элементе.Я не знаю, как это исправить, пожалуйста, помогите мне, спасибо.