简体   繁体   中英

View Pager Android Filter Touch Events

I am using a view pager to create a picture gallery. I currently have a custom ImageView that allows a user to pinch and zoom and pan. The problem I have is how do I differentiate between a pinch to zoom/pan and a switching of a page in the view pager.

I tried to use onInterceptTouchEvent for the view pager, but this method only captures a single point touch event of action down. So it is impossible to differentiate a down touch with two fingers going down for a pinch to zoom.

I tried implementing a touch listener on the View Pager, but get a series of errors. Below is the code followed by the errors.

viewPager.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d("Action", "Action-Touch List "+Integer.toString(getAction(event.getAction())));
        int action = getAction(event.getAction());
        View vv =(View) viewPager.getChildAt(0);
        ImageZoomView izv= (ImageZoomView) vv.findViewById(R.id.ViewImagePreview);


        if (Float.compare(izv.getZoomState().getPanX(), 0.5f) == 0
                && Float.compare(izv.getZoomState().getPanY(), 0.5f) == 0
                && Float.compare(izv.getZoomState().getZoom(), 1.0f) == 0
                && action != ACTION_MULTITOUCH_DOWN
                && action != ACTION_MULTITOUCH_UP) {

            **Error Here**viewPager.onTouchEvent(event);
            return true;
        } else {

            izv.onTouchEvent(event);
            return true;

        }

    }
});

getaction works correctly for multiple touches, etc.

04-28 12:03:57.365: E/AndroidRuntime(6662): FATAL EXCEPTION: main
04-28 12:03:57.365: E/AndroidRuntime(6662): java.lang.ArrayIndexOutOfBoundsException
04-28 12:03:57.365: E/AndroidRuntime(6662):     at android.view.MotionEvent.getX(MotionEvent.java:907)
04-28 12:03:57.365: E/AndroidRuntime(6662):     at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)
04-28 12:03:57.365: E/AndroidRuntime(6662):     at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:86)
04-28 12:03:57.365: E/AndroidRuntime(6662):     at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:210)
04-28 12:03:57.365: E/AndroidRuntime(6662):     at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:1771)
04-28 12:03:57.365: E/AndroidRuntime(6662):     at com.**See Double asterisk in above code)

Any help would be appreciated.

It looks like the error was because I had min version 3 in the Manifest accidentally instead of 4 or later. I am still trying to get a smooth touch enabled gallery working.

 <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM