开发者

Android: How do I implement a back button into a view flipper

I'm using a view flipper with tabs at the top of the view. I have a lis开发者_Python百科tview under the tabs, clicking an item on the list brings the user to another listview (still within the same view flipper).

What I would like to do is have a back button on the second listview, so the user can go back to the first list. I know I can use the viewflipper.showPrevious() function to go back, but how do I go about creating a back button and attaching this function to it?

Thanks in advance, Here's the layout code:

        <ViewFlipper android:id="@+id/layout_tab_one"
            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" />
        <ListView android:id="@+id/listview2" 
            android:layout_width="fill_parent" android:layout_height="wrap_content" />


        </ViewFlipper>

Activity code:

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


            }});


just override the

     @overide public void onBackPressed() 
         {     
          // your code for previouse list; 

          }

EDIT:just use ListView.addHeaderView(View v) and ListView.addFooterView(View v) android listviews: header and footer views


Another Option Here:

You can always do viewFinder.setDisplayedChild(0), where the number matches the view you want to display. This lets you select which view to display at any time.

Piggybacking off of the previous answer:

@overide public void onBackPressed() 
     {     
     viewFinder.setDisplayedChild(0)

      }

This lets you use the viewflipper more dynamically than simply using the .next() and .previous() methods. So for example you could move have multiple menus, and allow access to all of them from one another.

EX:

   someButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        viewfinder.setDisplayedChild(1);
    }
   });

Hope that helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜