1

我在 LinearLayout 中有一个 webview 和 ViewPager。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout 
    android:id="@+id/webview_wrapper"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1" />


<android.support.v4.view.ViewPager 
    android:id="@+id/slide_page"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#696969" 
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:visibility="gone" />

虽然我在 webview 和 ViewPager 上都设置了 OnTouchListener。当我在拖动时,我尝试在 webview 上开始拖动并将 MotionEvent 传递给 viewPager。

我的代码如下。

    WebView mainView  = tab.getWebView();
    mainView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent e) {
            Log.w("LOGTAG", "onTouch");                     
            switch (e.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    Log.w("LOGTAG", "ACTION_UP");
                    break;
                case MotionEvent.ACTION_MOVE:
                    Log.w("LOGTAG", "ACTION_MOVE");
                    Test(...);
                    mPager.dispatchTouchEvent(e);
                    break;
                case MotionEvent.ACTION_UP: 
                    Log.w("LOGTAG", "ACTION_UP");
                    break;
           }
           return false;
       }
   });

        private void Test(...) {
            FrameLayout wrapper =
                (FrameLayout) container.findViewById(R.id.webview_wrapper); 
            mPager = (ViewPager)container.findViewById(R.id.slide_page);
            ...
            wrapper.setVisibility(View.GONE);
            mPager.setVisibility(View.VISIBLE);        
            mPager.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent e) {
                    Log.w("LOGTAG", "mPager.onTouch");            

                    switch (e.getAction() & MotionEvent.ACTION_MASK) {

                        case MotionEvent.ACTION_DOWN:
                            Log.w("LOGTAG", "mPager.ACTION_DOWN");                      
                            break;
                        case MotionEvent.ACTION_MOVE:
                            Log.w("LOGTAG", "mPager.ACTION_MOVE");
                            break;
                        case MotionEvent.ACTION_UP: 
                            Log.w("LOGTAG", "mPager.ACTION_UP");
                            break;
                    }
                    return false;
                }
            });
        }

这是我的日志:

...

08-06 07:09:16.321: W/(26460): mPager.dispatchTouchEvent(e) = true

08-06 07:09:16.321: W/(26460): onTouch

08-06 07:09:16.321: W/(26460): ACTION_MOVE

08-06 07:09:16.321: W/(26460): mPager.onTouch

08-06 07:09:16.321: W/(26460): mPager.ACTION_MOVE

...

ViewPager onTouchListener 很好地检测到 MotionEvent,但是 viewpager 没有移动。

任何帮助将不胜感激。

4

1 回答 1

2

I solve this problem. I overrided onTouch method of Viewpager, and nothing did in my onTouch method. So viewpager received touch event but didn't move. when I removed TouchListener which I overrided, and it worked well.

于 2013-08-13T00:40:06.390 回答