使用NavigationDrawer从活动创建的Fragment中添加FragmentTabHost

时间:2016-02-16 19:25:28

标签: android android-fragments navigation-drawer fragment-tab-host android-navigationview

我一直在尝试做什么

使用Activity implements NavigationView.OnNavigationItemSelectedListener的{​​{1}},我几天打算做的事情是。我想在Android Studio 1.5.1上为导航抽屉模板提供的android.support.v4.app.Fragment中的FrameLayout内创建两个标签。
  - 我的当前活动使用工具栏而不是ActionBar。
  - 我希望使用ViewPager;因此需要使用支持Fragments   - 我在线跟踪了几个代码示例并查看了Android Developers Reference

其他关联帖子:

Unable to add Tabs inside Fragment of Navigation Drawer Android | 这使用Fragments和ActionBar

Android TabHost inside Fragment | Chosen Answer -> using FragmentActivity | 不幸地使用了一个新的FragmentActivity但我需要保留我的NavigationDrawer所以我正在使用一个活动

Android TabHost only inside one Fragment (NavigationDrawer) | 由于我不熟悉使用标签

,这并没有给我很多帮助

Android Adding Tab inside Fragment | Chosen Answer -> using FragmentTabHost | 我从此示例中获取了FragmentTabHost XML和Java代码,但它没有工作

Tabs in a class that extends fragment | andswer指的是googles文档,它不适用于我。

Adding Tab inside Fragment In Android? | 使用android.app.Fragment,建议的答案无效。

Using TabLayout inside a Fragment; tab text invisible | 我正在使用设计库v23.1.1 |在v22.2.1中报告了错误。这里的代码没有给我任何标签


任何建议都将不胜感激。

我的代码

NavActivity Class

import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;


public class NavigationMenuActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

    Toolbar toolbar;
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle toggle;
    NavigationView navigationView;
    FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navmenu_act);

    //Setting up the Toolbar
    toolbar = (Toolbar) findViewById(R.id.navmenu_appbar_tbar);
    setSupportActionBar(toolbar);

    //Set up the action bar toggle
    drawerLayout = (DrawerLayout) findViewById(R.id.navmenu_act_drawerlay);
    toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.setDrawerListener(toggle);
    toggle.syncState();

    //Set up the navigation View
    navigationView = (NavigationView) findViewById(R.id.navmenu_nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //SET THE FRAGMENT
    fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.navmenu_appbar_fl);

    if(fragment==null) { //If no fragment exists relate the fragment
        fragment = new TabLayoutFragmentExample();
        fragmentManager.beginTransaction()
                .add(R.id.navmenu_appbar_fl, fragment)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .commit();
    }
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.navmenu_act_drawerlay);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}



@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    if (id == R.id.nav_profile) {
        fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.navmenu_appbar_fl);
        if(fragment!=null) { //If a fragment exists replace the fragment
            fragment = new TestFragmentNoTabs();
            fragmentManager.beginTransaction()
                    .replace(R.id.navmenu_appbar_fl, fragment)
                    .commit();
        }
    } else if (id == R.id.nav_near_me) {
        // Handle the near me action here

    } else if (id == R.id.nav_proximity) {
        // Handle the proximity action
        fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.navmenu_appbar_fl);
        if(fragment!=null) {//If A fragment exists replace the fragment
            fragment = new TestFragmentNoTabsTWO();
            fragmentManager.beginTransaction()
                    .replace(R.id.navmenu_appbar_fl, fragment)
                    .commit();
        }
    }
    //Close the drawerLayout after it is clicked?
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.navmenu_act_drawerlay);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    //sync the toggle
    toggle.syncState();
}
}

navmenu_appbar.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".NavigationMenuActivity">

    <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/navmenu_appbar_tbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/JethrosTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/navmenu_appbar_fl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>
</android.support.design.widget.CoordinatorLayout>

TablayoutFragmentExample Class

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;


public class TabLayoutFragmentExample extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View inflatedView = inflater.inflate(R.layout.tab_layout_frag, container, false);

    TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tab_layout_frag_tl);
    tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
    final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.tab_layout_frag_vp);

    viewPager.setAdapter(new PagerAdapter //ChildFragManager; Tnx @Luksprog
            (getChildFragmentManager(), tabLayout.getTabCount())); 

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.setupWithViewPager(viewPager); //Set Viewpager; Tnx @Luksprog

    return inflatedView;
}

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:
                TestFragmentNoTabs tab1 = new TestFragmentNoTabs();
                return tab1;
            case 1:
                TestFragmentNoTabsTWO tab2 = new TestFragmentNoTabsTWO();
                return tab2;
            default:
                return null;
        }
    }

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

tab_layout_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout_frag_tl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/tab_layout_frag_vp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"/>

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

TestFragmentNoTabs&amp; TestFragmentNoTabsTWO(片段1和片段2)

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

public class TestFragmentNoTabs extends Fragment {
    public final String TAG = "TestFragmentNoTabs";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //Inflate the fragment view
        View view = inflater.inflate(R.layout.testfragnotabs, container, false);
        return view; //Return the view to the activity
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}

testfragnotabs.xml (类似于testfragnotabstwo.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/nearme_frag_rlay"
    android:clickable="false">

    <TextView
        android:id="@+id/nearme_frag_tv_seclab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" TestFragmentNoTabsOne -> testfragnotabs.xml" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView1"
        android:layout_above="@+id/nearme_frag_tv_seclab" />

</RelativeLayout>

以上代码的现状

该项目构建良好,导航抽屉也正常工作。但是片段和TabLayout应该出现的框架布局是空的,带有白色背景(就像tab_layout_frag.xml中的设计预览一样;这可能意味着ViewPager或TabLayout存在问题?)。

0 个答案:

没有答案