簡體   English   中英

如何將選項卡式活動添加到導航抽屜活動?

[英]How To Add Tabbed Activity Into Navigation Drawer Activity?

我聽說我們無法在片段內運行Android活動。

我選擇了Navigation Drawer Activity開始了一個新項目。 那么,我需要選項卡式活動作為默認視圖,如下所示:

在此輸入圖像描述

如果我們無法在navigation drawer activity中將tabbed activity作為片段運行,那么如何添加它?

在導航抽屜活動中,更改您的布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="in.net.spectrum.citytour.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
    //
    //
    // Tab layout to show tabs
    //
    //
    <android.support.design.widget.TabLayout
        android:background="@color/colorPrimary"
        app:tabSelectedTextColor="#ffffff"
        app:tabIndicatorColor="#FFFFFF"
        app:tabIndicatorHeight="3dp"
        app:tabTextColor="#FFFFFF"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        android:id="@+id/tbl_pages"/>

</android.support.design.widget.AppBarLayout>
<!-- <include layout="@layout/content_main" />-->
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    //
    //
    //ViewPager to show tab's fragments
    //
    //

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_pages"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />
</FrameLayout>


</LinearLayout>

現在在onCreate()里面的Activity Java代碼中添加以下行:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
           .
           .
           .
           .

    ViewPager vp_pages= (ViewPager) findViewById(R.id.vp_pages);
    PagerAdapter pagerAdapter=new FragmentAdapter(getSupportFragmentManager());
    vp_pages.setAdapter(pagerAdapter);

    TabLayout tbl_pages= (TabLayout) findViewById(R.id.tbl_pages);
    tbl_pages.setupWithViewPager(vp_pages);

    }

現在在onCreate()方法之外創建片段適配器:

class FragmentAdapter extends FragmentPagerAdapter {

    public FragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new YourFragment1();
            case 1:
                return new YourFragment2();
            case 2:
                return new YourFragment3();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }


    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            //
            //Your tab titles
            //
            case 0:return "Profile";
            case 1:return "Search";
            case 2: return "Contacts";
            default:return null;
        }
    }
}

輸出: 在此輸入圖像描述 在此輸入圖像描述

干杯,快樂的編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM