views:

1039

answers:

1

I want to set up some menu-like navigator for my app.

There is a listView in the main page and it contains two items, click each one will show its child view with ViewFlipper, and if user clicked the back button, he will return to the homepage again.

The question is how to make it, I can only use ViewFlipper to flip to next screen or prev screen, how to manage these child views here? How to put them in my layout xml file?

+1  A: 

Here follows a psudo-way of doing it.

//In OnCreate, add a click listener to your listview to make the view flip to the next view.

viewflipper = (ViewFlipper) findViewById(R.id.viewflipper);
listview = (ListView) findViewById(R.id.listview);


listview.setOnItemClickListener(new OnItemClickListener(){
  public void onItemClick(AdapterView<?> a, View v, int position, long id) {
     viewflipper.showNext();

});

// Override the onKeyDown in you Activty to handle the back button click.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if(viewflipper.getVisibleChild() != 0){
           viewflipper.showPrevious();
           return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

// xml for a viewflipper vith the listview as "firstpage" and a simple textview as the "second page"

<ViewFlipper android:id="@+id/viewflipper" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        > 
        <ListView android:id="@+id/listview" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
        /> 
        <TextView android:id="@+id/secondview" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="This is the second view" 
        /> 
</ViewFlipper> 

Check out this example, maybe it can give you some ideas: http://whyandroid.com/android/174-flipping-your-views.html

PHP_Jedi
That particular example you link to is a bit old (August 2008), not to mention ripped off from the original AndroidGuys post. Here are links to the two samples from that post that have been kept up to date: http://github.com/commonsguy/cw-android/tree/master/Fancy/Flipper1/ http://github.com/commonsguy/cw-android/tree/master/Fancy/Flipper2/
CommonsWare
Thanks for your answers. But what I need is a little bit complex. Because the navigation is not always linear, for example, HOME-> A|B|C -> if A selected -> a1|a2|, if B selected -> b1|b2. If I have A|B|C|a1|a2|b1|b2 views all predefined, how can I put all those into that ViewFlipper, or if some of them like a2, b1 are dynamically created by fetching data from internet, what can I do then?
virsir
If i understand correctly, you could add a 2 listviews to the viewflipper. The first listview showing A,B,C and when you click any of the flip to the second listview with its sub-items a1,a2,a3 ... If you click B u can use the same listview to show b1,b2 that was used for a1,a2,a3 etc...
PHP_Jedi