我在Android中制作了一个导航抽屉,我想在其中实现onClick。这是我的主要活动:
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle aToggle;
private Toolbar toolbar;
private RecyclerView recyclerView;
private RecyclerAdapter recyclerAdapter;
private RecyclerView.Adapter adapter;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
aToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.navig, R.string.open, R.string.Close);
navigationView = (NavigationView) findViewById(R.id.nav_view);
mDrawerLayout.addDrawerListener(aToggle);
toolbar = (Toolbar) findViewById(R.id.nav_action);
toolbar.setNavigationIcon(R.drawable.navig);
setSupportActionBar(toolbar);
aToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView.setItemIconTintList(null);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerAdapter = new RecyclerAdapter(getApplicationContext());
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(recyclerAdapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (aToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}}
这是我的活动的XML布局:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alpit.formula2.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="0dp"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="58dp"
android:orientation="vertical"></android.support.v7.widget.RecyclerView>
<android.support.v7.widget.Toolbar
android:id="@+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EF6C00"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark"></android.support.v7.widget.Toolbar>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFA726"
app:menu="@menu/navigation_menu"
app:theme="@style/NavigationTheme">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
这是我的菜单项:
<group
android:id="@+id/gp1"
android:checkableBehavior="single">
<item
android:id="@+id/nav_maths"
android:icon="@drawable/maths"
android:title="Maths" />
<item
android:id="@+id/nav_physics"
android:icon="@drawable/physics"
android:title="Physics" />
<item
android:id="@+id/nav_chem"
android:icon="@drawable/chem"
android:title="Chemistry" />
<item
android:id="@+id/EEE"
android:icon="@drawable/lightbulb"
android:title="Electronics Electrical" />
</group>
<group
android:id="@+id/gp2"
android:checkableBehavior="single">
<item
android:id="@+id/unitconversion"
android:icon="@drawable/unitconversion"
android:title="Unit Conversion" />
<item
android:id="@+id/Scientist"
android:icon="@drawable/scientist"
android:title="Scientist" />
<item
android:id="@+id/favourite"
android:icon="@drawable/favourite"
android:title="Favourite" />
</group>
<group
android:id="@+id/gp3"
android:checkableBehavior="single">
<item
android:id="@+id/Share"
android:icon="@drawable/share"
android:title="Share" />
<item
android:id="@+id/Rate"
android:icon="@drawable/rate"
android:title="Rate" />
<item
android:id="@+id/ads"
android:icon="@drawable/ad"
android:title="Remove Ads" />
<item
android:id="@+id/aboutus"
android:icon="@drawable/aboutus"
android:title="About Us" />
</group>
</menu>
问题是我无法理解如何在导航抽屉上实现onClick
,因为它由我们给出的列表而不是任何listView填充。
如何在导航栏的项目上实施onClick
?
答案 0 :(得分:22)
您需要添加 实现NavigationView.OnNavigationItemSelectedListener
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
并添加方法
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()) {
case R.id.nav_maths: {
//do somthing
break;
}
}
//close navigation drawer
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
设置监听器的方法
private void setNavigationViewListner() {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
onCreate() 中的 调用方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setNavigationViewListener()
}
答案 1 :(得分:1)
以下代码用于向DrawerLayout添加切换
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);
并将侦听器添加到导航菜单项
implements NavigationView.OnNavigationItemSelectedListener
并覆盖以下方法
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if(id == R.id. nav_maths){
//Handle your stuff here
}
}
将以下代码添加到onCreate方法
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
答案 2 :(得分:1)
将 NavigationItemSelectedListener 事件添加到 nav_view
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
使用 NavigationView.OnNavigationItemSelectedListener
实施您的活动并为点击项
添加此代码@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
答案 3 :(得分:1)
中的
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (aToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}}
添加以下行:
switch (item.getItemId()) {
case R.id.nav_maths:
// your logic here.
return true;
case R.id.nav_physics:
//your logic here
return true;
default:
return super.onOptionsItemSelected(item);
}
答案 4 :(得分:1)
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val nav_view: NavigationView = findViewById(R.id.nav_view)
nav_view.setNavigationItemSelectedListener(this)
nav_view.bringToFront();
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.miid_log_out -> Toast.makeText(this, "Working", Toast.LENGTH_SHORT).show()
}
return false
}
}
答案 5 :(得分:0)
第1步:在活动中扩展NavigationView.OnNavigationItemSelectedListener
第二步:
然后将显示一个错误,然后覆盖该方法:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
return false;
}
使用menuItem.getId()
获取ID。并根据需要执行操作。
最后一步:在onCreate navigationView.setNavigationItemSelectedListener(this);
就是这样。
答案 6 :(得分:0)
binding.navigationView.setNavigationItemSelectedListener {
when(it.itemId){
R.id.book -> Toast.makeText(applicationContext, "Booked", Toast.LENGTH_SHORT).show()
}
true
}
答案 7 :(得分:-2)
{{1}}