Solve the problem that the first item does not respond or the display is abnormal due to the dynamic setting of the width and height of the GridView, gridviewitem

Source: Internet
Author: User

Solve the problem that the first item does not respond or the display is abnormal due to the dynamic setting of the width and height of the GridView, gridviewitem

I encountered this problem twice when I was working on the project recently. Now I want to share my experience.

I. Problems

Problem code:

</Pre> <p> <pre name = "code" class = "java"> @ Overridepublic View getView (int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubfinal ViewHolder holder; final Light light = beans. get (position); if (convertView = null) {holder = new ViewHolder (); convertView = inflater. inflate (R. layout. lo_light_item, null); holder. tvName = (TextView) convertView. findViewById (R. id. TV _light_item); holder. image = (ImageView) convertView. findViewById (R. id. iv_light_item); holder. ly = (LinearLayout) convertView. findViewById (R. id. ly_light_item); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag () ;}< pre name = "code" class = "java"> // set the width and height of a single item holder. ly. setLayoutParams (new GridView. layoutParams (WindowManager. layoutParams. FILL_PARENT, gv. getHeight ()/4 ));
Return convertView ;}
 

 
 

Symptoms

1. The View of the first Item is incomplete and always half is displayed. There is no problem with other items. For example, it is useless to set them.

2. The click event set in the first Item does not work, but it takes effect immediately after a click event is gently rolled.

Solution:

<Pre name = "code" class = "java"> @ Overridepublic View getView (int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubfinal ViewHolder holder; final Light light = beans. get (position); if (convertView = null) {holder = new ViewHolder (); convertView = inflater. inflate (R. layout. lo_light_item, null); holder. tvName = (TextView) convertView. findViewById (R. id. TV _light_item); holder. image = (ImageView) convertView. findViewById (R. id. iv_light_item); holder. ly = (LinearLayout) convertView. findViewById (R. id. ly_light_item); // sets the width and height of a single item. ly. setLayoutParams (new GridView. layoutParams (WindowManager. layoutParams. FILL_PARENT, gv. getHeight ()/4); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag () ;}return convertView ;}


 

 

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.