码迷,mamicode.com
首页 > 移动开发 > 详细

android 中 listview 设置自动匹配高度

时间:2016-04-24 16:59:38      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:

1.布局文件

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ListView
            android:id="@+id/lv0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ff0"
            android:dividerHeight="5dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#0f0"
            android:gravity="bottom"/>

        <ListView
            android:id="@+id/lv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#f0f"
            android:dividerHeight="5dp"/>

    </LinearLayout>
</ScrollView>

2. java 代码

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


/**
 * 设置ListView的自动高度
 * 3种方式:
 * 1.onWindowFocusChanged() 中设置
 * 2.onResume() 中设置
 * 3.onCreate() 中view.post()方法中设置
 */
public class ListViewAutoHeight extends Activity {

    private static final String TAG = "autoHeight";
    private ListView lv0;
    private ListView lv1;

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
//        setListViewAutoHeight(lv0);
//        setListViewAutoHeight(lv1);
        Log.e(TAG, "onWindowFocusChanged ");
    }

    @Override
    protected void onResume() {
        super.onResume();
//        setListViewAutoHeight(lv0);
//        setListViewAutoHeight(lv1);
        Log.e(TAG, "onResume ");
    }

    @Override
    protected void onCreate(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 = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            lv0Datas.add("item_0_" + i);
        }
        ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv0Datas);
        lv0.setAdapter(adapter0);


        lv1 = (ListView) findViewById(R.id.lv1);
        List<String> lv1Datas = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            lv1Datas.add("item_1_" + i);
        }
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv1Datas);
        lv1.setAdapter(adapter1);

        // 随便在哪个控件身上的post 方法中调用设置自动高度的方法即可
        // post 是将该方法添加到Loop队列的末尾,如果直接调用是不起作用的
        lv1.post(new Runnable() {
            @Override
            public void run() {
                setListViewAutoHeight(lv0);
                setListViewAutoHeight(lv1);

                Log.e(TAG, "post ");
            }
        });
    }

    /** 计算高度 */
    private void setListViewAutoHeight(ListView lv) {
        if (lv == null || lv.getCount() <= 0) return;
        int totalHeight = 0;
        for (int i = 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);
    }

}

 

android 中 listview 设置自动匹配高度

标签:

原文地址:http://www.cnblogs.com/Westfalen/p/5427402.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!