我正在创建一个应用程序,它需要所有活动的相同导航抽屉。为此,我创建了一个扩展Activity(需要子类)并在那里编写Navigation Drawer代码的类。但不适用于setOnItemClickListener
这项工作对我来说
public class MyDrawer extends AppCompatActivity {
ActionBarDrawerToggle toggle;
protected RelativeLayout fullLayout;
protected FrameLayout frameLayout;
@Override
public void setContentView(final int layoutResID) {
fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.mydrawer, null);
frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);
getLayoutInflater().inflate(layoutResID, frameLayout, true);
super.setContentView(fullLayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout3);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
drawer.closeDrawers();
int itemId = menuItem.getItemId();
Toast.makeText(getApplicationContext(), menuItem.getTitle().toString(),
Toast.LENGTH_LONG).show();
//navigationView.getMenu().findItem(R.id.drawer_5_reasons).setChecked(true);
return true;
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (toggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/drawer_framelayout">
<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_layout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>
<FrameLayout
android:id="@+id/drawer_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main2"
app:menu="@menu/activity_main_drawer"
android:background="#fefefd" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
使用:
public class yourclass extends MyDrawer {
但
中没有找到项目 listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
答案 0 :(得分:1)
我不明白listView与导航抽屉有什么关系。 如果您尝试在导航抽屉的项目上设置onItemClickListener,则应在setNavigationItemSelectedListener中设置它并执行您想要的任何操作OnNavigationItemSelected()。
private void setUpDrawerContent(NavigationView navigationView){
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
doWhateverYouWant(menuItem);
return true;
}
});
}
由于导航抽屉对于扩展MyDrawer类的所有活动都是相同的,我假设所有活动的功能都相同。如果没有,您可以在子活动中调用相同的方法(setUpDrawerContent(NavigationView nv))。