想要使用片段从导航抽屉活动移动到另一个屏幕,以在所有屏幕上显示抽屉
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了想要使用片段从导航抽屉活动移动到另一个屏幕,以在所有屏幕上显示抽屉相关的知识,希望对你有一定的参考价值。
我正在尝试最后两天在我的抽屉活动旁边添加片段,以便在整个应用程序中看到导航抽屉。我已尝试过stackoverflow和其他许多方法,但仍然没有成功。然后我必须从第一个片段移动到第二个片段,依此类推,直到需要导航抽屉。当我从我的活动移动到任何片段时,我想替换除抽屉之外的整个视图。每个片段都有自己的layout.xml,就像一个活动(线性/相对布局作为父级)。
抽屉活动:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Button btnfragOne = (Button) findViewById(R.id.btnfrag_one);
btnfragOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragOne fragment = new FragOne();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frag2, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
});
}
1st Fragment类:
public class FragOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_one, container, false);
}
// 2nd Fragment类:
public class FragTwo extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_two, container, false);
}
答案
只需执行此任务,您必须覆盖onNavigationItemSelected
中的Activity
方法,该方法返回NavigationDrawer
上所选片段的id。
试试这个,
@Override
public boolean onNavigationItemSelected(MenuItem item) {
//calling the method displayselectedscreen and passing the id of selected menu
displaySelectedFragment(item.getItemId());
return true;
}
现在displaySelectedFragment
,
private void displaySelectedScreen(int itemId) {
//creating fragment object
Fragment fragment = null;
//initializing the fragment object which is selected
switch (itemId) {
case R.id.your_fragment_one_id:
fragment = new FragOne();
break;
case R.id.your_fragment_two_id:
fragment = new FragTwo();
break;
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.main_layout_id_which_is_to_be_replace, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.your_drawer_layout_id);
drawer.closeDrawer(GravityCompat.START);
}
编辑 - 如果你想从FragOne
导航到FragTwo
。试试这个,
在你的Activity
中创建一个方法,
public void showFragTwo(){
FragmentManager manager = getSupportFragmentManager();
FragTwo frag = new FragTwo();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.your_layout_id_which_is_to_be_replace, frag);
transaction.commit();
}
然后在你的FragOne
当你想要启动FragTwo
时,从startFragTwo
调用Activity
方法,
((YourActivity) getActivity()).showFragTwo();
以上是关于想要使用片段从导航抽屉活动移动到另一个屏幕,以在所有屏幕上显示抽屉的主要内容,如果未能解决你的问题,请参考以下文章