- 我是新手,我正在学习使用Android Studio导航抽屉活动模板代码(mainMenu.java) 开始这个项目
- 我创建了另一个空活动(viewProfile.java),以便在用户按导航抽屉菜单时调用它。示例:用户按'查看个人资料'在菜单里面。
- 我需要有关如何在我的所有活动中使用单个Navi Drawer菜单的帮助,例如viewProfile.java Activity。
- 尝试从mainMenu.java复制与Navi Drawer相关的所有代码并粘贴到viewProfile.java中。遗憾的是,根本不起作用。
- 我评论了一些我尝试过的代码。
mainMenu.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;
}
}
viewProfile.java
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;
}
}
答案 0 :(得分:0)
建议在这种情况下使用多个片段而不是活动。但即使您仍想使用不同的活动,也只需在其他活动中包含导航抽屉,就像在其中一个活动中实施一样。然而,这导致多次写入同一段代码,应该避免这种情况。
答案 1 :(得分:0)
使用片段和容器,这样如果您想更改布局,您只能更改布局的一部分而不是整个布局。因此,您的导航抽屉在您的应用中保持安全。
答案 2 :(得分:0)
你应该为此使用片段,你可以为此获得大量的演示..如果你还想使用活动,你必须在每个布局中包含nevigation
<?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);
}
所以你必须在每个活动中实现这个方法,并通过调用任何活动使它们完成..