简体   繁体   English

在选择的导航项目上不显示带有选项卡布局的片段

[英]On navigation item selected not showing fragment with Tab Layout

I am doing a project in which I want to show different fragments depending on where I pick on the navigation view.我正在做一个项目,我想根据我在导航视图上选择的位置显示不同的片段。 The problem is that It never shows the fragments that it must show.问题是它从不显示它必须显示的片段。

The view of the Navigation view is called activity_main and from there you can arrive to the content_main which is the layout used by the fragment manager. Navigation 视图的视图称为 activity_main,从那里您可以到达 content_main,它是片段管理器使用的布局。

The problem is that the two tabs are not integrated in the toolbar of the mainactivity问题是mainactivity的toolbar里没有集成两个tab

activity_main.xml活动_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

app_bar_main.xml app_bar_main.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="com.blindsiot.carlos.blindsiot.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" />

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

<include layout="@layout/content_main" />

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

content_main.xml content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.blindsiot.carlos.blindsiot.MainActivity"
    tools:showIn="@layout/app_bar_main">

</RelativeLayout>

MainActivity.java主活动.java

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    navigationView.setCheckedItem(R.id.nav_principal);

    /* (. . .) */


    @Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    Fragment currFragment = null;
    FragmentManager fragmentManager = getSupportFragmentManager();

    if (id == R.id.nav_principal) {
        currFragment = new FastAccessFragment();
    } else if (id == R.id.nav_planning) {
        currFragment = new SignUp();
    } else if (id == R.id.nav_settings) {

    } else if (id == R.id.nav_help) {

    } else if (id == R.id.nav_logout) {
        editor.putBoolean("signedUp", false);
        editor.apply();
        Intent intent = new Intent(this, LoginActivity.class);
        if (intent != null) {
            this.finish();
            startActivity(intent);
        }
    } else if (id == R.id.nav_about) {

    }

    if (currFragment != null) {
        fragmentManager
                .beginTransaction()
                .replace(R.id.content_main, currFragment)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .commit();
    }

    setTitle(item.getTitle());


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);

    return true;
}

FastAccessFragment.java - This is where I am actually and where I don't understand why It does not work FastAccessFragment.java - 这就是我实际所在的地方,但我不明白为什么它不起作用

public class FastAccessFragment extends Fragment {

private ViewPager viewPager;
View rootview;
TabLayout tabLayout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootview = inflater.inflate(R.layout.fast_access_fragment, container, false);
    tabLayout = (TabLayout) rootview.findViewById(R.id.tabLayout_fragment);

    tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.action_sign_in_short)).setIcon(R.drawable.tab_login));
    tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.action_sign_up_short)).setIcon(R.drawable.tab_user));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    viewPager = (ViewPager) rootview.findViewById(R.id.pager_fragment);

    PagerAdapter pagerAdapter = new PagerAdapter(getChildFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(pagerAdapter);

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

    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
            tabLayout.getTabAt(0).setText("test");
            tabLayout.getTabAt(1).setText("test2");
        }
    });

    //Adding onTabSelectedListener to swipe views
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    return rootview;
}

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

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

  }

I also experienced the same problem.我也遇到了同样的问题。 I got a fragment inside fragment with TabLayout and ViewPager, inside a Navigation Drawer Activity, in Home menu.我在Home菜单中的导航抽屉活动中使用 TabLayout 和 ViewPager 在片段中找到了一个片段。

When I clicked another menu and clicked back the Home , my fragment inside the TabLayout never got loaded.当我单击另一个菜单并单击返回Home ,我在 TabLayout 中的片段从未加载过。 The tab is loaded, but not the content.已加载选项卡,但未加载内容。

Took me almost 3 days to figured out what was the cause.我花了将近 3 天的时间才弄清楚是什么原因。 And finally I knew that we should use getChildFragmentManager() instead of getActivity().getSupportFragmentManager() .最后我知道我们应该使用getChildFragmentManager()而不是getActivity().getSupportFragmentManager() Because what?因为什么? Because the ViewPager is placed in Fragment, not in Activity.因为 ViewPager 是放在 Fragment 中,而不是放在 Activity 中。 Hope it helps anyone who experiencing the same problem.希望它可以帮助任何遇到相同问题的人。

This is the WRONG one:这是错误的:

private void setupViewPager(View rootView)
{
    this.mainViewPager = rootView.findViewById(R.id.view_pager_main);

    FragmentManager fragmentManager = this.getActivity().getSupportFragmentManager();

    ViewPagerAdapter adapter = new ViewPagerAdapter(fragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);

    DataListFragment movieFragment = (DataListFragment) ((MainActivity) this.getActivity()).getTabFragment(0);
    DataListFragment tvFragment = (DataListFragment) ((MainActivity) this.getActivity()).getTabFragment(1);

    adapter.addFragment(movieFragment);
    adapter.addFragment(tvFragment);

    this.mainViewPager.setAdapter(adapter);
}

This is the correct one:这是正确的:

private void setupViewPager(View rootView)
{
    this.mainViewPager = rootView.findViewById(R.id.view_pager_main);

    FragmentManager fragmentManager = this.getChildFragmentManager();

    ViewPagerAdapter adapter = new ViewPagerAdapter(fragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);

    DataListFragment movieFragment = (DataListFragment) ((MainActivity) this.getActivity()).getTabFragment(0);
    DataListFragment tvFragment = (DataListFragment) ((MainActivity) this.getActivity()).getTabFragment(1);

    adapter.addFragment(movieFragment);
    adapter.addFragment(tvFragment);

    this.mainViewPager.setAdapter(adapter);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM