Navigation Drawer: setNavigationItemSelectedListener is not working.

有些话、适合烂在心里 提交于 2019-12-24 09:26:40

问题


Here, This is my implementation in java:

 import static com.example.krsnv.kooc.R.id.nav_gro;

 public class Displayprofile extends AppCompatActivity {


private FirebaseRecyclerAdapter<User, userholder> mAdapter;
RecyclerView mRecyclerView;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.displayprofile);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerlayout) ;
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
   // getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            int id = menuItem.getItemId();
            if (id == R.id.nav_account) {
                Toast.makeText(Displayprofile.this, "my account",Toast.LENGTH_LONG).show();

            }
            else if (id == R.id.nav_settings)
            {
                Toast.makeText(Displayprofile.this,"settings",Toast.LENGTH_SHORT).show();

            }
            else if (id == R.id.nav_logout)
            {
                Toast.makeText(Displayprofile.this,"logout",Toast.LENGTH_SHORT).show();

            }
            else if (id == nav_gro)
            {
                Toast.makeText(Displayprofile.this,"groceries",Toast.LENGTH_SHORT).show();

            }
            return true;
        }
    } );



    DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("workers");
    mRecyclerView = (RecyclerView) findViewById(R.id.userrecycler);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    mAdapter = new FirebaseRecyclerAdapter<User, userholder>(User.class, android.R.layout.two_line_list_item, userholder.class, mRef) {
        @Override
        public void populateViewHolder(userholder userViewHolder, User users, int position) {
            userViewHolder.setName(users.getName());
            userViewHolder.setText(users.getPhone());
        }
    };
    mRecyclerView.setAdapter(mAdapter);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();





}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mToggle.onOptionsItemSelected(item))
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mAdapter.cleanup();
}
public static class userholder extends RecyclerView.ViewHolder {
    private final TextView mNameField;
    private final TextView mTextField;

    public userholder(View itemView) {
        super(itemView);
        mNameField = (TextView) itemView.findViewById(android.R.id.text1);
        mTextField = (TextView) itemView.findViewById(android.R.id.text2);
    }

    public void setName(String name) {
        mNameField.setText(name);
    }

    public void setText(String text) {
        mTextField.setText(text);
    }
}







}

The xml code is given below:

>
<ScrollView
     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"
android:background="#cecece"
>


        <LinearLayout 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"
            android:padding="40dp"
            android:orientation="vertical"

            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="3"
                android:layout_gravity="center"
                android:gravity="center"
                android:textSize="18sp"
                android:text="find chef's near you\n"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/userrecycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:padding="16dp"
                />

        </LinearLayout>





    </ScrollView>
     <android.support.design.widget.NavigationView
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     app:menu="@menu/navigation_menu"
     android:layout_gravity="start"
      app:headerLayout="@layout/navigation_header"
       android:id="@+id/nav_view">
     </android.support.design.widget.NavigationView>

    </android.support.v4.widget.DrawerLayout>

When I click an item in the navigation drawer it doesn't show the Toast message. Here, setNavigationItemSelectedListener is not working So, what can be done to solve this issue ?


回答1:


In order to use this setNavigationItemSelectedListener you should have menu as give below in res->menu folder

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/menu_payment"
    android:checked="false"
    android:icon="@drawable/ic_action_payment"
    android:title="@string/menu_payment" />

<item
    android:id="@+id/menu_history"
    android:checked="false"
    android:icon="@drawable/ic_action_history"
    android:title="@string/menu_history" />
</menu>



回答2:


Add this

 @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.navigation_menu, menu);
            return true;
        }


来源:https://stackoverflow.com/questions/40970755/navigation-drawer-setnavigationitemselectedlistener-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!