How can I use ListView and ViewFlipper to navigate user in Android app?

I want to set up some kind of menu-like navigator for my application.

The main page has a listView, and it contains two items, each of which displays its child view with a ViewFlipper, and if the user pressed the back button, it will return to the home page again.

The question is how to do this, can I only use the ViewFlipper to go to the next screen or the previous screen, how do I manage these child views here? How do I put them in my XML layout file?

+2


a source to share


1 answer


Here's a psudo way to do it.

// In OnCreate, add a click listener to your list to view in 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 of your Activity to refer to the Back button.



@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 viewflipper with list box as "first page" and plain text view as "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> 

      

+3


a source







All Articles