ScrollView嵌套ListView问题:如何让ListView随着ScrollView一起滚动

当Android的ScrollView嵌套ListView时,通常会导致滚动冲突。解决方法是确保ListView在ScrollView内完全显示,从而避免其独立滚动。通过动态设置ListView高度或者自定义ListView的onMeasure方法,可以实现只让ScrollView滚动的效果。本文提供了使用自定义onMeasure方法的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如题,Android滚动控件有个规律:一个父滚动控件嵌套了一个子滚动控件,当手指在两个控件重叠部分滑动时,会优先触发子控件的滚动属性。那么如何只触发父控件的滚动属性而不触发子控件的滚动属性?很简单,只要让整个ListView在父布局中显示出来就好了。假设父布局是一个不会滚动的控件(如RelativeLayout),当ListView在父布局中完全显示时(限制是这个ListView高度小于屏幕高度),ListView就不会滚动了吧。那么现在将RelativeLayout换成ScrollView,即便没有那个限制,只要ListView在ScrollView中完全显示(ListView高度大于屏幕高度也可以),就不会触发ListView的滚动属性。

现在问题就变成如何设置ListView的高度,使得ListView完全显示在ScrollView中。我们先将ListView的高度设置成wrap_content,看看ListView是否会自动调节高度,在ScrollView中完全显示:

MainActivity.java:

package com.example.noscrolllistviewtest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
private ListView list1;
private ListView list2;
private String[] data1 = {"itm1","item2","item3","item4","item5","item6","item7","item8"};
private String[] data2 = {"itm1","item2","item3","item4","item5","item6","item7","item8"};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		list1 = (ListView) findViewById(R.id.list1);
		list1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data1));
		list2 = (ListView) findViewById(R.id.list2);
		list2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data2));
	}

}

main.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:layout_width="match_parent"
        android:layout_height="match_parent" >

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

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

            <ListView
                android:id="@+id/list2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </ListView>
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

效果图:


每个ListView都有8个item,但是都只显示出了1个item,由于没有完全显示出来,滑动的时候只触发了ListView的滚动属性。这显然不是我们要的效果。

试过之后,我们发现无论是wrap_content还是match_parent属性都无法奏效,设置固定的值显然不是一种好办法。那么应该怎么办呢?这里有两种办法:1.根据数据源的size和每个item的高度,动态设置ListView的高度 2.自定义一个ListView,在onMeare()中对高度进行一定的设置

由于我们用的是系统默认的item,item的高度不好求,所以我们先用下第二种办法,上代码:

package com.xiaoteng.dms.component;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class NoScrollListView extends ListView {

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

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

	public NoScrollListView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSpec);
	}

}

main.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:layout_width="match_parent"
        android:layout_height="match_parent" >

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

            <com.example.noscrolllistviewtest.NoScrollListView
                android:id="@+id/list1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </com.example.noscrolllistviewtest.NoScrollListView>

            <com.example.noscrolllistviewtest.NoScrollListView
                android:id="@+id/list2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </com.example.noscrolllistviewtest.NoScrollListView>
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

效果图:


ok,问题解决!至于onMeasure(widthMeasureSpec, heightMeasureSpec);MeasureSpec.makeMeasureSpec(size, mode);View.getChildMeasureSpec(spec, padding, childDimension)的详细用法会在后面的博文中给出说明。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值