我正在尝试在导航抽屉项上设置OnClickListener,但似乎无法解决。从抽屉中单击项时,应打开一个新的意图。 我什至试图逐步解决这个视频问题。
https://www.youtube.com/watch?v=M_4Oh2FeRYs
我是android的新手,所以我似乎找不到该bug。
这是我的Java代码:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private ActionBarDrawerToggle mToggle;
private DrawerLayout mDrawerLayout;
private NavigationView navView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = findViewById(R.id.gridView);
//Drawer
DrawerLayout mDrawerLayout = findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.drawer_open,R.string.drawer_close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navView = findViewById(R.id.navView);
navView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
mDrawerLayout.closeDrawer(GravityCompat.START);
}
else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.nav2:
Toast.makeText(MainActivity.this, "nav1 selected", Toast.LENGTH_SHORT).show();
Intent i = new Intent(MainActivity.this, new.class);
startActivity(i);
break;
case R.id.nav1:
Toast.makeText(MainActivity.this, "nav2 selected", Toast.LENGTH_SHORT).show();
Intent j = new Intent(MainActivity.this, new.class);
startActivity(j);
break;
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
这是navigation_menu布局文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav1"
android:title="@string/nav1"
android:icon="@mipmap/nav2"
/>
<item
android:id="@+id/nav2"
android:title="@string/nav2"
android:icon="@mipmap/nav1"/>
</menu>
这是mainactivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:nestedScrollingEnabled="true"
tools:context="com.example.xxx.xxx.MainActivity"
app:layout_behavior="android.support.design.widget.AppBarLayout.ScrollingViewBehaviour">
<LinearLayout
android:orientation="vertical"
android:background="#ffff"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent"
tools:context="com.example.xxx.xxx.MainActivity"
android:isScrollContainer="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#ff77"
android:layout_marginTop="5dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/banner1"
android:src="@drawable/banner1"
android:scaleType="fitXY"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"/>
</LinearLayout
<GridView
android:id="@+id/gridView"
android:nestedScrollingEnabled="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff"
android:numColumns="3"
android:padding="2dp"
android:gravity="center"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp">
</GridView>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navView"
android:layout_width="200dp"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
android:layout_gravity="start"
android:alpha="0.4"
android:fitsSystemWindows="true"/>
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:0)
尝试一下
public class Main extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private ActionBarDrawerToggle mToggle;
private DrawerLayout mDrawerLayout;
private NavigationView navView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Drawer
mDrawerLayout = findViewById(R.id.drawer_layout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navView = findViewById(R.id.nav_view);
navView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
mDrawerLayout.closeDrawer(GravityCompat.START);
}
else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.nav_camera:
Toast.makeText(Main.this, "nav1 selected", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Main.this, test.class);
startActivity(i);
break;
case R.id.nav_gallery:
/* Toast.makeText(Main.this, "nav2 selected", Toast.LENGTH_SHORT).show();
Intent j = new Intent(Main.this, new.class);
startActivity(j);*/
break;
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
为我工作 要么 转到文件>新建>活动(最后一部分)>导航抽屉活动 它将为您的项目添加导航抽屉类
答案 1 :(得分:0)
问题是我需要将其余布局放在FrameLayout中,此链接很有帮助。
https://developer.android.com/training/implementing-navigation/nav-drawer
因此只需要更改mainactivity.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:nestedScrollingEnabled="true"
tools:context="com.example.xxx.xxx.MainActivity"
app:layout_behavior="android.support.design.widget.AppBarLayout.ScrollingViewBehaviour">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frameLayout">
<LinearLayout
android:orientation="vertical"
android:background="#ffff"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent"
tools:context="com.example.xxx.xxx.MainActivity"
android:isScrollContainer="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#ff77"
android:layout_marginTop="5dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/banner1"
android:src="@drawable/banner1"
android:scaleType="fitXY"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"/>
</LinearLayout
<GridView
android:id="@+id/gridView"
android:nestedScrollingEnabled="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff"
android:numColumns="3"
android:padding="2dp"
android:gravity="center"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp">
</GridView>
</LinearLayout>
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navView"
android:layout_width="200dp"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
android:layout_gravity="start"
android:alpha="0.4"
android:fitsSystemWindows="true"/>
</android.support.v4.widget.DrawerLayout>