【问题标题】:Selecting item of sliding tabs menu from navigation drawer with some fragment从带有一些片段的导航抽屉中选择滑动选项卡菜单项
【发布时间】:2016-08-30 15:41:22
【问题描述】:

我有一个项目导航抽屉,使用库 (https://github.com/mikepenz/MaterialDrawer) 和滑动选项卡,其中包含 3 个片段(HomeFragment、LiveFragment、MovieFragment)。

我在 MainActivity.class 中创建了导航抽屉,其中有一些项目(家庭、直播电视、电影)。当我从导航抽屉中单击项目 Home 然后转到 HomeFragment 时,我想要。我已经尝试使用下面的代码,但是当我从导航抽屉中单击项目主页时没有任何反应。 请帮我解决这个问题。谢谢:)

这是我的 MainAactivity.class

public class MainActivity extends AppCompatActivity{
public final static int NAV_ID_FRAG_ONE = 1;
public final static int NAV_ID_FRAG_TWO = 2;
public final static int NAV_ID_FRAG_THREE = 3;
public final static int NAV_ID_ABOUT_ACTIVITY = 6;

Toolbar toolbar;
private SlidingTabLayout mSlidingTabLayout;
private ViewPager mViewPager;
private Drawer result;
private AccountHeader headerResult;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    toolbar.setTitle("JMN Anywhere");
    setSupportActionBar(toolbar);


    mViewPager = (ViewPager) findViewById(R.id.vp_tabs);
    mViewPager.setAdapter(new TabAdapter(getSupportFragmentManager(), this));

    mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.st1_tabs);
    mSlidingTabLayout.setDistributeEvenly(true);//meratakan posisi icon
    mSlidingTabLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
    mSlidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.slidingcolor));
    mSlidingTabLayout.setCustomTabView(R.layout.tab_view, R.id.tv_tab);
    mSlidingTabLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    mSlidingTabLayout.setViewPager(mViewPager);
    //Navigation Drawer
    //Header
    AccountHeader headerResult = new AccountHeaderBuilder()
            .withActivity(this)
            .withCompactStyle(false)
            .withSavedInstance(savedInstanceState)
            .withThreeSmallProfileImages(false)
            .withHeaderBackground(R.drawable.header)
            .build();

    //List Drawer
    Drawer result = new DrawerBuilder()
            .withActivity(this)
            .withToolbar(toolbar)
            .withDisplayBelowStatusBar(true)
            .withActionBarDrawerToggleAnimated(true)
            .withDrawerGravity(Gravity.LEFT)
            .withSavedInstance(savedInstanceState)
            .withAccountHeader(headerResult)
            .withHasStableIds(true)
            .withAccountHeader(headerResult) //set the AccountHeader we created earlier for the header
            .addDrawerItems(//set item drawer
                    new PrimaryDrawerItem().withName("Home").withDescription("Beranda").withIcon(R.drawable.ic_home_black_48dp).withIdentifier(NAV_ID_FRAG_ONE).withSelectable(false),
                    new PrimaryDrawerItem().withName("Live TV").withDescription("Siaran Televisi").withIcon(R.drawable.ic_live_tv_black_48dp).withIdentifier(NAV_ID_FRAG_TWO).withSelectable(false),
                    new PrimaryDrawerItem().withName("Movies").withDescription("Film-film").withIcon(R.drawable.ic_local_movies_black_48dp).withIdentifier(NAV_ID_FRAG_THREE).withSelectable(false),
                    new ExpandableDrawerItem().withName("Categories").withLevel(2).withIdentifier(4).withSelectable(false).withSubItems(
                            new SecondaryDrawerItem().withName("Action").withLevel(3).withIdentifier(2000),
                            new SecondaryDrawerItem().withName("Comedy").withLevel(3).withIdentifier(2001)
                    ),
                    new SectionDrawerItem().withName("Others"),
                    new SecondaryDrawerItem().withName("Account").withIcon(R.drawable.ic_account_box_black_48dp).withIdentifier(5).withSelectable(false),
                    new SecondaryDrawerItem().withName("About").withIcon(R.drawable.ic_info_black_48dp).withIdentifier(NAV_ID_ABOUT_ACTIVITY).withSelectable(false),
                    new SecondaryDrawerItem().withName("Logout").withIcon(R.drawable.ic_exit_to_app_black_48dp).withIdentifier(7).withSelectable(false)
            )
            .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                @Override
                public boolean onItemClick(View view, int i, IDrawerItem drawerItem) {
                    Fragment fragment = null;

                    switch ((int) drawerItem.getIdentifier()) {
                        case NAV_ID_FRAG_ONE:
                            fragment = new HomeFragment();
                            break;
                        case NAV_ID_FRAG_TWO:
                            fragment = new LiveFragment();
                            break;
                        case NAV_ID_FRAG_THREE:
                            fragment = new MovieFragment();
                            break;
                        case NAV_ID_ABOUT_ACTIVITY:
                            Intent intent = new Intent(MainActivity.this, AboutActivity.class);
                            startActivity(intent);
                            break;
                    }

                    return false;
                }
            })
            .withSelectedItem(1)
            .withFireOnInitialOnClick(true)
            // add the items we want to use with our Drawer
            .build();

    new RecyclerViewCacheUtil<IDrawerItem>().withCacheSize(2).apply(result.getRecyclerView(), result.getDrawerItems());

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            // User chose the "Settings" item, show the app settings UI...
            Intent in = new Intent("com.ajjunaedi.jmnanywhere.AboutActivity");
            startActivity(in);
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

}

【问题讨论】:

  • onItemClick方法中的fragmnet变量,已初始化但从未使用过。

标签: android android-fragments navigation-drawer pagerslidingtabstrip


【解决方案1】:

您需要添加 FragmentManager 来添加/删除/替换片段。

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.fragment,fragment).commit();

getSupportFragmentManager() = 返回 FragmentManager 以与与此活动关联的片段进行交互。

.beginTransaction() = 对与此 FragmentManager 关联的 Fragment 启动一系列编辑操作。

R.id.fragment 是您放置片段的容器的 ID。

有关更多信息,请参阅这篇文章 - What does FragmentManager and FragmentTransaction exactly do?

编辑:您将在您的 switch 语句之后添加它。我希望这是您正在寻找的答案。

将此添加到您的 xml 文件中。在baselayout里面

<RelativeLayout
    android:id="@+id/fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

根据您的需要调整它的大小。或者您可以简单地将行 android:id="@+id/fragment" 添加到您的基本布局中。

【讨论】:

  • 我之前没有这个 R.id.fragment,我在哪里添加它?
  • 您需要在 xml 文件中的布局内添加一个布局(线性/相对)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多