The first Item is not displayed when the width and height of the Item are dynamically set in the GridView. gridviewitem
A Gridview is added to viewpaper. Each viewpaper must be exactly filled with 3x2 display. Since girdview is vertically scalable, I need to dynamically set the item height in the Gridview to confirm that the layout is full.
Problem: the first item in the Gridview is not displayed.
To dynamically set the height of an Item, you only need to use the parent width and height in the getView method of the adapter for re-calculation.
Let's just talk about the code.
<Pre name = "code" class = "java"> @ Overridepublic View getView (final int position, View convertView, ViewGroup parent) {Holder holder = null; if (null = convertView) {holder = new Holder (); LayoutInflater mInflater = LayoutInflater. from (mContext); convertView = mInflater. inflate (R. layout. f2_gridview_item, parent, false);/* set the convertview size dynamically based on parent */convertView. setLayoutParams (new AbsListView. layoutParams (int) (parent. getWidth ()/3)-1, (int) (parent. getHeight ()/2); // dynamically sets the height of the item holder. btn_gv_item = (ImageView) convertView. findViewById (R. id. btn_gv_item); convertView. setTag (holder);} else {holder = (Holder) convertView. getTag ();/* solves the problem of dynamically setting the convertview size. The first BUG is not displayed */convertView. setLayoutParams (new AbsListView. layoutParams (int) (parent. getWidth ()/3)-1, (int) (parent. getHeight ()/2); // dynamically sets the height of the item} // _ Holder. btn_gv_item.setText (mLists. get (position); return convertView ;}
Pictures with bugs
Images after bug resolution
The bug modification method and bug. Please compare and try it out