1

我一直在努力做的事

几天来我的目标是使用Activitythat implements NavigationView.OnNavigationItemSelectedListener。我希望它在 Android Studio 1.5.1 上的导航抽屉模板提供的aandroid.support.v4.app.Fragment中创建两个选项卡。 - 我的当前活动使用工具栏而不是操作栏。 - 我希望使用; 因此需要使用 Support 。 - 我在网上关注了几个代码示例,并查看了Android Developers ReferenceFrameLayout

ViewPagerFragments


其他链接帖子:

无法在导航抽屉 Android 的片段中添加标签| 这使用 Fragments 和 ActionBar

Android TabHost 内部 Fragment | 选择答案-> 使用 FragmentActivity | 不幸的是使用了一个新的 FragmentActivity 但我需要保留我的 NavigationDrawer 所以我正在使用一个活动

Android TabHost 仅在一个 Fragment 内 (NavigationDrawer) | 这并没有真正为我提供太多帮助,因为我对使用标签很陌生

Android 在 Fragment 中添加 Tab | 选择答案-> 使用 FragmentTabHost | FragmentTabHost从这个示例中获取了 XML 和 Java 代码,但它没有工作

扩展片段的类中的选项卡| andswer 指的是对我不起作用的谷歌文档。

在 Android 的 Fragment 中添加选项卡?| 用途android.app.Fragment和建议的答案不起作用。

在 Fragment 中使用 TabLayout;标签文本不可见| 我正在使用设计库v23.1.1| 中报告的错误v22.2.1。这里的代码也没有给我标签


任何建议将不胜感激。

我的代码

导航活动类

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>

TablayoutFragment示例类

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 & 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 存在问题?)。

4

0 回答 0