문제

What I'm trying to accomplish is to have a main screen with three buttons, and depending on which button you press the corresponding view slides in from the right hand side. I have used ViewFlipper and using ViewFlipper.showNext() I am able to slide in the next View. I would like to be able to change which View is next in line so that I can have buttons corresponding to views.

I have tried to use ViewFlipper.add() and ViewFlipper.remove() to achieve this but to no avail. Could somebody tell me where I'm going wrong? Thanks in advance.

Below is my code for my main.xml, and each View is a separate layout.

<?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>

And here is my onCreate() method:

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();      
         }
     });
도움이 되었습니까?

해결책

Try using these methods of ViewFlipper:

public void removeViewAt (int index)
public void addView (View child, int index, ViewGroup.LayoutParams params)

This should allow you to remove a certain view you don't need anymore and add another one.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top