用两个手指翻转不同的Web视图

时间:2012-02-10 18:46:54

标签: android viewflipper gesture-recognition

我有两个WebView要一个接一个地显示,我的意思是,当我滑动或用手指拖动时,第一个webview逐渐消失,让位于当前视图中的第二个,这已经完成,但我现在的问题是我的手指水平移动以滑动或拖动视图取代了水平滚动事件,所以为了解决这个问题,我想把一根手指的动作用于水平滚动和两根手指的移动以便翻转通过不同的观点,但我不知道如何做到这一点,我已经做了一些研究,但我没有找到任何有趣的样本。

这是我的代码(用一根手指翻转作品):

xml文件:

    <ViewFlipper android:id="@+id/details"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >  
        <WebView
          android:id="@+id/webview0"
          android:layout_width="fill_parent"
      android:layout_height="fill_parent"/>  
        <WebView
      android:id="@+id/webview1"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"/>   
    </ViewFlipper>

onCreate():

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    vf = (ViewFlipper) findViewById(R.id.details);

    WebView MyWebView[] =new WebView[2];

    MyWebView[0] = (WebView) findViewById(R.id.webview0);
    MyWebView[0].setWebViewClient(new WebViewClient());
    MyWebView[0].getSettings().setJavaScriptEnabled(true);
    MyWebView[0].loadUrl("http://www.cnn.com");
    MyWebView[0].setOnTouchListener(new OnTouchListener() {            
        @Override
        public boolean onTouch(View v, MotionEvent event) {               
           touchHandler(v, event);
           return false;
        }
     });

    MyWebView[1] = (WebView) findViewById(R.id.webview1);
    MyWebView[1].setWebViewClient(new WebViewClient());
    MyWebView[1].getSettings().setJavaScriptEnabled(true);
    MyWebView[1].loadUrl("http://www.bbc.co.uk/");  
    MyWebView[1].setOnTouchListener(new OnTouchListener() {            
        @Override
        public boolean onTouch(View v, MotionEvent event) {
           touchHandler(v, event);              
           return false;
        }
     });

    viewOrder = new int[MyWebView.length];
    for (int i=0; i<MyWebView.length; i++) {
        viewOrder[i] = i;
        MyWebView[i].setId(i);
    }
}

进行翻转的方法:

 public boolean touchHandler(View v, MotionEvent event) {
    float currentX = event.getX();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        startX = event.getX();

        currentView = vf.getCurrentView();

        movePageThreshold = (currentView.getWidth() / 5);

        int viewCount = viewOrder.length;
        for (int i=0; i<viewCount; i++) {
            //Log.i("MyApp", "ord=id:" + viewOrder[i] + "==" + cv.getId());
            if (viewOrder[i] == currentView.getId()) {
                curIdx = i;
                break;
            }
        }

        if (curIdx >= 0) {
            preIdx = curIdx - 1;
            nxtIdx = curIdx + 1;
            preIdx = (preIdx < 0         )?viewCount - 1:preIdx;
            nxtIdx = (nxtIdx >= viewCount)?0            :nxtIdx;

            prevView = vf.findViewById(viewOrder[preIdx]);
            nextView = vf.findViewById(viewOrder[nxtIdx]);
        }

        //Log.i(LOG_TAG, String.format("Pre=%d(%d),Cur=%d(%d),Nxt=%d(%d)"  ,preIdx,prevView.getId() ,curIdx,currentView.getId() ,nxtIdx,nextView.getId()));

        break;
    case MotionEvent.ACTION_MOVE:
        int travelDistanceX = (int)(currentX - this.startX);
        int fingerPosX = (int)currentX;

        if (flipMode == FLIPMODE_NOMOVE) {
            if (travelDistanceX > SWITCH_THRESHOLD) {
                flipMode = FLIPMODE_PREV;
            } else if
               ( travelDistanceX < (SWITCH_THRESHOLD * -1)  ) {
                flipMode = FLIPMODE_NEXT;
            } else {
                flipMode = FLIPMODE_NOMOVE;
            }
        }

        if (flipMode == FLIPMODE_PREV) {
            prevView.layout(fingerPosX - prevView.getWidth(), 
                      prevView.getTop(), 
                      fingerPosX , 
                      prevView.getBottom());

            vf.bringChildToFront(prevView);
            prevView.setVisibility(View.VISIBLE);

        }
        if ( flipMode == FLIPMODE_NEXT)  {
            nextView.layout(fingerPosX , 
                      nextView.getTop(), 
                      fingerPosX + currentView.getWidth() + nextView.getWidth(), 
                      nextView.getBottom());

            vf.bringChildToFront(nextView);
            nextView.setVisibility(View.VISIBLE);
        }
        break;

    case MotionEvent.ACTION_UP:

        int activeIdx = -1;
        if ((this.startX - currentX) > movePageThreshold ) {
            activeIdx = nxtIdx;
        }else if 
            ((this.startX - currentX) < (movePageThreshold * -1) ) {
            activeIdx = preIdx;
        } else {
            activeIdx = curIdx;
        }
        int activeId = viewOrder[activeIdx];
        for(int i=0; i<vf.getChildCount(); i++) {
            // Log.i("MyApp",String.format("vf_id:%d,sel_id:%d",vf.getChildAt(i).getId(),activeId));
            if (vf.getChildAt(i).getId() == activeId) {
                vf.setDisplayedChild(i);
                break;
            }
        }
        flipMode = 0;
    default:
        break;
    }
    return true;
}

所以请用任何建议(或教程)用两根手指用一根手指替换翻转(滑动/拖动)

提前致谢

0 个答案:

没有答案