Android listview viewpager解決衝突 滑動,androidviewpager

來源:互聯網
上載者:User

Android listview viewpager解決衝突 滑動,androidviewpager
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;}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {if (parent != null) {parent.requestDisallowInterceptTouchEvent(true);}return super.dispatchTouchEvent(ev);}@Overridepublic boolean onInterceptTouchEvent(MotionEvent arg0) {if (parent != null) {parent.requestDisallowInterceptTouchEvent(true);}return super.onInterceptTouchEvent(arg0);}@Overridepublic 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);        }    }


Demo :

http://download.csdn.net/detail/aaawqqq/8447693

 

祝福大家每日精進

謝謝



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.