多个活动中的导航抽屉 [英] Navigation drawer in multiple activities
问题描述
我知道这个问题已经被问过多次了.但是我的担心是不同的.
I know this question has been asked multiple times. But my concern is different.
使用先生. Tamada教程我创建了一个NavigaionActivity
和多个fragments
来替换FrameLayout
.一切正常.
Using Mr. Tamada Tutorial I've created a NavigaionActivity
and multiple fragments
to replace in FrameLayout
. It's working fine.
现在,在片段中选择任何选项之后,将打开另一个活动.
Now, after selecting any option in a fragment, another activity gets open.
我要在该活动中使用相同的导航菜单.
导航视图-片段-活动(在此处显示导航视图)
Navigation view -- fragments -- Activity (display navigation view here)
我尝试过的事情:
-
在该活动中使用显示
Navigation view
的xml
代码. (DrawerLayout,CoordinatorLayout,AppBarLayout等)
use the
xml
code of displayingNavigation view
in that activity. (DrawerLayout, CoordinatorLayout, AppBarLayout etc)
然后在Activity.java中,单击转向导航活动的菜单项.
Then in Activity.java, on click of menu item diverting to the Navigation Activity.
代码:
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. It is doing the task but code isn't re-usable
- 为导航创建一个单独的布局文件,并将其包含在活动"中.但是,这取代了主要内容.在此仅包含导航是可见的.
还有其他方法吗?
推荐答案
下降投票者-解决方案是..
Downvoters - here the solution is..
- 创建一个包含
DrawerLayout
和NavigationView
的xml
文件(一个人可以使用Question中给出的xml,而没有主要内容)-navigation.xml
-
如许多答案中所建议,创建一个
extends
AppCompatActivity的BaseActivity.然后对navigation.xml
进行充气.
- Create an
xml
file which will haveDrawerLayout
andNavigationView
(one can use the xml given in Question, without the main content) -navigation.xml
As suggested in many answers "create a BaseActivity which
extends
AppCompatActivity. And inflatenavigation.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);
...... }}
要使用此NavigationMenu
的任何Activity
,请为该类扩展BaseActivity.
In whichever Activity
you wanna use this NavigationMenu
, extend BaseActivity for that class.
GraphActivity extends BaseActivity { .... }
在GraphActivity.xml
中添加NavigationMenu
代码.您不能只包含navigation.xml
它会禁用当前的xml小部件.
In GraphActivity.xml
add the NavigationMenu
code. You can't just include the navigation.xml
it will disable the current xml widgets.
完成!
这篇关于多个活动中的导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!