The first Item is not displayed when the width and height of the Item are dynamically set in the GridView. gridviewitem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.