我想在其中添加一个包含多个视图的viewflipper(比如3个视图,每个视图中有一个图像)。以下是我的代码:
在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&gt; 3> 2而不是1> 2&gt; 3在刷卡。由于我是android的新手,我刚从别处复制了这段代码而没有理解代码。
答案 0 :(得分: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;
}