本文介绍了导航抽屉:setNavigationItemSelectedListener不起作用.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,这是我在Java中的实现:

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);
    }
}







}

xml代码如下:

>
<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>

当我单击导航抽屉中的项目时,它不会显示Toast消息.在这里,setNavigationItemSelectedListener无法正常工作,那么,该怎么办才能解决此问题?

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 ?

推荐答案

<?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>

这篇关于导航抽屉:setNavigationItemSelectedListener不起作用.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-04 22:44