2017-05-03 61 views
1

你好我想爲我的自定義佈局,增添一分tablayout的標籤佈局這一個: enter image description here定製與多級標籤

我已經成功添加了tablayout但:

如何加入sublayout如下所示?? 正如我們在圖片中看到的第一子佈局有關,第一個選項卡布局

我layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.tiger.alahedclub.activity.navigation"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:popupTheme="@style/generalnotitle" 
    > 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      app:titleTextColor="@android:color/black" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/yellow" 
      app:popupTheme="@style/generalnotitle"> 



      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 


      <TextView 
       android:id="@+id/action_bar_title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_centerHorizontal="true" 
       android:text="@string/app_name" 
       android:textColor="@android:color/black" 
       android:textSize="24sp" /> 


      </RelativeLayout> 
     </android.support.v7.widget.Toolbar> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:background="@color/yellow" 
      app:tabTextColor="@android:color/black" 
      app:tabTextAppearance="@style/MineCustomTabText" 
      app:tabSelectedTextColor="#1B5E20" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabIndicatorHeight="2dp" 
      app:tabMaxWidth="0dp" 
      app:tabMode="fixed" 
      app:tabGravity="fill" /> 


    </android.support.design.widget.AppBarLayout> 


    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#B71C1C" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

</android.support.design.widget.CoordinatorLayout> 

更新:

活動持viewpager:

public class Detail_match extends AppCompatActivity { 
    private TabLayout tabLayout,tabLayout2; 
    private ViewPager viewPager; 

    int onStartCount = 0; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     onStartCount = 1; 
     if (savedInstanceState == null) // 1st time 
     { 
      this.overridePendingTransition(R.anim.anim_slide_in_left, 
        R.anim.anim_slide_out_left); 
     } else // already created so reverse animation 
     { 
      onStartCount = 2; 
     } 
     setContentView(R.layout.activity_detail_match); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayShowTitleEnabled(false); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 


     viewPager = (ViewPager) findViewById(R.id.viewpager); 
     setupViewPager(viewPager); 


     tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(viewPager); 

     tabLayout2 = (TabLayout) findViewById(R.id.tabs2); 


    } 


    @Override 
    protected void onPause() { 
     super.onPause(); 
    } 
    @Override 
    protected void onStart() { 
     // TODO Auto-generated method stub 
     super.onStart(); 
     if (onStartCount > 1) { 
      this.overridePendingTransition(R.anim.anim_slide_in_right, 
        R.anim.anim_slide_out_right); 

     } else if (onStartCount == 1) { 
      onStartCount++; 
     } 

    } 

    private void setupViewPager(ViewPager viewPager) { 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
     adapter.addFrag(new FiveFragment(), getString(R.string.lineup)); 
     adapter.addFrag(new SixFragment(), getString(R.string.stats)); 
     viewPager.setAdapter(adapter); 
    } 

    class ViewPagerAdapter extends FragmentPagerAdapter { 
     private final List<Fragment> mFragmentList = new ArrayList<>(); 
     private final List<String> mFragmentTitleList = new ArrayList<>(); 

     public ViewPagerAdapter(FragmentManager manager) { 
      super(manager); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      return mFragmentList.get(position); 
     } 

     @Override 
     public int getCount() { 
      return mFragmentList.size(); 
     } 

     public void addFrag(Fragment fragment, String title) { 
      mFragmentList.add(fragment); 
      mFragmentTitleList.add(title); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      return mFragmentTitleList.get(position); 
     } 
    } 
} 

我的碎片five.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/coordinatorLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v4.widget.SwipeRefreshLayout 
      android:id="@+id/swipe_refresh" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
      <ScrollView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <android.support.v7.widget.RecyclerView 
       android:id="@+id/recycler_post" 
       android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 


      </ScrollView> 

     </android.support.v4.widget.SwipeRefreshLayout> 

    </RelativeLayout> 



</android.support.design.widget.CoordinatorLayout> 
+0

在您的viewpager片段佈局中...添加子佈局..使用另一個佈局佈局 – rafsanahmad007

+0

在片段XML內部創建另一個Tablayout。 –

+0

@ rafsanahmad007你能指導我怎麼做 –

回答

2

在你Viewpager片段:那你是返回Fragment getItem(int position)

你需要添加一個Tablayout和XML

所以一個viewpagerFiveFragment XML:

<LinearLayout 
    android:id="@+id/main_layout" 
    android:orientation="vertical" 
    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:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <!-- our tablayout to display tabs --> 
    <android.support.design.widget.TabLayout 
     android:id="@+id/tabLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

    <!-- View pager to swipe views --> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent"/> 

</LinearLayout> 

而且在FiveFragmentonCreateView()用Tablayout設置viewpager ..

贊:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.your_layout, container, false); 
    viewPager = (ViewPager) view.findViewById(R.id.pager); 
    addTabs(viewPager); 

    tabLayout = (TabLayout) view.findViewById(R.id.tabLayout); 
    tabLayout.setupWithViewPager(viewPager); 

} 

private void addTabs(ViewPager viewPager) { 
    ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager()); 
    adapter.addFrag(new Sublayout1(), "ONE"); 
    adapter.addFrag(new Sublayout2(), "TWO"); 
    viewPager.setAdapter(adapter); 
} 

class ViewPagerAdapter extends FragmentPagerAdapter { 
    private final List<Fragment> mFragmentList = new ArrayList<>(); 
    private final List<String> mFragmentTitleList = new ArrayList<>(); 

    public ViewPagerAdapter(FragmentManager manager) { 
     super(manager); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return mFragmentList.get(position); 
    } 

    @Override 
    public int getCount() { 
     return mFragmentList.size(); 
    } 

    public void addFrag(Fragment fragment, String title) { 
     mFragmentList.add(fragment); 
     mFragmentTitleList.add(title); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return mFragmentTitleList.get(position); 
    } 
} 

因此,每個Fragment都包含要控制的項目的嵌套標籤。

+0

非常感謝@ rafsanahmad007我正在嘗試它現在很抱歉回覆晚 –

+0

它完美謝謝你的努力。 –