将多个片段添加到cheesesquare演示中

时间:2016-04-27 11:49:08

标签: android-fragments android-tablayout android-coordinatorlayout android-appbarlayout

我正在使用开源项目(https://github.com/chrisbanes/cheesesquare)来开发自己的应用程序。

我想要的是:

1.在Home Fragment中,toobar下面有一个tablayout,当recycelview滚动时,工具栏可以隐藏,但FloatingActionButton总是停留; enter image description here

enter image description here 2.在Message Frgment中,没有Tab,在FloatingActionButton上,只有一个带有工具栏的简单空白片段。 enter image description here

我尝试在MainActivity中执行此操作:

private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                menuItem.setChecked(true);


                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                Fragment fragment = null;

                int id = menuItem.getItemId();

                if (id == R.id.nav_home) {

                    fragment = new HomeFragment();
                } else if (id == R.id.nav_message) {

                    fragment = new MessageFragment();
                } else if (id == R.id.nav_friends) {

                    fragment = new FriendsFragment();
                } else if (id == R.id.nav_discussion) {

                    fragment = new DiscussionFragment();
                }

                ft.replace(R.id.viewpager, fragment);
                ft.commit();

                mDrawerLayout.closeDrawer(GravityCompat.START);
                return true;
            }
        });
    }

但这不起作用,因为tablayout同时保留在HomeFragment和MessageFragment中。

当我尝试将tablayout更改为HomeFragment的布局xml时,我也遇到了一些问题,因为下面的代码应该写在MainActivity中

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

我感到困惑,我该怎样做才能实现目标?

1 个答案:

答案 0 :(得分:0)

我尝试解决像this这样的问题,实现基本功能。