Android 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” />

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)}.

  • 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

  • 18
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值