Set Row Height for the gridview

Source: Internet
Author: User

Recently, I encountered such a problem in my work. Using a gridview to display data, the item is only a textview, but the number of text displayed in it is not fixed and must all be displayed.

Problems:

1. Set the width and height of the item to match_parent or wrap_content. When there are too many contents, the content displayed in the next row will be overwritten.

2. No attribute can set row height for the gridview, so the height cannot be controlled.

Problems encountered:. Expectation

The solution is:

Design Concept: 1. first obtain the height of textview
2. Store the height in the global variable, compare it with the original one, and store the big one in the global variable.
3. Set the height in the global variable to textview.


Now that I understand the idea, I will start to understand the code.

First, create an activity_main.xml file.

<span style="font-family:SimSun;font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <GridView        android:id="@+id/mygridview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:horizontalSpacing="10dp"        android:verticalSpacing="10dp" /></RelativeLayout></span>


Call in mainactivity

<Span style = "font-family: simsun; font-size: 18px;"> package COM. example. gridviewdemo; import Java. util. arraylist; import Java. util. list; import android. app. activity; import android. OS. bundle; import android. widget. gridview; public class mainactivity extends activity {private gridview mygridview; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); initview (); initdata ();} private void initview () {mygridview = (gridview) findviewbyid (R. id. mygridview); mygridview. setnumcolumns (constant. grivew_column_nums);}/***** @ Method Name: initdata * @ Description: Todo * @ created by: yzk * @ Creation Time: 2:17:28, January 1, October 15, 2014 * @ remarks: initialize data * @ return type: void */private void initdata () {list <string> List = new arraylist <string> (); list. add ("news"); list. add ("Sports"); list. add ("Entertainment"); list. add ("I love adventure"); list. add ("123"); list. add ("I love cycling"); list. add ("Believe in yourself, everything is difficult at the beginning, and I am the master of my website"); list. add ("cherish life, everyone is responsible"); list. add ("Come Baby"); list. add ("first snow in 2002"); list. add ("a fire in winter"); mygridview. setadapter (New myadapter (mainactivity. this, list) ;}}</span>

The following is a global variable class.

Constant

<Span style = "font-family: simsun; font-size: 18px;"> package COM. example. gridviewdemo; public class constant {public static int griview_column_height = 0; // public static int grivew_column_nums = 4; // Number of columns} </span>

Create a griview Adapter

<Span style = "font-family: simsun; font-size: 18px;"> public class myadapter extends baseadapter {private context mcontext; private list <string> mdata; public myadapter (context mcontext, list <string> data) {super (); this. mcontext = mcontext; this. mdata = data ;}@ overridepublic int getcount () {If (mdata = NULL) {return 0;} return mdata. size () ;}@ overridepublic object getitem (INT position) {return mdata. get (position) ;}@ overridepublic long getitemid (INT position) {return position ;}@ overridepublic view getview (INT position, view convertview, viewgroup parent) {viewholder holder = NULL; if (convertview = NULL) {holder = new viewholder (); convertview = view. inflate (mcontext, R. layout. item_griview, null); holder. item = (textview) convertview. findviewbyid (R. id. ITEM); convertview. settag (holder);} else {holder = (viewholder) convertview. gettag ();} initkeytextview (holder. item, position); holder. item. settext (string) getitem (position); Return convertview;} class viewholder {textview item;}/***** @ Method Name: initkeytextview * @ description: todo * @ Creator: yzk * @ Creation Time: 11:58:22, January 1, October 15, 2014 * @ Note: Get the height of each textview and compare it, set the highest value to the height of textview * @ Param ll * @ Param position * @ return type: void * @ Note: 1, you need to store the height of textview In the retrieved item to the global variable so that it will function. * 2. make sure to set the height of textview in the addongloballayoutlistener listener. * do not remove the height. In getview, the height is invalid. * @ Design Philosophy: 1. first, obtain the height of textview * 2. store the height in the global variable, compare it with the original one, and store the large one in the global variable * 3. then, set the height in the global variable to textview */Public void initkeytextview (final view LL, final int position) {viewtreeobserver vto2 = ll. getviewtreeobserver (); vto2.addongloballayoutlistener (New ongloballayoutlistener () {@ overridepublic void ongloballayout () {ll. getviewtreeobserver (). removeglobalonlayoutlistener (this); If (position % constant. grivew_column_nums = 0) {constant. griview_column_height = 0;} If (LL. getheight ()> constant. griview_column_height) {constant. griview_column_height = ll. getheight ();} setheight (LL, constant. griview_column_height) ;}});} public void setheight (view LL, int height) {ll. setlayoutparams (New layoutparams (layoutparams. match_parent, height, gravity. center) ;}}</span>

Note:

1. You need to store the height of textview In the retrieved item to the global variable so that it will take effect.
2. You must set the textview height in the addongloballayoutlistener listener,
It is not allowed to retrieve the height. In getview, this height is invalid. (The retrieved value is 0. It may be modified using final in the source code. The specific reason is unclear, please give me some advice)

This method

You have used viewtreeobserver to dynamically obtain the height of a view. To learn more about other dynamic methods, click

Source code download: http://download.csdn.net/detail/forwardyzk/8041089

We discuss each other and make progress.

Set Row Height for the gridview

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.