如题,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)的详细用法会在后面的博文中给出说明。