2015-09-04 60 views
0

我在我的活動中使用三個viewflippers,每個人都從服務器加載圖片。他們三人的動畫效果很好。重點是我也試圖刷他們每一個。我實現了一個手勢檢測,這裏是代碼:在瀏覽器上滑動衝突

package com.erc.AndroidViewFlipper; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.view.MotionEvent; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ViewFlipper; 

public class AndroidViewFlipperActivity extends Activity { 

    ViewFlipper page,page2; 

    Animation animFlipInForeward; 
    Animation animFlipOutForeward; 
    Animation animFlipInBackward; 
    Animation animFlipOutBackward; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     page = (ViewFlipper)findViewById(R.id.flipper); 
     page2 = (ViewFlipper)findViewById(R.id.flipper2); 

     animFlipInForeward = AnimationUtils.loadAnimation(this, R.anim.flipin); 
     animFlipOutForeward = AnimationUtils.loadAnimation(this, R.anim.flipout); 
     animFlipInBackward = AnimationUtils.loadAnimation(this, R.anim.flipin_reverse); 
     animFlipOutBackward = AnimationUtils.loadAnimation(this, R.anim.flipout_reverse); 

     page.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.flipin)); 
     page.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.flipout)); 
     page.startFlipping(); 
     page.setAutoStart(true); 

     page2.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.flipin)); 
     page2.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.flipout)); 
     page2.startFlipping(); 
     page2.setAutoStart(true); 



    } 

    private void SwipeRight(){ 
     page.setInAnimation(animFlipInBackward); 
     page.setOutAnimation(animFlipOutBackward); 
     page.showPrevious(); 
    } 

    private void SwipeLeft(){ 
     page.setInAnimation(animFlipInForeward); 
     page.setOutAnimation(animFlipOutForeward); 
     page.showNext(); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     // TODO Auto-generated method stub 
     return gestureDetector.onTouchEvent(event); 
    } 

    SimpleOnGestureListener simpleOnGestureListener 
    = new SimpleOnGestureListener(){ 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
       float velocityY) { 

      float sensitvity = 50; 
      if((e1.getX() - e2.getX()) > sensitvity){ 
       SwipeLeft(); 
      }else if((e2.getX() - e1.getX()) > sensitvity){ 
       SwipeRight(); 
      } 

      return true; 
     } 

    }; 

    GestureDetector gestureDetector 
    = new GestureDetector(simpleOnGestureListener); 
} 

的問題是,無論我觸摸屏幕,第一viewflipper將刷卡。我想要的是,當我觸摸一個特定的視圖片時,只有被觸摸的片段會被滑動。請幫忙嗎?

回答

0

您錯誤地將ViewFlipper實例分配給了pagepage2變量。在下面的代碼片段中,這兩個語句使用了同樣的觀點ID,R.id.flipper

page = (ViewFlipper)findViewById(R.id.flipper); 
    page2 = (ViewFlipper)findViewById(R.id.flipper); 

[更新]

此外,您還需要更新您的SwipeRight()SwipeLeft()simpleOnGestureListener,如下(增加了一些方法)。代碼現在測試是否在ViewFlipper中啓動了一個手指滑動手勢,然後滑動該腳蹼。

private void SwipeRight(ViewFlipper vf) { 
    vf.setInAnimation(animFlipInBackward); 
    vf.setOutAnimation(animFlipOutBackward); 
    vf.showPrevious(); 
} 

private void SwipeLeft(ViewFlipper vf) { 
    vf.setInAnimation(animFlipInForeward); 
    vf.setOutAnimation(animFlipOutForeward); 
    vf.showNext(); 
} 

SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() { 

    @Override 
    public boolean onFling(
      MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 

     ViewFlipper vf = getTouchedViewFlipper(e1); 
     if (vf == null) { 
      return true; 
     } 

     float sensitivity = 50.0; 
     if ((e1.getX() - e2.getX()) > sensitivity) { 
      SwipeLeft(vf); 
     } else if ((e2.getX() - e1.getX()) > sensitivity) { 
      SwipeRight(vf); 
     } 

     return true; 
    } 

}; 

private ViewFlipper getTouchedViewFlipper(MotionEvent ev) { 
    ViewFlipper vf = checkTouchedViewFlipper(page, ev); 
    if (vf == null) { 
     vf = checkTouchedViewFlipper(page2, ev); 
    } 
    return vf; 
} 

private ViewFlipper checkTouchedViewFlipper(ViewFlipper vf, MotionEvent ev) { 
    Rect vfRect; 
    vf.getHitRect(vfRect); 
    return (vfRect.contains((int)ev.getX(), (int)ev.getY())) ? vf : null; 
} 
+0

我編輯了我的答案,但沒有解決問題 –

+0

查看我更新的答案。 – cybersam

+0

不幸的是,它沒有工作,我更新了我的代碼,我在你的答案中改變了唯一的事情是我在方法checkTouchedViewFlipper中初始化了vfRect(Rect vfRect = null)。我仍然無法滑動每個viewflipper –