ScrollView嵌套ListView只显示一行的解决方法

来源:互联网 发布:复方雄蛾强肾胶囊淘宝 编辑:程序博客网 时间:2024/06/10 11:11

第一种方法: 在ScrollView中嵌套ListView,无法正确的计算ListView的大小,故可以通过代码,根据当前的ListView的列表项计算列表的尺寸。实现代码如下:
1 layout

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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" >    <ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent" >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent" >            <ListView                android:id="@+id/lv"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:fadingEdge="vertical"                android:fadingEdgeLength="5dp" />        </LinearLayout>    </ScrollView></LinearLayout>

2 代码

package com.example.apple.listviewscrollview;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ListAdapter;import android.widget.ListView;import java.util.ArrayList;import java.util.List;public class MainActivity extends ActionBarActivity {    private ListView listView;    private ArrayList<String> list;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initComponent();        initData();        setAdapter();        setListViewHeightBasedOnChildren(listView);    }    private void setAdapter() {        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));    }    private void initData() {        list = new ArrayList<String>();        for (int i = 0; i < 20; i++) {            list.add("dilireba" + i);        }    }    private void initComponent() {        listView = ((ListView) findViewById(R.id.lv));    }    public void setListViewHeightBasedOnChildren(ListView listView) {        // 获取ListView对应的Adapter        ListAdapter listAdapter = listView.getAdapter();        if (listAdapter == null) {            return;        }        int totalHeight = 0;        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {            // listAdapter.getCount()返回数据项的数目            View listItem = listAdapter.getView(i, null, listView);            // 计算子项View 的宽高            listItem.measure(0, 0);            // 统计所有子项的总高度            totalHeight += listItem.getMeasuredHeight();        }        ViewGroup.LayoutParams params = listView.getLayoutParams();        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));        // listView.getDividerHeight()获取子项间分隔符占用的高度        // params.height最后得到整个ListView完整显示需要的高度        listView.setLayoutParams(params);    }}

疑问:
1布局,我在ScrollView中直接嵌套ListView,用上面的方法并没有解决问题,而是必须在ListView外加一层LinearLayout布局,这是为啥?

<ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent" >        <!--<LinearLayout-->            <!--android:layout_width="match_parent"-->            <!--android:layout_height="match_parent" >-->            <ListView                android:id="@+id/lv"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:fadingEdge="vertical"                android:fadingEdgeLength="5dp" />        <!--</LinearLayout>-->    </ScrollView>
0 0
原创粉丝点击