Android 内含 gridview 的可扩展列表视图
Expandable listview with gridview inside on Android
我正在尝试将一组数据传递到我的 GridAdapter,以便我可以获取它并打印为 gridview,问题是我当前的数组返回 null,我在这里做错了什么?如何获取 child 的数组列表并将其传递给我的 GridAdapter?提前谢谢你。
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<Group> groups;
private ArrayList<Child> child;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public ExpandListAdapter(Context context, ArrayList<Group> groups) {
this.context = context;
this.groups = groups;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
child = groups.get(groupPosition).getItems();
return child.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.gridview, null);
}
CustomGridView gridView = (CustomGridView) convertView
.findViewById(R.id.GridView_toolbar);
gridView.setNumColumns(3);// gridView.setGravity(Gravity.CENTER);//
gridView.setHorizontalSpacing(5);// SimpleAdapter adapter =
GridAdapter adapter = new GridAdapter(context, **child**);
gridView.setAdapter(adapter);// Adapter
int totalHeight = 0;
for (int size = 0; size < adapter.getCount(); size++) {
RelativeLayout relativeLayout = (RelativeLayout) adapter.getView(
size, null, gridView);
TextView textView = (TextView) relativeLayout.getChildAt(0);
textView.measure(0, 0);
totalHeight += textView.getMeasuredHeight();
}
gridView.SetHeight(totalHeight);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Group group = (Group) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.group_name);
tv.setText(group.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
取子数组为
ArrayList<ArrayList<Child>> child = new ArrayList<>();
并在构造函数调用时初始化它
public ExpandListAdapter(Context context, ArrayList<Group> groups) {
this.context = context;
this.groups = groups;
for(int i = 0; i < groups.size(); i++){
child.add(i, groups.get(i).getItems());
}
}
并删除
child = groups.get(groupPosition).getItems();
来自 getChild 方法
我正在尝试将一组数据传递到我的 GridAdapter,以便我可以获取它并打印为 gridview,问题是我当前的数组返回 null,我在这里做错了什么?如何获取 child 的数组列表并将其传递给我的 GridAdapter?提前谢谢你。
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<Group> groups;
private ArrayList<Child> child;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public ExpandListAdapter(Context context, ArrayList<Group> groups) {
this.context = context;
this.groups = groups;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
child = groups.get(groupPosition).getItems();
return child.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.gridview, null);
}
CustomGridView gridView = (CustomGridView) convertView
.findViewById(R.id.GridView_toolbar);
gridView.setNumColumns(3);// gridView.setGravity(Gravity.CENTER);//
gridView.setHorizontalSpacing(5);// SimpleAdapter adapter =
GridAdapter adapter = new GridAdapter(context, **child**);
gridView.setAdapter(adapter);// Adapter
int totalHeight = 0;
for (int size = 0; size < adapter.getCount(); size++) {
RelativeLayout relativeLayout = (RelativeLayout) adapter.getView(
size, null, gridView);
TextView textView = (TextView) relativeLayout.getChildAt(0);
textView.measure(0, 0);
totalHeight += textView.getMeasuredHeight();
}
gridView.SetHeight(totalHeight);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Group group = (Group) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.group_name);
tv.setText(group.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
取子数组为
ArrayList<ArrayList<Child>> child = new ArrayList<>();
并在构造函数调用时初始化它
public ExpandListAdapter(Context context, ArrayList<Group> groups) {
this.context = context;
this.groups = groups;
for(int i = 0; i < groups.size(); i++){
child.add(i, groups.get(i).getItems());
}
}
并删除
child = groups.get(groupPosition).getItems();
来自 getChild 方法