[英]Working with Navigation Drawer in multiple activities?
- 我是新手,正在学习使用 Android Studio Navigation Drawer Activity Template 代码 (mainMenu.java) 来启动这个项目
- 我创建了另一个 Empty Activity (viewProfile.java),以便在用户按下 Navigation Drawer 菜单时可以调用它。 示例:用户按菜单内的“查看个人资料”。
- 我需要有关如何在我的所有活动中使用单个 Navi Drawer 菜单的帮助,例如 viewProfile.java Activity。
- 尝试从 mainMenu.java 复制与 Navi Drawer 相关的所有代码并粘贴到 viewProfile.java 中。 根本不起作用,很遗憾。
- 我已经评论了我尝试过的一些代码。
主菜单
public class mainMenu extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//Defining views
private TextView editTextUserName;
private TextView textView_profile_name;
private TextView textView_profile_email;
private TextView textView_profile_amount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
Toast.makeText(this, "nav_profile", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_listing) {
startActivity(new Intent(this, ViewAllStock.class));
} else if (id == R.id.nav_history) {
Toast.makeText(this, "nav_history", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_coming) {
Toast.makeText(this, "nav_coming", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_logout) {
logout();
} else if (id == R.id.nav_setting) {
Toast.makeText(this, "nav_setting", Toast.LENGTH_SHORT).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
视图配置文件
public class viewProfile extends AppCompatActivity implements ListView.OnItemClickListener, NavigationView.OnNavigationItemSelectedListener {
private ListView listView;
private String JSON_STRING;
private SwipyRefreshLayout mSwipyRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_all_stock);
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
//
// 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.setDrawerListener(toggle);
// toggle.syncState();
//
// NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
// navigationView.setNavigationItemSelectedListener(ViewAllStock.this);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
// Handle the camera action
Toast.makeText(ViewAllStock.this, "nav_profile", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_listing) {
Toast.makeText(ViewAllStock.this, "nav_listing", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_history) {
Toast.makeText(ViewAllStock.this, "nav_history", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_coming) {
Toast.makeText(ViewAllStock.this, "nav_coming", Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_logout) {
Toast.makeText(ViewAllStock.this, "nav_logout", Toast.LENGTH_SHORT).show();;
} else if (id == R.id.nav_setting) {
Toast.makeText(ViewAllStock.this, "nav_setting", Toast.LENGTH_SHORT).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
在这种情况下,建议使用多个片段而不是活动。 但是,即使您仍然想使用不同的活动,也只需在其他活动中包含导航抽屉,就像您在其中一个活动中实施的方式一样。 然而,这会导致多次编写同一段代码,这是应该避免的。
使用片段和容器,这样如果你想改变布局,你只能改变布局的一部分而不是整个布局。所以你的导航抽屉在你的应用程序中保持安全。
你应该为此使用片段,你可以获得很多演示。如果你仍然想使用活动,你必须在每个布局中包含导航
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:background="@drawable/bg"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<include
android:id="@+id/toolbar_offr"
layout="@layout/toolbar_offer" />
</LinearLayout>
<ListView
android:id="@+id/OfferFragLV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ll1">
</ListView>
</RelativeLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.phoenix.spicejunction.frag.FragmentDrawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_below="@id/mainRL"
android:layout_gravity="start"
android:layout_marginTop="50dp"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
这是我的 fragment_navigation_drawer 布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:layout_marginTop="40dp"
>
<RelativeLayout
android:id="@+id/nav_header_container"
android:layout_width="match_parent"
android:layout_height="145dp"
android:layout_alignParentTop="true"
android:background="#fff"
android:gravity="center" >
<com.phoenix.spicejunction.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profileIV"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/_10sdp"
android:src="@drawable/ic_launcher"
app:civ_border_color="@color/my_yellow"
app:civ_border_width="3dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/profileIV"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="TextView"
android:textColor="#000" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/_5sdp"
android:text="AGE: "
android:textColor="#000" />
</RelativeLayout>
<!--
<android.support.v7.widget.RecyclerView
android:id="@+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nav_header_container"
android:layout_marginTop="15dp" />
-->
<ListView
android:id="@+id/home_listDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/nav_header_container"
android:layout_gravity="start"
android:layout_marginTop="5dp"
android:entries="@array/slider_array" />
</RelativeLayout>
我正在使用这个方法
@Override
public void onDrawerItemSelected(View view, int position) {
base.displayView(position);
}
所以你必须在每个活动中实现这个方法,并通过调用任何活动使它们完成..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.