Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"addIgnoredView()" is useless #751

Open
fuchaosz opened this issue May 23, 2016 · 2 comments
Open

"addIgnoredView()" is useless #751

fuchaosz opened this issue May 23, 2016 · 2 comments

Comments

@fuchaosz
Copy link

fuchaosz commented May 23, 2016

In English:
I found the method "addIgnoredView()" is useless, when a view in ViewGroup. for example:

<LinearLayout
        android:id="@+id/ly_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </android.support.v4.view.ViewPager>

    </LinearLayout>

slidingMenu.addIgnoredView(viewpager);
now it's useless and the viewpager can neither get the ACTION_DOWN,ACTION_MOVE,ACTION_UP nor "setOnPagerChangeListener()" , because of this(in CustomViewAbove.java):

private boolean isInIgnoredView(MotionEvent ev) {
        Rect rect = new Rect();
        for (View v : mIgnoredViews) {
            **v.getHitRect(rect);**
            if (rect.contains((int)ev.getX(), (int)ev.getY())) return true;
        }
        return false;
    }

v.getHitRect(rect). this method only get the location of v releated to it's parent, but the MotionEvent who's location is the whole screen,so rect.contains((int)ev.getX(), (int)ev.getY()) always return false, even if your touch point is in the ignored view.
How to fix it? look at this:

    private boolean isInIgnoredView(MotionEvent ev) {
        Rect rect = new Rect();
        for (View v : mIgnoredViews) {
            //v.getHitRect(rect);
            //这里稍微做了修改,v.getHitRect只能获取相对于父控件的位置,如果v嵌套在一个viewGroup中,
            // 那么添加这个v到mIgnoredViews中将不会游任何效果,例如:LinearLayout嵌套一个ViewerPager,将ViewPager加入mIgnoredViews后,依然无法接受到滑动事件
            //所以这里改为v.getGlobalVisibleRect,获取v在整个屏幕的坐标,而MotionEvent的坐标也是相对于整个屏幕来测量的
            v.getGlobalVisibleRect(rect);
            if (rect.contains((int)ev.getX(), (int)ev.getY())) return true;
        }
        return false;
    }

use v.getGlobalVisibleRect(rect); inplace of `v.getHitRect(rect);

for more detail,please see my csdn blog: http://blog.csdn.net/fuchaosz/article/details/51513288

中文:
当一个子view嵌套在ViewGroup中,将这个子View加入SlidingMenu的ignoredViews中,即:slidingMenu.addIgnoredView(v),是不起作用的,例如:一个LinearLayout中包含一个ViewerPager,此时ViewPager的滑动事件会和SlidingMenu冲突,这时ViewPager将不能滑动,怎么办呢?可以调用slidingMenu.addIgnoredView(viewPager),然而这是无效的,原因在这里(在CustomViewAbove.java):

private boolean isInIgnoredView(MotionEvent ev) {
        Rect rect = new Rect();
        for (View v : mIgnoredViews) {
            **v.getHitRect(rect);**
            if (rect.contains((int)ev.getX(), (int)ev.getY())) return true;
        }
        return false;
    }

v.getHitRect(rect)取的的坐标是相对于父view的,但是MotionEvent的坐标是整个屏幕的,所以会导致rect.contains((int)ev.getX(), (int)ev.getY())始终返回false,即使你点击的区域是viewPager的区域
怎么修复?看下面的方法:

    private boolean isInIgnoredView(MotionEvent ev) {
        Rect rect = new Rect();
        for (View v : mIgnoredViews) {
            //v.getHitRect(rect);
            //这里稍微做了修改,v.getHitRect只能获取相对于父控件的位置,如果v嵌套在一个viewGroup中,
            // 那么添加这个v到mIgnoredViews中将不会游任何效果,例如:LinearLayout嵌套一个ViewerPager,将ViewPager加入mIgnoredViews后,依然无法接受到滑动事件
            //所以这里改为v.getGlobalVisibleRect,获取v在整个屏幕的坐标,而MotionEvent的坐标也是相对于整个屏幕来测量的
            v.getGlobalVisibleRect(rect);
            if (rect.contains((int)ev.getX(), (int)ev.getY())) return true;
        }
        return false;
    }

v.getGlobalVisibleRect(rect); 代替 v.getHitRect(rect);

更多详细信息,请看我的csdn博客:http://blog.csdn.net/fuchaosz/article/details/51513288

@theBlbDan
Copy link

What specific problem are you experiencing with a ViewPager? I have a ViewPager inside a RelativeLayout and have not had any problems with setOnPagerChangeListener().

<?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" >

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        android:background="@drawable/background_tabs" />

    <android.support.v4.view.ViewPager
        android:id="@+id/quicknav_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tabs" />
</RelativeLayout>

@fuchaosz
Copy link
Author

@theBlbDan I'm not mean setOnPagerChangeListener(), I mean 'addIgnoreView()' is useless in somehow, for more detail, please see my csdn bolg here:
http://blog.csdn.net/fuchaosz/article/details/51513288

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants