利用事件分发机制解决ScrollView嵌套ListView滑动冲突
来源:互联网 发布:2k18艾佛森捏脸数据 编辑:程序博客网 时间:2024/06/04 18:26
记得以前面试的时候,面试官问了ScrollView嵌套ListView使用的问题。那么ScrollView嵌套ListView使用会出现什么效果呢?
如布局文件如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" 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="com.wzc.example.administrator.demo.ThirdActivity"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="false"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:src="@mipmap/ic_launcher" /> <ListView android:id="@+id/list_view2" android:background="#00ffff" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:src="@mipmap/ic_launcher" /> </LinearLayout> </ScrollView></RelativeLayout>
效果如下:
ListView只会显示一行,且ListView不能滑动。
那么怎么样才能实现ListView和ScrollView滑动互不影响呢?
其实方法有很多种,我这里讲的是利用事件分发机制实现。
第一种方法:自定义父层View(这里是ScrollView)并重写onInterceptTouchEvent()方法
public class MyScrollView extends ScrollView { public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } //不拦截,继续分发下去 @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return false; }}
将MyScrollView替代布局中的ScrollView,并设置ListView的宽高。
<com.wzc.example.administrator.demo.view.MyScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="false"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:src="@mipmap/ic_launcher" /> <ListView android:id="@+id/list_view2" android:layout_width="wrap_content" android:layout_height="200dp" android:background="#00ffff" /> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:src="@mipmap/ic_launcher" /> </LinearLayout> </com.wzc.example.administrator.demo.view.MyScrollView>
运行结果如下:
第二种方法: 自定义子View(这里是ListView)并重写dispatchTouchEvent()方法,通知通知父层ViewGroup不截获。
代码如下:
@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) { //通知父层ViewGroup不截获 getParent().requestDisallowInterceptTouchEvent(true); return super.dispatchTouchEvent(ev); }
运行效果如下:
第三种方法: 设置setOnTouchListener监听
代码如下:
//当用户按下的时候,我们告诉父组件,不要拦截我的事件(这个时候子组件是可以正常响应事件的),拿起之后就会告诉父组件可以阻止。 mListView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: v.getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: v.getParent().requestDisallowInterceptTouchEvent(false); break; } return false; } }); }
运行效果如下:
建议使用第一种方法,在父层View中控制事件分发。
点击源码下载
2 0
- 利用事件分发机制解决ScrollView嵌套ListView滑动冲突
- Scrollview嵌套listview利用事件分发机制解决滑动冲突
- 通过事件分发机制处理ListView与ScrollView滑动冲突
- scrollView里面嵌套listview,导致滑动事件冲突解决。
- ScrollView嵌套ListView滑动冲突解决
- 解决ScrollView嵌套ListView滑动冲突
- Scrollview嵌套listview解决滑动冲突
- Android中自定义ListView,解决scrollview嵌套listview 滑动事件冲突
- Android事件分发机制、滑动冲突解决
- Android 事件分发实践(一),解决ScrollView嵌套ListView滑动的问题
- 防止viewpager和子view滑动冲突(ScrollView嵌套webview,Listview嵌套Listview 滑动事件冲突)
- 事件分发机制-ScrollView嵌套ListView问题产生原理以及常见解决方式
- scrollview嵌套listview 滑动事件冲突的解决方法
- Android scrollview嵌套listview 滑动事件冲突的解决方法
- 完美解决ScrollView嵌套ListView滑动冲突(有图有真相)
- 完美解决ScrollView嵌套ListView滑动冲突(有图有真相)
- Android解决ScrollView嵌套ListView滑动冲突问题
- android ListView/GridView与ScrollView嵌套的滑动冲突解决
- 编译原理-词法分析器-用高级语言实现词法分析器功能
- qtcretor中常用快捷键集合
- pl/sql NULL 语句
- HTML5之canvas4
- 装箱子
- 利用事件分发机制解决ScrollView嵌套ListView滑动冲突
- Oracle 查询优化的基本准则详解
- Eclipse集成Tomcat 报错Tomcat 7.x JDK name
- 优先级队列:PriorityQueue
- c语言之IO使用文件分割
- HTML5之canvas5
- 针对线程学习过程中遇到的问题或者是学习经验笔记
- 原创XListView
- SDOS_SdoInfoInd