I have an ExpandableListView and am setting a customized BaseExpandableAdapter to it. My question is, Can I inflate the Layout(XML) that I want to be displayed in the constructor of the Adapter itself, rather than inflating it in the getChildView() each time? I will be dynamically changing the images that are to be displayed with each expandable List to be selected in getChildView() though. The code goes here.
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;
}
Is there any problem with above, it is displaying the images correctly and scrolling as well if there are more images. But my question is, this job of inflating layout and getting the gl and hscrl, should this be in the constructor of the Adapter(as shown above) OR should it be in the getChildView???
These 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);
I have found out that If I need a layout to be inflated again and again for each item in the adapter then the inflate should be done in getChildView() or getView() of any adapter.
However in the above code I am inflating it once in the constructor and adding the images manually to a GridLayout hence I need not inflate it again and again in the getChildView().
Hence it works fine, as it is inflated once in the constructor of the adapter.