2016-03-28 5 views
1

私は2つのタブを持っています。タブの位置はレイアウトの下です。私はGoogleプレイのようなアクションバーの下のタブ位置を望んでいます。別のタブには、saveEnabledをfalseに設定しても、前のタブからのデータが表示されます。ここ は私のコードアクションバーの下のタブ位置を作る方法

TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Reusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 


    // Do the same for the other tabs 

    intent = new Intent().setClass(this, RunningOrder.class); 
    spec = tabHost.newTabSpec("runningOrder") 
    .setIndicator("Running Order", res.getDrawable(R.drawable.running_order)) 
     .setIndicator("Running Order") 
    .setContent(intent); 
    tabHost.addTab(spec); 


    intent = new Intent().setClass(this, OrderHistoryTab.class); 
    spec = tabHost 
    .newTabSpec("orderHistory") 
    .setIndicator("Order History", 
    res.getDrawable(R.drawable.history_icon)) 
    .setContent(intent); 
    tabHost.addTab(spec); 

    //set tab which one you want open first time 0 or 1 or 2 
    tabHost.setCurrentTab(0); 

であり、これは私のxmlレイアウトアクションバースワップでframeLayoutとtabWidget位置の下のタブ位置を作るための

 <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white" 
    android:orientation="vertical" > 



    <TabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:saveEnabled="false" 
    android:layout_height="fill_parent" 

    > 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:saveEnabled="false" 
     android:layout_weight="1"/> 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:textAlignment="textStart" 
     android:saveEnabled="false" 
     android:layout_marginBottom="-4dp"/> 

</LinearLayout> 

答えて

0

です。

<TabWidget 
    android:id="@android:id/tabs" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0" 
    android:textAlignment="textStart" 
    android:saveEnabled="false" 
    android:layout_marginBottom="-4dp"/> 


<FrameLayout 
    android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:saveEnabled="false" 
    android:layout_weight="1"/> 
+0

ありがとうございます! – puja

+0

なぜ、現在のタブには以前のタブデータが表示されるのですか? – puja

0

Google Playと同様に、デザインサポートライブラリのviewpagerとともにTabLayoutを使用できます。

あなたは以下のようにコーディングすることができ、あなたの活動に
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/coordinatorLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".HomeActivity"> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <android.support.v7.widget.Toolbar 
     android:id="@+id/hometoolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/app_action_bar_primary" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:layout_scrollFlags="scroll|enterAlways" /> 
    <android.support.design.widget.TabLayout 
     android:id="@+id/home_tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:layout_below="@+id/hometoolbar" 
     android:background="?attr/colorPrimary" 
     app:borderWidth="0dp" 
     app:tabPaddingStart="-1dp" 
     app:tabPaddingEnd="-1dp" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:tabIndicatorColor="@color/app_color_secondary" 
     app:tabIndicatorHeight="5dp"/> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/home_tab_layout"/> 
    </RelativeLayout> 
</android.support.design.widget.CoordinatorLayout> 

TabLayout tabLayout = (TabLayout) findViewById(R.id.home_tab_layout); 
tabLayout.addTab(tabLayout.newTab().setText("Running Order")); 
tabLayout.addTab(tabLayout.newTab().setText("Order History")); 
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); 
final PagerAdapter adapter = new PagerAdapter 
     (getSupportFragmentManager(), tabLayout.getTabCount()); 
viewPager.setAdapter(adapter); 

あなたは以下のようにFragmentStatePagerAdapter拡張することにより、アダプタを作成することができ、

public class PagerAdapter extends FragmentStatePagerAdapter { 
     int mNumOfTabs; 
     public PagerAdapter(FragmentManager fm, int NumOfTabs) { 
      super(fm); 
      this.mNumOfTabs = NumOfTabs; 
     } 
     @Override 
     public Fragment getItem(int position) { 
      switch (position) { 
       case 0: 
        RunningOrderFragment tab1 = new RunningOrderFragment(); 
        return tab1; 
       case 1: 
        OrderHistoryFragment tab2 = new OrderHistoryFragment(); 
        return tab2; 
       default: 
        return null; 
      } 
     } 
     @Override 
     public int getCount() { 
      return mNumOfTabs; 
     } 
    } 

希望これはあなたを助けます。

関連する問題