scrollview嵌套listiview(解决高度问题以及两者滚动冲突问题)

1、scrollview由于只能有一个子布局,因此scrollview嵌套listview,必须得用LinearLayout来包裹listview,然后scrollview再包含线性布局。

xml布局文件

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <ScrollView
        android:id="@+id/sc"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/line"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <com.bwie.view.MyListView
                android:id="@+id/lv"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </com.bwie.view.MyListView>

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

2、mainactivity代码

package com.bwie.scrollview_listview;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;

public class MainActivity extends Activity implements OnScrollListener {

    private ListView lv;
    private String[] name = new String[] { "A", "S", "D", "F", "G", "H", "J",
            "H", "J", "K"};
    private boolean isLoad=false;
    private ScrollView sc;
     

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 找到该布局文件下的控件
        lv = (ListView) findViewById(R.id.lv);
        sc = (ScrollView) findViewById(R.id.sc);
        

        // 为lv设置适配器
        lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_expandable_list_item_1, name));

        // 重新定义listiview的高度(自己计算listview的高度)
        setListViewHeightBasedOnChildren(lv);
        
        //为lv设置滚动监听事件,如果listview滚动到最后一条时,就请求父控件拦截touch事件,父控件进行滚动
        lv.setOnScrollListener(this);

    }

    private void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            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();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
        if(scrollState  == SCROLL_STATE_TOUCH_SCROLL && isLoad)
        {
            //请求父亲拦截touch事件
            sc.requestDisallowInterceptTouchEvent(false);
        }
        
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        
        //判断是否加载到最底部
        isLoad = (firstVisibleItem + visibleItemCount) == totalItemCount;
        
    }
    
 
}

3、重写listview控件,请求父控件(scrollview控件)不要拦截touch事件

package com.bwie.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import android.widget.ListView;

public class MyListView extends ListView {

 

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    
    

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        // 请求父亲不要拦截touch事件
        getParent().requestDisallowInterceptTouchEvent(true);

        return super.dispatchTouchEvent(ev);
    }

}

 

posted on 2016-04-17 20:31  IT心得  阅读(644)  评论(0编辑  收藏  举报