android 中 listview 设置自动匹配高度

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

}

 

posted @ 2016-04-24 16:45  MarcoReus  阅读(723)  评论(0编辑  收藏  举报