[英]Navigation drawer in multiple activities
I know this question has been asked multiple times. 我知道这个问题已经被问过多次了。 But my concern is different.
但是我的担心是不同的。
Using Mr. Tamada Tutorial I've created a NavigaionActivity
and multiple fragments
to replace in FrameLayout
. 使用Tamada教程,我创建了一个
NavigaionActivity
和多个要替换在FrameLayout
fragments
。 It's working fine. 一切正常。
Now, after selecting any option in a fragment, another activity gets open. 现在,在片段中选择任何选项之后,将打开另一个活动。
I want the same Navigation menu in that Activity. 我想要该活动中的相同导航菜单。
Navigation view -- fragments -- Activity (display navigation view here)
导航视图-片段-活动(在此处显示导航视图)
What I tried: 我试过的
use the xml
code of displaying Navigation view
in that activity. 在该活动中使用显示
Navigation view
的xml
代码。 (DrawerLayout, CoordinatorLayout, AppBarLayout etc) (DrawerLayout,CoordinatorLayout,AppBarLayout等)
Then in Activity.java, on click of menu item diverting to the Navigation Activity. 然后,在Activity.java中,单击转向“导航活动”的菜单项。
Code : 代码 :
XML: XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
....>
<android.support.design.widget.CoordinatorLayout
....>
<android.support.design.widget.AppBarLayout
....>
<android.support.v7.widget.Toolbar
.... />
</android.support.design.widget.AppBarLayout>
<LinearLayout
.... /> <!-- main content of this Acitivity-->
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
.... />
</android.support.v4.widget.DrawerLayout>
Activity.java: Activity.java:
public void dashboard(MenuItem item) {
Bundle bundle = new Bundle();
bundle.putString("fragment", Constant.DASHBOARD_FRAGMENT);
UtilityClass.newActivity(this, NavigationActivity.class, bundle);
}
And handling the call on Navigation Activity. 并处理对Navigation Activity的调用。 It is doing the task but code isn't re-usable
它正在执行任务,但是代码不可重用
Is there any other way to do it? 还有其他方法吗?
Downvoters - here the solution is.. 拒绝投票者-这里的解决方案是..
xml
file which will have DrawerLayout
and NavigationView
(one can use the xml given in Question, without the main content) - navigation.xml
xml
这将有文件DrawerLayout
和NavigationView
(可以使用在问题中给出的XML,而主要内容) - navigation.xml
As suggested in many answers "create a BaseActivity which extends
AppCompatActivity. And inflate navigation.xml
. 正如许多答案中所建议的那样,“创建
extends
AppCompatActivity的BaseActivity。并为navigation.xml
充气。
public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View view = View.inflate(this, R.layout.navigation, null); // view declarations DrawerLayout drawer = (DrawerLayout) view.findViewById(R.id.drawer_layout); NavigationView navigationView = (NavigationView) view.findViewById(R.id.nav_view); ...... }}
In whichever Activity
you wanna use this NavigationMenu
, extend BaseActivity for that class. 在任何一个
Activity
,你想用这个NavigationMenu
,延长BaseActivity该类。
GraphActivity extends BaseActivity { .... }
In GraphActivity.xml
add the NavigationMenu
code. 在
GraphActivity.xml
添加NavigationMenu
代码。 You can't just include the navigation.xml
it will disable the current xml widgets. 您不能只包含
navigation.xml
它会禁用当前的xml小部件。
Done! 做完了!
Try this: 尝试这个:
public class MainActivity extends BaseActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_describtion);
getLayoutInflater().inflate(R.layout.activity_main, frameLayout);
mDrawerList.setItemChecked(position, true);
}
Instead use Google recommended MasterDetailFlow Design 而是使用Google推荐的MasterDetailFlow设计
You can read how to implement this on 您可以阅读如何实施
https://inducesmile.com/android/android-fragment-masterdetail-flow-tutorial-in-android-studio/ https://inducesmile.com/android/android-fragment-masterdetail-flow-tutorial-in-android-studio/
NavigationDrawer令人垂涎。...使用其他东西因为使用起来不太舒服。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.