i have a GridView with an custom BaseAdapter. In each cell i(or user) can choose(trough a button) between 3 layouts(small, medium, large) to inflate. If i choose small, every cell gets the layout small:
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null) {
View temporaryView = myInflater.inflate(R.layout.activity_dialog_box, (ViewGroup) myDialogBox.getWindow().getDecorView().findViewById(android.R.id.content));
Button scaleButton = (Button) temporaryView.findViewById(R.id.dialog_button_scale);
switch (scaleButton.getText().toString()) {
case "small":
Log.e("small","true");
convertView = myInflater.inflate(R.layout.customadapter_view_layout_small, parent, false);
myImg = (ImageView) convertView.findViewById(R.id.imageview_layout_normal_portrait_sizesmall);
myLabelTextView = (TextView) convertView.findViewById(R.id.textview_label_layout_normal_portrait_sizesmall);
myABCTextView = (TextView) convertView.findViewById(R.id.textview_abc_layout_normal_portrait_sizesmall);
break;
case "medium":
Log.e("medium","true");
convertView = myInflater.inflate(R.layout.customadapter_view_layout_medium, parent, false);
myImg = (ImageView) convertView.findViewById(R.id.imageview_layout_normal_portrait_sizemedium);
myLabelTextView = (TextView) convertView.findViewById(R.id.textview_label_layout_normal_portrait_sizemedium);
myABCTextView = (TextView) convertView.findViewById(R.id.textview_abc_layout_normal_portrait_sizemedium);
break;
case "large":
Log.e("large","true");
convertView = myInflater.inflate(R.layout.customadapter_view_layout_large, parent, false);
myImg = (ImageView) convertView.findViewById(R.id.imageview_layout_normal_portrait_sizelarge);
myLabelTextView = (TextView) convertView.findViewById(R.id.textview_label_layout_normal_portrait_sizelarge);
myABCTextView = (TextView) convertView.findViewById(R.id.textview_abc_layout_normal_portrait_sizelarge);
break;
default:
break;
}
convertView.setTag(new Holder(myImg, myLabelTextView, myABCTextView));
At Startup, for every Cell which fits on the Screen, convertView is null. So the Switch case and the inflating of the layout and the initialization of the ImagView and the two TextViews happens quite often. For this case let's say user selected small:
switch (scaleButton.getText().toString()) {
case "small":
Log.e("small","true");
convertView = myInflater.inflate(R.layout.customadapter_view_layout_small, parent, false);
myImg = (ImageView) convertView.findViewById(R.id.imageview_layout_normal_portrait_sizesmall);
myLabelTextView = (TextView) convertView.findViewById(R.id.textview_label_layout_normal_portrait_sizesmall);
myABCTextView = (TextView) convertView.findViewById(R.id.textview_abc_layout_normal_portrait_sizesmall);
break;
(Bad for Performance). So i thought, why don't set the rootView for the specific layout-size in a seperate thread:
private View inflateLayout() {
View myTestView = null;
switch (scaleButtonText) {
case "small":
myTestView = myInflater.inflate(R.layout.customadapter_view_layout_small, (ViewGroup) gv, false);
break;
case "medium":
myTestView = myInflater.inflate(R.layout.customadapter_view_layout_medium, (ViewGroup) gv, false);
break;
case "large":
myTestView = myInflater.inflate(R.layout.customadapter_view_layout_large, (ViewGroup) gv, false);
break;
default:
break;
}
return myTestView;
}
Where gv is my GridView. And give it to the adapter:
myCustomAdapter = new NewCustomAdapter(myActivityContext, myTestView);
And later, simply set convertView to myTestView in the getView method:
case "small":
Log.e("small","true");
convertView = myTestView;
myImg = (ImageView) convertView.findViewById(R.id.imageview_layout_normal_portrait_sizesmall);
myLabelTextView = (TextView) convertView.findViewById(R.id.textview_label_layout_normal_portrait_sizesmall);
myABCTextView = (TextView) convertView.findViewById(R.id.textview_abc_layout_normal_portrait_sizesmall);
break;
The Advantage would be that i don't have to inflate the layout again and again and again at starting the app.
The Problem: I miss something. Because the layout isn't really inflated, the GridView doesn't really exist or just in a messy condition. Can somebody explain me why it can't work like this or what i'm missing. Any help is appreciated.