开发者

How to use a ListView and a ViewFlipper to navigate user in an Android app?

开发者 https://www.devze.com 2022-12-29 01:51 出处:网络
I want to set up some menu-like navigator for my app. There is a listView in the ma开发者_开发百科in page and it contains two items, click each one will show its child view with ViewFlipper, and if

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

There is a listView in the ma开发者_开发百科in 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?


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

精彩评论

暂无评论...
验证码 换一张
取 消