我有一个项目导航抽屉使用库(https://github.com/mikepenz/MaterialDrawer),滑动标签包含3个片段(HomeFragment,LiveFragment,MovieFragment)。
我在MainActivity.class中创建了导航抽屉,还有一些项目(Home,Live TV,Movie)。我想从导航抽屉中单击项目Home然后转到HomeFragment。我尝试使用下面的代码,但是当我从导航抽屉中单击项目Home时没有任何反应。 请帮我解决这个问题。谢谢:)
这是我的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);
}
}
}
答案 0 :(得分:0)
您需要添加FragmentManager来添加/删除/替换片段。
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment,fragment).commit();
getSupportFragmentManager()=返回FragmentManager以与与此活动相关联的片段进行交互。
.beginTransaction()=对与此FragmentManager关联的片段启动一系列编辑操作。
R.id.fragment是您放置片段的容器的ID。
有关详细信息,请参阅此帖子 - What does FragmentManager and FragmentTransaction exactly do?
编辑:您将在切换语句后添加此内容。我希望这是你正在寻找的答案。
将其添加到您的xml文件中。在baselayout里面
<RelativeLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
根据您的愿望调整大小。或者您只需将行android:id="@+id/fragment"
添加到baselayout。