如何将选项卡式活动添加到导航抽屉活动中? [英] How To Add Tabbed Activity Into Navigation Drawer Activity?

查看:44
本文介绍了如何将选项卡式活动添加到导航抽屉活动中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说我们无法在片段内运行Android活动.

I heard that we can not run Android activity inside fragment.

我选择了 Navigation Drawer Activity 开始了一个新项目.然后,我需要使用选项卡式活动作为默认视图,如下所示:

I started a new project with Navigation Drawer Activity chosen. then, I need tabbed activity as default view like this :

如果我们不能在导航抽屉活动中将选项卡式活动作为片段运行,那么如何添加它?

If we can not run tabbed activity as fragment in navigation drawer activity then how to add it?

推荐答案

在导航抽屉中激活更改布局,如下所示:

In the navigation drawer activtiy change your layout like this :

<?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代码中,添加以下行:

Now in your Activity Java code inside onCreate() add these lines :

    @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()方法之外创建片段适配器:

Now outside onCreate() method create the fragment adapter :

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;
        }
    }
}

输出:

干杯,祝您编程愉快.

这篇关于如何将选项卡式活动添加到导航抽屉活动中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆