So, the issue is with how you're creating the View
in this method, and there's a couple of things going wrong:
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout linearlayout=new LinearLayout(mContext);
ImageView imageView = new ImageView(mContext);
TextView textView =new TextView(mContext);
textView.setGravity(Gravity.CENTER);
linearlayout.setOrientation(LinearLayout.VERTICAL);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setLayoutParams(new GridView.LayoutParams(230, 230));
textView.setText(mThumbTxt[position]);
linearlayout.addView(imageView);
linearlayout.addView(textView);
return linearlayout;
}
1) Right now, you're ignoring the convertView, so you're wasting GridView
s recycling mechanism by not checking if that is null before instantiating the View
.
2) You're attaching GridView.LayoutParams to a child View nested within a LinearLayout. The LinearLayout should have GridView.LayoutParams, but the LinearLayout's children should have LinearLayout.LayoutParams.
3) There's a difference between layout_gravity
, and gravity
-- you're using gravity, which for LinearLayout won't work as you think it should. (Unless in this context you change your TextView to be match_parent for width, but then that might mess other things up)
I'd recommend ditching the dynamic creation and taking an XML inflation approach.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…