更改ViewFlipper中显示的旁边的视图

时间:2012-01-12 23:07:52

标签: android android-view

我想要完成的是拥有一个带有三个按钮的主屏幕,并根据哪个按钮按下相应的视图从右侧滑入。我使用ViewFlipper并使用ViewFlipper.showNext()我可以在下一个视图中滑动。我希望能够更改下一个View,以便我可以使用与视图相对应的按钮。

我尝试使用ViewFlipper.add()ViewFlipper.remove()来实现这一目标,但无济于事。有人能告诉我哪里出错了吗?提前谢谢。

以下是我的main.xml的代码,每个视图都是一个单独的布局。

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<include
    android:id="@+id/main"
    layout="@layout/first_view" />

//Do I need these below?
<include
    android:id="@+id/first"
    layout="@layout/more_info_1" />

<include
    android:id="@+id/second"
    layout="@layout/more_info_2" />

<include
    android:id="@+id/third"
    layout="@layout/more_info_3" />

</ViewFlipper>

这是我的onCreate()方法:

flipper = (ViewFlipper) findViewById(R.id.flipper);
    Button moreInfo1 = (Button) findViewById(R.id.moreinfobutton1);
    Button moreInfo2 = (Button) findViewById(R.id.moreinfobutton2);
    Button moreInfo3 = (Button) findViewById(R.id.moreinfobutton3);
    Button backButton1 = (Button) findViewById(R.id.backbutton1);
    Button backButton2 = (Button) findViewById(R.id.backbutton2);
    Button backButton3 = (Button) findViewById(R.id.backbutton3);

    moreInfo1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            flipper.setInAnimation(inFromRightAnimation());
            flipper.setOutAnimation(outToLeftAnimation());
            flipper.showNext();
            }
        });

    moreInfo2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            flipper.setInAnimation(inFromRightAnimation());
            flipper.setOutAnimation(outToLeftAnimation());

            //What to put here to change the next View to something else?
            flipper.showNext();
            }
        });

    moreInfo3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            flipper.setInAnimation(inFromRightAnimation());
            flipper.setOutAnimation(outToLeftAnimation());
            flipper.showNext();
            }
        });

    backButton1.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
             flipper.setInAnimation(inFromLeftAnimation());
             flipper.setOutAnimation(outToRightAnimation());
             flipper.showPrevious();      
         }
     });

    backButton2.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
             flipper.setInAnimation(inFromLeftAnimation());
             flipper.setOutAnimation(outToRightAnimation());
             flipper.showPrevious();      
         }
     });

    backButton3.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
             flipper.setInAnimation(inFromLeftAnimation());
             flipper.setOutAnimation(outToRightAnimation());
             flipper.showPrevious();      
         }
     });

1 个答案:

答案 0 :(得分:1)

尝试使用ViewFlipper

的这些方法
public void removeViewAt (int index)
public void addView (View child, int index, ViewGroup.LayoutParams params)

这应该允许您删除不再需要的某个视图并添加另一个视图。