[Android FAQ] The problem that the first Item does not respond or the display is abnormal due to the dynamic setting of the Item width and height of the GridView. androidgridview

Source: Internet
Author: User

[Android FAQ] The problem that the first Item does not respond or the display is abnormal due to the dynamic setting of the Item width and height of the GridView. androidgridview


Preface

This problem was encountered in a previous project and recently encountered a very strange problem in using the GridView. Here we will record and share it.

 

StatementWelcome to repost, but please keep the original source of the article :) blog Park: http://www.cnblogs.com farmer UNCLE: http://over140.cnblogs.com

 

Body

I. Problems

1.1 check the problem code first:

@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
If (convertView = null ){
ConvertView = LayoutInflater. from (getActivity (). inflate (R. layout. fragment_feed_grid_item, parent, false );
ConvertView. findViewById (R. id. avatar). setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
// StartActivity
}
});
}

ConvertView. setLayoutParams (new GridView. LayoutParams (m1_wwidth/3, mWidowWidth/3 ));

// Other code
Return convertView;
}

The purpose of the code is to dynamically set the Item size in the GridView Based on the screen width, which is naturally written in this way.

 

1.2 specific symptoms

1.2.1 The ImageView 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 it.

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

 

Ii. Solution

@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
If (convertView = null ){
ConvertView = LayoutInflater. from (getActivity (). inflate (R. layout. fragment_feed_grid_item, parent, false );
ConvertView. setLayoutParams (new GridView. LayoutParams (m1_wwidth/3, m1_wwidth/3 ));
ConvertView. findViewById (R. id. user_avatar). setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
// StartActivity
}
});
}

// Other code
Return convertView;
}

As shown above.

 

Iii. Related Articles

Why is changing where layout parameters are set solving my "OnClickListener not working for first item in GridView" issue?

 

 

End

Blog is becoming increasingly obsolete, and it is still necessary to write frequently.


How does android dynamically set the length and width of the items in the GridView according to the width of the GridView?

Item control is in the adapter. You only need to input the Gridview length and width as parameters when the length and width of your Gridview change, and design a small algorithm to control the length and width of the neutron view of the adapter, remember to call the adapter. the yydatasetchanged () function updates the UI.

Question about the items in the gridview developed by android

1. android: numColumns = "3" can control the number of items displayed in a girdview line. = "3" is displayed in one row.
2. Split the three items equally and fill the screen
1) The girdview width must be set to match_parent android: layout_width = "match_parent". Make sure that the width of the entire gridview is full of the screen width.
2) Make the layout width loaded in the adapter android: layout_width = "match_parent", and the gridview calculates the width by itself.
3) Imageview is centered in the adapter view layout, and its width is also set to android: layout_width = "match_parent ".
4) To prevent images from being smaller than items, you can set android: scaleType = "fitXY" of Imageview to automatically stretch the image. Set src for Imageview to display the image size, while background to display the image size. This can be based on your needs

If you only want to display the image, you can. If a complex item only modifies the adapterView layout

Related Article

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.