ScrollView嵌套ListView

//如下布局

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

    <Button 
            android:id="@+id/change"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="改变listview的高"
            />
        <Button 
            android:id="@+id/change"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="改变listview的高"
            />
        <Button 
            android:id="@+id/change"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="改变listview的高"
            />
        <Button 
            android:id="@+id/change"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="改变listview的高"
            />
        <Button 
            android:id="@+id/change"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="改变listview的高"
            />
        <Button 
            android:id="@+id/change"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="改变listview的高"
            />
        <Button 
            android:id="@+id/change"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="改变listview的高"
            />
        <Button 
            android:id="@+id/change"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="改变listview的高"
            />
        <Button 
            android:id="@+id/change"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="改变listview的高"
            />
        <Button 
            android:id="@+id/change"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="改变listview的高"
            />
    </LinearLayout>

</ScrollView>

//如何解决ScrollView中ListView的滑动和ListView下面的布局(button)按钮的滑动
代码如下:
在onCreate()里调用如下两个方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
setLvHeigthBasedOnChildren(lv);

}


    private void init() {
        ScrollView  scroll = (ScrollView) findViewById(R.id.scroll);
        // scroll的滑动监听
        scroll.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                    break;
                case MotionEvent.ACTION_MOVE:
                    // Scrollview划出的高度
                    int scrollY = v.getScrollY();
                    // Scrollview可见的高度
                    int height = v.getHeight();
                    int scrollViewNeasuredHeight = scroll.getChildAt(0)
                            .getMeasuredHeight();
                    if (scrollY == 0) {// 第一条
                        scroll.requestDisallowInterceptTouchEvent(true);
                    }
                    if ((scrollY + height) == scrollViewNeasuredHeight) {
                        System.out.println("滑动到了底部 scrollY=" + scrollY);
                        System.out.println("滑动到了底部 height=" + height);
                        System.out.println("滑动到了底部 scrollViewMeasuredHeight="
                                + scrollViewNeasuredHeight);
                    }

                    break;

                default:
                    break;
                }
                return false;
            }
        });
        lv = (ListView) findViewById(R.id.lv);
        change = (Button) findViewById(R.id.change);
        // 配置数据
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, books);
        lv.setAdapter(adapter);
        // Lv的滑动事件
        lv.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == 2) {
                    scroll.requestDisallowInterceptTouchEvent(true);
                } else {
                    scroll.requestDisallowInterceptTouchEvent(false);
                }
                return false;
            }
        });
        // Lv的滑动监听
        lv.setOnScrollListener(new OnScrollListener() {
            private boolean islast;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // listview第一行的索引+他的高度能显示的条数,如果结果是listview的最后一条的索引,
                int i = firstVisibleItem + visibleItemCount;
                islast = i == totalItemCount;// 为true表示说明listview滚动到最后一条也显示了
                if (islast) {
                    // 可以让父亲(scrollview)截断touch事件
                    scroll.requestDisallowInterceptTouchEvent(false);
                }

            }
        });

    }

根据listview的数据来计算他的应得的高度,设置高度

public void setLvHeigthBasedOnChildren(ListView listview) {
        ArrayAdapter listAdapter = (ArrayAdapter) listview.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listview);
            if (listItem != null) {
                listItem.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                listItem.measure(MeasureSpec.UNSPECIFIED,
                        MeasureSpec.UNSPECIFIED);
                totalHeight += listItem.getMeasuredHeight();
            }
        }
        ViewGroup.LayoutParams params = listview.getLayoutParams();
        params.height = totalHeight
                + (listview.getDividerHeight() * (listAdapter.getCount() - 1))
                + listview.getPaddingTop() + listview.getPaddingBottom();
        int h = getWindowManager().getDefaultDisplay().getHeight();
        if (params.height > h / 2) {
            params.height = h / 2;
        }
        listview.setLayoutParams(params);

    }

//通过分发事件解决ScrollView嵌套Listview加其控件的滑动

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值