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 ;}