我有一个 ExpandableListView 并正在为其设置一个自定义的 BaseExpandableAdapter。我的问题是,我可以膨胀我想在适配器本身的构造函数中显示的 Layout(XML),而不是每次都在 getChildView() 中膨胀它吗?不过,我将动态更改要在 getChildView() 中选择的每个可扩展列表显示的图像。代码在这里。
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private LayoutInflater mInflater;
GridLayout gl = null;
HorizontalScrollView hScrl;
public MyExpandableListAdapter (ArrayList<SensorType> parentGroup, Context context) {
mContext = context;
mInflater = LayoutInflater.from(mContext);
View view = infalInflater.inflate(R.layout.expandlist_items, null);
gl = (GridLayout) view.findViewById(R.id.gl);
hScrl = (HorizontalScrollView) view.findViewById(R.id.hScroll);
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (childPosition == 0) {
gl1.removeAllViews();
}
int mMax = 8; // show 8 images
for (int i =0; i < mMax; i++) {
ImageView myImg = new ImageView(mContext);
myImg.setBackgroundRes(R.drawable.image1);
gl.addView(myImg);
}
return hScrl;
}
上面有什么问题吗,如果有更多图像,它会正确显示图像并且滚动。但我的问题是,这个膨胀布局和获取 gl 和 hscrl 的工作,应该在 Adapter 的构造函数中(如上所示)还是应该在 getChildView 中?
这 4 个 LOC:
mInflater = LayoutInflater.from(mContext);
View view = infalInflater.inflate(R.layout.expandlist_items, null);
gl = (GridLayout) view.findViewById(R.id.gl);
hScrl = (HorizontalScrollView) view.findViewById(R.id.hScroll);