开发者

Android- Gesture enabled tab layout with lists

I am trying to get swipe gestures working with a tab layout in an android app. What I am working with is a single activity with a different view for each tab. Rather than having them contained in a FrameLayout, I am using a ViewFlipper for my tab content. I want to make left/right swipe gestures change the active tabs the active tab to the one to the left or right of the current one.

The problem arises when swiping to and from my third list after items have been added to it. I'm seeing a behavior where the third list is displayed, but is overlaid on the list I was on previously.

Any ideas why this is happening?

My java code for switching views/tabs:

public void onSwipe(int direction) {     
    switch (direction) {     
        case SimpleGestureFilter.SWIPE_RIGHT : swipeRight(); break;
        case SimpleGestureFilter.SWIPE_LEFT :  swipeLeft(); break;
    }    
}

private void swipeLeft() {  

    viewFlipper.setInAnimation(this,R.anim.slide_in_right);
    viewFlipper.setOutAnimation(this,R.anim.slide_out_left);
    viewFlipper.showNext();
    tabHost.setCurrentTab( (tabHost.getCurrentTab()+1 ) % NUMTABS );
}

private void swipeRight() {

    viewFlipper.setInAnimation(this, android.R.anim.slide_in_left); 
    viewFlipper.setOutAnimation(this,android.R.anim.slide_out_right);
    viewFlipper.showPrevious();
    tabHost.setCur开发者_高级运维rentTab( (tabHost.getCurrentTab()+(NUMTABS-1) ) % NUMTABS ); //loop around to avoid -1
}

My layout:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />
    <ViewFlipper
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/tabs"
        android:layout_above="@+id/search_button"
        android:padding="5dp" >          
         <ListView android:id="@+id/list1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:choiceMode="multipleChoice"
          android:layout_alignParentTop="true"
          android:textFilterEnabled="true"/>

         <ListView android:id="@+id/list2"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:choiceMode="multipleChoice"
          android:layout_alignParentTop="true"
          android:textFilterEnabled="true"/>


         <ListView android:id="@+id/list3"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" 
          android:layout_alignParentTop="true"/>


     </ViewFlipper>
     <Button android:id="@+id/search_button"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:text="Search" />

</RelativeLayout>


This is now supported out of the box in the Android SDK compatibility libs: http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html?m=1

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜