Auto Match height in ListView setting in Android

Source: Internet
Author: User

1. Layout files

<ScrollViewxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical">        <ListViewAndroid:id= "@+id/lv0"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "#ff0"Android:dividerheight= "5DP"/>        <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "50DP"Android:background= "#0f0"android:gravity= "Bottom"/>        <ListViewAndroid:id= "@+id/lv1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "#f0f"Android:dividerheight= "5DP"/>    </LinearLayout></ScrollView>

2. Java code

Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.view.Window;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView;Importjava.util.ArrayList;Importjava.util.List;/*** Set Automatic height of ListView * 3 ways: * 1.onWindowFocusChanged () set in * 2.onResume () set in * 3.onCreate () in View.post () method*/ Public classListviewautoheightextendsActivity {Private Static FinalString TAG = "Autoheight"; PrivateListView lv0; PrivateListView Lv1; @Override Public voidOnwindowfocuschanged (BooleanHasfocus) {        Super. onwindowfocuschanged (hasfocus);//setlistviewautoheight (lv0);//setlistviewautoheight (LV1);LOG.E (TAG, "onwindowfocuschanged"); } @Overrideprotected voidOnresume () {Super. Onresume ();//setlistviewautoheight (lv0);//setlistviewautoheight (LV1);LOG.E (TAG, "Onresume"); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.activity_listview_auto_height); Lv0=(ListView) Findviewbyid (r.id.lv0); List<String> Lv0datas =NewArraylist<>();  for(inti = 0; I < 10; i++) {Lv0datas.add ("Item_0_" +i); } arrayadapter<String> Adapter0 =NewArrayadapter<string> ( This, Android.        R.layout.simple_list_item_1, Lv0datas);        Lv0.setadapter (ADAPTER0); Lv1=(ListView) Findviewbyid (R.ID.LV1); List<String> Lv1datas =NewArraylist<>();  for(inti = 0; I < 20; i++) {Lv1datas.add ("Item_1_" +i); } arrayadapter<String> Adapter1 =NewArrayadapter<string> ( This, Android.        R.layout.simple_list_item_1, Lv1datas);        Lv1.setadapter (Adapter1); //The method of setting automatic height in the Post method of any control//Post is to add the method to the end of the loop queue if the direct call is not workingLv1.post (NewRunnable () {@Override Public voidrun () {setlistviewautoheight (lv0);                Setlistviewautoheight (LV1); LOG.E (TAG,"POST");    }        }); }    /**Calculate Height*/    Private voidsetlistviewautoheight (ListView LV) {if(LV = =NULL|| Lv.getcount () <= 0)return; intTotalheight = 0;  for(inti = 0; I < Lv.getcount (); i++) {View View= Lv.getadapter (). GetView (I,NULL, LV); View.measure (0, 0); Totalheight+=view.getmeasuredheight (); } viewgroup.layoutparams params=Lv.getlayoutparams (); Params.height= Totalheight + lv.getdividerheight () * (Lv.getcount ()-1) + lv.getpaddingtop () +Lv.getpaddingbottom ();    Lv.setlayoutparams (params); }}

Auto Match height in ListView setting in Android

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.