RecyclerView项目时如何关闭导航抽屉

How to close navigation drawer when RecyclerView items

我在菜单中使用了 recyclerView。我希望在单击 RecyclerView 中的项目时关闭菜单。目前,当我点击该项目时,菜单不会关闭。我应该怎么做才能关闭菜单?我正在使用 NavigationAdapter。我用 NavigationAdapter 添加了 recyclerview 的数据。我在 MainActivity 中显示了菜单。

导航适配器

public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter.ViewHolder> {

    Context context;
    ArrayList<NavigationDataModel> arrayList = new ArrayList<>();

    public NavigationAdapter(Context context, ArrayList<NavigationDataModel> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.navigationrecyclerview_adapter11, parent, false);



        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {

        holder.navIcon.setImageResource(arrayList.get(position).getIcon());
        holder.rootView.setBackgroundColor(arrayList.get(position).getColor());
        holder.navTitle.setText(arrayList.get(position).getTitle());

        holder.navTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
if(String.valueOf(position).equals("0")){
    AppCompatActivity activity = (AppCompatActivity) v.getContext();
    Fragment myFragment = new HesabimFragment();
    activity.getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, myFragment).addToBackStack(null).commit();
}

                if(String.valueOf(position).equals("1")){
                    AppCompatActivity activity = (AppCompatActivity) v.getContext();
                    Fragment myFragment = new SSSFragment();
                    activity.getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, myFragment).addToBackStack(null).commit();
                }

            }
        });

    }


    @Override
    public int getItemCount() {
        return arrayList.size();
    }



    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView navIcon;
        TextView navTitle;
        RelativeLayout rootView;

        public ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);
            navIcon = itemView.findViewById(R.id.navIcon);
            navTitle = itemView.findViewById(R.id.navTitle);

        }
    }

MainActivity

public class Main2Activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
 RecyclerView navRecyclerView;
    LinearLayoutManager layoutManager;
    ArrayList<NavigationDataModel> arrayList = new ArrayList<>();
    NavigationAdapter adapter;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);    

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();


        navigationView.setNavigationItemSelectedListener(this);

        navRecyclerView = findViewById(R.id.navRecyclerView);
        navRecyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        navRecyclerView.setLayoutManager(layoutManager);

        DividerItemDecoration divider = new DividerItemDecoration(
                navRecyclerView.getContext(),
                DividerItemDecoration.VERTICAL
        );
        divider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.diveder_recycler));
        navRecyclerView.addItemDecoration(divider);

        initArray();

        adapter = new NavigationAdapter(this, arrayList);
        navRecyclerView.setAdapter(adapter);}

 private void initArray() {

        NavigationDataModel model = new NavigationDataModel();
        model.setIcon(R.mipmap.avatar);
        model.setTitle("Hesabım");
        arrayList.add(model);

        NavigationDataModel model2 = new NavigationDataModel();
        model2.setIcon(R.mipmap.sss3);
        model2.setTitle("S.S.S");
        arrayList.add(model2);
}

 @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mDrawerLayout.closeDrawer(GravityCompat.END, false);

        return true;
    }

您可以在 activity Main2Activity:

中创建一个方法
public void onSideMenuClick() {
          //place your closeDrawer code here
        DrawerLayout mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
        mDrawerLayout.closeDrawer(GravityCompat.END, false);
  }

在单击适配器中的菜单项时调用该方法:

((Main2Activity) context).onSideMenuClick();

希望这会有所帮助!!

创建界面 NavClick

     public interface NavClick {
           public void onNavClick(int position); //position for determining which item is clicked
       }

首先,在NavigationAdapter中声明NavClick的对象

    NavClick navClick

然后像这样更新你的构造函数

    public NavigationAdapter(Context context, ArrayList<NavigationDataModel> arrayList,NavClick navClickEvent) {
    this.context = context;
    this.arrayList = arrayList;
    this.navClick = navClickEvent;
}

然后在onBindViewHolder中

   holder.navTitle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         navClick.onNavClick(position)
         }

最后修改 NavAdapter 对象声明

       adapter = new NavigationAdapter(this, arrayList,new NavClick(){
             @Overide
             public void onNavClick(int position){
               //Now you can add your condition which to call when clicking
                 }
                 } );