Scrollview 里 嵌套listview

想给listview的上方加一个header,我们可以 在listview上方增加布局来实现 如:
<?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"
    android:orientation="vertical"
    tools:context="com.example.administrator.testlistviewscrollview.MainActivity">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="15dip"
        android:text="-&#45;&#45;&#45;&#45;这里是标题&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;"
        android:textColor="@android:color/holo_red_dark" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:divider="@android:color/holo_blue_bright"
        android:dividerHeight="5dip"/>
</LinearLayout>

但是,需要注意的是,这中做法,listview的header是固定在listview上的,不能随着listview的滑动而滑动。
如果,我们的需求是:header随着listview的滑动而滑动呢?

这个时候,我们或许就会用到ScrollView,在ScrollView里嵌套一个listview来实现我们的需求
如”
xml.
<?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"
    android:orientation="vertical"
    tools:context="com.example.administrator.testlistviewscrollview.MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"><!--这里为什么又嵌套了一层布局呢?不说了。如果不知道,自己去掉这层view再运行看看-->

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="15dip"
                android:text="-&#45;&#45;&#45;&#45;这里是标题&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;"
                android:textColor="@android:color/holo_red_dark" />

            <ListView
                android:id="@+id/listview"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:divider="@android:color/holo_blue_bright"
                android:dividerHeight="5dip" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>


java类:
public class MainActivity extends Activity {

    private ListView listView;
    private TestAdapter adapter;
    private List<String> data;
    int n = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listview);
        data = getData();
        adapter = new TestAdapter(this, data);
        listView.setAdapter(adapter);

        setListener();
    }

    private List<String> getData() {
        List<String> data = new ArrayList<String>();
        for (int i = 2; i < 20; i++) {
            data.add("第" + n + "页  测试数据" + i);
        }
        n++;  // 用来模拟分页加载
        return data;
    }

}

这样的话产生了一个问题:你在屏幕上看到的listview只显示一个条目,如何解决这个问题呢?
1.xml中 写死listview的高度。经过测设,这种方法也具有一定的可行性,但是它还有个弊端,就是listview的数据是可变动的,除非你能正确的写出listview的高度。
2. 在代码中动态计算listview的高度:在给listview设置完adapter之后,再动态的设置listview的高度。

在  listView.setAdapter(adapter);之后调用  Util.setListViewHeight(listView);
setListViewHeight:
    public static void setListViewHeight(ListView listview) {
        ListAdapter listAdapter = listview.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listview);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        int aa = listAdapter.getCount();
        ViewGroup.LayoutParams params = listview.getLayoutParams();
        params.height = totalHeight + (listview.getDividerHeight() * (aa - 1));
        // ((MarginLayoutParams) params).setMargins(0,0,0, 0);
        listview.setLayoutParams(params);
    }

运行后发现,虽然目前是打到我们的需求了,但是还存在几个问题:
1》在刚打开页面的是时候,界面自动定位到listview的第0条目,listview的header是看不到的

2》分页加载加载第二页的时候,listview的高度不更新

时间紧,这俩个问题后续-------


另外 提醒大家注意一下:之前我看到网上好多帖子说 用 动态设置listview的高度这种方法时,要求adapter的xml布局里必须要有LinearLayout,还说出了原因。之前我就年轻的相信了,。后来我写代码验证了一下,这种说法是不正确的,我用的是相对布局,也是可以的。也许,网上的那种说法只是在某些特定的情况下才成立。

所以,提醒大家,不要轻易相信别人:),一定要自己验证



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值