Android listview viewpager解决冲突 滑动
Android listview viewpager滑动 跳动 冲突解决
ListView中嵌套ViewPage有或者滑动手势冲突解决
在listview 上使用 addHeaderView 在第一栏添加 viewpager 当做header
如:
当触发 滑动事件 的时候容易引起 滑动冲突 (比如斜着滑动viewpager 的时候 listview会跳动)
特别是在 下拉刷新或者上拉加载 的时候 , 组件可能会传递到viewpager当中
查阅了很多的帖子 发现修改起来都非常麻烦
(1)解决方案
1. 针对viewpager 做了些修改
替换掉support.v4当中的viewpager即可:
- package com.example.bz_viewpager;
- import android.content.Context;
- import android.support.v4.view.ViewPager;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.view.ViewGroup;
- /**
- * viewpage 和listview 相互冲突 将父view 传递到viewpage 里面
- *
- * 使用父类的方法 parent.requestDisallowInterceptTouchEvent(true);
- *
- * 当 requestDisallowInterceptTouchEvent 如果为true的时候 表示:父view 不拦截子view的touch 事件
- *
- * 这个方法只是改变flag
- *
- * @author baozi
- *
- */
- public class DecoratorViewPager extends ViewPager {
- private ViewGroup parent;
- public DecoratorViewPager(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
- public DecoratorViewPager(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public void setNestedpParent(ViewGroup parent) {
- this.parent = parent;
- }
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev) {
- if (parent != null) {
- parent.requestDisallowInterceptTouchEvent(true);
- }
- return super.dispatchTouchEvent(ev);
- }
- @Override
- public boolean onInterceptTouchEvent(MotionEvent arg0) {
- if (parent != null) {
- parent.requestDisallowInterceptTouchEvent(true);
- }
- return super.onInterceptTouchEvent(arg0);
- }
- @Override
- public boolean onTouchEvent(MotionEvent arg0) {
- if (parent != null) {
- parent.requestDisallowInterceptTouchEvent(true);
- }
- return super.onTouchEvent(arg0);
- }
- }
2 . 在xml里面:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#f1f1f1" >
- <com.example.bz_viewpager.DecoratorViewPager
- android:id="@+id/vp"
- android:layout_width="match_parent"
- android:layout_height="200dp"
- android:fadingEdge="none" />
- </RelativeLayout>
3. 在代码里使用
将 viewpager 的父view传递到viewpager里面
调用: vp.setNestedpParent((ViewGroup)vp.getParent()); 方法
如下:
- lv = (ListView) findViewById(R.id.lv);
- View header = LayoutInflater.from(MainActivity.this).inflate(R.layout.viewpage_layout, null);
- DecoratorViewPager vp = (DecoratorViewPager) header.findViewById(R.id.vp);
- vp.setNestedpParent((ViewGroup)vp.getParent());
- MyPagapter myPagapter = new MyPagapter(MainActivity.this);
- vp.setAdapter(myPagapter);
- lv.addHeaderView(header);
(2)解析:
viewgroup 当中有 一个 requestDisallowInterceptTouchEvent方法
这个方法只改变flag 当 view.requestDisallowInterceptTouchEvent 参数为true的时候
view 不会拦截其子控件的 触摸事件
- /**
- * Called when a child does not want this parent and its ancestors to
- * intercept touch events with
- * {@link ViewGroup#onInterceptTouchEvent(MotionEvent)}.
- *
- * <p>This parent should pass this call onto its parents. This parent must obey
- * this request for the duration of the touch (that is, only clear the flag
- * after this parent has received an up or a cancel.</p>
- *
- * @param disallowIntercept True if the child does not want the parent to
- * intercept touch events.
- */
- public void requestDisallowInterceptTouchEvent(boolean disallowIntercept);
贴上源码:
- public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
- if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
- // We're already in this state, assume our ancestors are too
- return;
- }
- if (disallowIntercept) {
- mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
- } else {
- mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
- }
- // Pass it up to our parent
- if (mParent != null) {
- mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
- }
- }
Android listview viewpager解决冲突 滑动的更多相关文章
- Android ListView焦点事件冲突问题与解决
Android ListView对于单纯列表展示是很好用的,但是一旦牵扯到对listView进行操作就会遇到各种各样的问题.比如保存Checkbox状态与item复用的冲突.遇到可获取焦点的控件比如说 ...
- 114、Android禁止ViewPager的左右滑动
有时候在开发中会遇到一些“诡异”的要求,比如在ViewPager中嵌入ListView,或者再嵌入一个ViewPager,那么在滑动的时候就会造成被嵌入的XXView不能滑动了,那么现在就把最外层的V ...
- Android禁止ViewPager的左右滑动
转载请注明出处:http://blog.csdn.net/allen315410/article/details/40744287 有时候在开发中会遇到一些"诡异"的要求,比方在V ...
- Android ListView 和 ScrollView 冲突问题
近期做一款APP,当中有一个类似微博的评论功能的界面,先是列出微博的正文内容和图片等.然后下边是评论. 一開始就想着用一个ScrollView把主要内容和评论区的ListView包起来.然后加入各个控 ...
- [Android]ListView & ViewPager & GridView 常见问题解决方法
1. ViewPager左右滚动到两边时的渐变色,若想禁止,可以在布局文件中配置:android:overScrollMode="never" 2. ListView,GridVi ...
- [Android]使用ViewPager实现图片滑动展示
在淘宝等电商的APP首页经常能看到大幅的广告位,通常有多幅经常更新的图片用于展示促销信息,如下图所示: 通常会自动滚动,也可以根据手势滑动.我没有研究过人家的APP是通过什么实现的,可能有第三方已经封 ...
- 011 Android TabLayout+ViewPager实现顶部滑动效果(多个页面)
1.TabLayout介绍 TabLayout提供了一个水平的布局用来展示Tabs,很多应用都有这样的设计,典型的有网易新闻,简书,知乎等.TabLayout就可以很好的完成这一职责,首先TabLay ...
- Android使用ViewFlipper实现左右滑动效果面
在我的博客中,上次是使用ViewPager实现左右滑动的效果的,请看文章:Android使用ViewPager实现左右滑动效果. 这次我来使用ViewFlipper实现这种效果,好了,先看看效果吧: ...
- android 解决ListView点击与滑动事件冲突
如果你的ListView的Item有滑动功能,但又点击Item跳转到其它activity,这样若是在Adapter里面写点击事件是会导致滑动事件获取不到焦点而失效: 解决方法:不要在adapter里面 ...
随机推荐
- C中存储类、链接和内存管理
在C语言中,有5种不同的存储类型,即存储类.在介绍存储类之前,需要首先简单介绍几个术语.如下: 作用域:描述程序中可以访问一个标识符的一个或多个区域.一个C变量的作用域可以是代码块作用域.函数原型作用 ...
- star
Astronomers often examine star maps where stars are represented by points on a plane and each star h ...
- soupUI生成webservice客户端代码
在Apache网站下载axis2软件包,同时本机安装soapUI工具.例如,当前我使用的axis2版本为axis2-1.4-bin.zip,soapUI版本为3.6. Apache Axis下载地址: ...
- Amazon Launches FBA Export to Expand Beyond Media Categories
Amazon launched a new program called FBA Export for third-party sellers to help them export products ...
- iOS- 自定义UIView (测试block和代理)
#import <UIKit/UIKit.h> typedef void(^compeletionHandler) (NSInteger selectButtonIndex); @clas ...
- 【鬼脸原创】github搭建动态网站
a{ color:blue; font-weight:bold; } #cnblogs_post_body ol li { list-style-type: cjk-ideographic; } p[ ...
- nodejs 实现 http proxy 透明转发
这两天需要安装一个Linux虚拟机,接下来要通过 yum 来安装需要的软件. 因单位的网络需要通过代理才能访问外网,公司代理服务器:proxy.xxxx.com,给yum增加了proxy设置: # v ...
- Git 暂存区
可以用 git log 查看提交日志(附加的 --stat 参数可以看到每次提交的文件变更统计). $ cd /path/to/my/workspace/demo $ git log --stat 如 ...
- codeforces 675A A. Infinite Sequence(水题)
题目链接: A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input st ...
- Python之随机数
import numpy as np list = range(3) # 0 1 2 np.random.shuffle(list)#2 1 3,打乱了list import randomprint ...