我的片段立即打开。我希望它在导航抽屉关闭后打开。
如何在导航抽屉支持库中关闭抽屉后打开片段?
private void initNavigationView()
{
navigationView = (NavigationView) findViewById(R.id.navigation_view);
drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {super.onDrawerClosed(drawerView)
}
};
drawerLayout.post(new Runnable() {
@Override
public void run() {
drawerToggle.syncState();
}
});
drawerToggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(drawerToggle);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.tentang:
break;
case R.id.keluar:
finish();
break;
}
selectDrawerItem(menuItem);
return true;
}
private void selectDrawerItem(MenuItem menuItem)
{
Fragment fragment = null;
Class fragmentClass = null;
switch (menuItem.getItemId())
{
case R.id.fragment_home:
fragmentClass = com.wahyuapps.benux.fragment.fragmentHome.class;
break;
case R.id.fragment_distro_linux:
fragmentClass = com.wahyuapps.benux.fragment.fragmentDistroLinux.class;
break;
}
try {
fragment = (Fragment)fragmentClass.newInstance();
}catch(Exception e){
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_content, fragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
drawerLayout.closeDrawers();
}
});
}
我在这上面写了什么代码? 抽屉关闭时如何打开碎片? 在我的情况下,我的片段在点击项目后打开了。我想在抽屉关闭后打开一些延迟片段。
答案 0 :(得分:0)
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
TestingFragment hello = new TestingFragment();
fragmentTransaction.add(R.id.mainlayout, hello, "HELLO");
fragmentTransaction.commit();
这样你可以调用片段......而R.id.mainlayout是你活动的根布局。
修改2
在延迟一段时间后打开片段
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_content, fragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
}
}, 1000);
为处理程序选择此导入 - import android.os.Handler;
答案 1 :(得分:0)
我认为你想延迟打开片段。如果您需要在抽屉关闭后进行片段转换。然后,您可以通过延迟过渡来实现这一目标。
您的代码应该是这样的。
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.tentang:
break;
case R.id.keluar:
finish();
break;
}
selectDrawerItem(menuItem);
return true;
}
private void selectDrawerItem(MenuItem menuItem)
{
Fragment fragment = null;
Class fragmentClass = null;
switch (menuItem.getItemId())
{
case R.id.fragment_home:
fragmentClass = com.wahyuapps.benux.fragment.fragmentHome.class;
break;
case R.id.fragment_distro_linux:
fragmentClass = com.wahyuapps.benux.fragment.fragmentDistroLinux.class;
break;
}
try {
fragment = (Fragment)fragmentClass.newInstance();
}catch(Exception e){
e.printStackTrace();
}
changeFragment(fragment);
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
drawerLayout.closeDrawers();
}
});
创建一种方法来更改片段。
public void changeFragment(final Fragment fragment){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_content, fragment).commit();
}
}, 400);
}
我刚刚过渡了400毫秒的延迟。根据你的意愿改变它。
希望有所帮助:)