1
我有一個ExpandableListView,並且正在爲它設置一個自定義的BaseExpandableAdapter。我的問題是,我可以誇大我想顯示在適配器本身的構造函數中的佈局(XML),而不是每次都在getChildView()中膨脹它?我將動態地更改要顯示的圖像,但每個可擴展列表都要在getChildView()中選擇。代碼在這裏。在適配器的構造函數中填充xml文件
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的工作,應該在適配器的構造函數中(如上所示),或者它應該在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);