2013-10-10 50 views
0

我想添加一個具有多個視圖的viewflipper(例如,在每個視圖中包含一個圖像的3個視圖)。以下是我的代碼:具有多個視圖的Android Viewflipper序列

在XML:

<ViewFlipper 
android:id="@+id/view_flipper" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_margin="6dip" > 

<!-- The child Views/Layout to flip --> 


<!-- Layout 1 for 1st Screen --> 

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" 
android:orientation="vertical" > 

<ImageView 
android:id="@+id/imageView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/help1" /> 
</LinearLayout> 


<!-- Layout 2 for 2nd Screen --> 

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" 
android:orientation="vertical" > 

<ImageView 
android:id="@+id/imageView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/help2" /> 
</LinearLayout> 

<!-- Layout 3 for 3rd Screen --> 

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" 
android:orientation="vertical" > 

<ImageView 
android:id="@+id/imageView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/help3" /> 
</LinearLayout> 


</ViewFlipper> 

在Java:

viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper); 

// Method to handle touch event like left to right swap and right to left swap 
public boolean onTouchEvent(MotionEvent touchevent) 
{ 
switch (touchevent.getAction()) 
{ 
// when user first touches the screen to swap 
case MotionEvent.ACTION_DOWN: 
{ 
lastX = touchevent.getX(); 
break; 
} 
case MotionEvent.ACTION_UP: 
{ 
float currentX = touchevent.getX(); 

// if left to right swipe on screen 
if (lastX < currentX) 
{ 
// If no more View/Child to flip 
if (viewFlipper.getDisplayedChild() == 0) 
break; 

// set the required Animation type to ViewFlipper 
// The Next screen will come in form Left and current Screen will go OUT from Right 
viewFlipper.setInAnimation(this, R.anim.in_from_left); 
viewFlipper.setOutAnimation(this, R.anim.out_to_right); 
// Show the next Screen 
viewFlipper.showNext(); 
} 

// if right to left swipe on screen 
if (lastX > currentX) 
{ 
if (viewFlipper.getDisplayedChild() == 1) 
break; 
// set the required Animation type to ViewFlipper 
// The Next screen will come in form Right and current Screen will go OUT from Left 
viewFlipper.setInAnimation(this, R.anim.in_from_right); 
viewFlipper.setOutAnimation(this, R.anim.out_to_left); 
// Show The Previous Screen 
viewFlipper.showPrevious(); 
} 
break; 
} 
} 
return false; 
} 

這是工作只是觀點進來的順序1> 3> 2,而不是1> 2 > 3刷卡。由於我剛接觸android,我剛剛從其他地方複製了這段代碼,而未理解代碼。

回答

2

您正在使用ViewFlipper在視圖之間左右滑動。你應該真的使用ViewPager

也有人說,你可以做你想做的與視圖的鰭狀肢,如果你改變你的ACTION_UP到類似:

case MotionEvent.ACTION_UP: { 
    float currentX = touchevent.getX(); 

    if (lastX > currentX) { 
     if (viewFlipper.getDisplayedChild() == 2) 
      break; 

     // anim stuff here... 

     viewFlipper.showNext(); 
    } 

    if (lastX < currentX) { 
     if (viewFlipper.getDisplayedChild() == 0) 
      break; 

     // anim stuff here... 

     viewFlipper.showPrevious(); 
    } 
    break; 
} 
+0

是不是這可能與viewflipper? – Bhaijaan

+0

太棒了!謝謝 – Bhaijaan