您的位置:首页 > 移动开发 > Android开发

Android ScrollView和ListView事件冲突问题解决方法

2016-07-27 12:02 627 查看
不知道有没有朋友遇到这种问题:就是当你一个ScrollView里面包含一个ListView的时候,你会发现一个问题:当listview里面的item条目过多的时候,在Listview中只会显示一部分,当你滑动该listview确滑动的是ScrollView。从而导致listview中显示的N多数据显示不出来。以下是解决办法:

layout文件代码:

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

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

<ListView
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="@+id/listView"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/breagund"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/breagund"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/breagund"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/breagund"/>
</LinearLayout>
</ScrollView>


MainActivity代码:

public class MainActivity extends Activity {

private ScrollView scrollView;
private ListView listView;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) this.findViewById(R.id.listView1);
scrollView =(ScrollView) this.findViewById(R.id.scrollView1);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,getItem());
listView.setAdapter(adapter);
}

public List<String> getItem(){
List<String> list = new ArrayList<String>();
for (int i=0;i<30;i++){
list.add("test"+i);
}
return list;
}

//拦截事件,进行事件分发。从而指定到需要分发的对象中(解决2者的冲突事件)
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
//如果
if (event.getAction()==MotionEvent.ACTION_MOVE){
//将事件分发到listview中
listView.dispatchTouchEvent(event);
}
return super.dispatchTouchEvent(event);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android listview 布局