简体   繁体   English

单击导航抽屉中RecyclerView中的项目后如何关闭导航抽屉

[英]How to close the navigation drawer after clicking on item in RecyclerView that is in the nav drawer

I have a navigation drawer that has a RecyclerView inside of it. 我有一个里面有RecyclerView的导航抽屉。 When clicking on an item in the RecyclerView I successfully can navigate to my desired activity using this RecyclerAdapter: 当单击RecyclerView中的项目时,我可以使用以下RecyclerAdapter成功导航到所需的活动:

public class AdapterNavigation extends RecyclerView.Adapter<AdapterNavigation.Holder> {

    private List<NavigationItem> mItemList;
    private Context mContext;


    //this class takes a context and a list of the items you want to populate into the recycler view
    public AdapterNavigation(Context context, List<NavigationItem> itemList) {
        mItemList = itemList;
        mContext = context;


    }
    @Override
    public Holder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        //our xml showing how one row looks
        View row = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view_navigation, viewGroup, false);


        Holder holder = new Holder(row);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        holder.recyclerNavigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Intent intent;
                switch (position){
                    //specify what activity will launch on click
                    case 0:
                        intent =  new Intent(mContext, Maintenance.class);
                        break;
                    case 1:
                        intent =  new Intent(mContext, Contact.class);
                        break;
                    default:
                        intent =  new Intent(mContext, MainActivity.class);
                        break;
                }

                mContext.startActivity(intent);

            }
        });

        NavigationItem item = mItemList.get(position);

        holder.iconImageView.setImageResource(item.getIcon());
        holder.titleTextView.setText(item.getTitle());
        holder.countTextView.setText(Integer.toString(item.getCount()));


    }

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

    public class Holder extends RecyclerView.ViewHolder {
        protected ImageView iconImageView;
        protected TextView titleTextView;
        protected TextView countTextView;
        protected RelativeLayout recyclerNavigation;

        public Holder(View view) {
            super(view);
            iconImageView = (ImageView) view.findViewById(R.id.iconImageView);
            titleTextView = (TextView) view.findViewById(R.id.titleTextView);
            countTextView = (TextView) view.findViewById(R.id.countTextView);
            //the whole view
            recyclerNavigation = (RelativeLayout) view.findViewById(R.id.recyclerNavigation);
        }

    }



}

Now what I want to do is to have the navigation drawer close right before an item is clicked. 现在,我要做的就是在单击某项之前关闭导航抽屉。 The way it looks when you click an item in the navigation menu in the android Gmail app. 当您在android Gmail应用程序的导航菜单中单击某个项目时,它的外观。

So right before: 因此,在此之前:

mContext.startActivity(intent);

I would close the navigation drawer. 我将关闭导航抽屉。

The issue is getting a reference to that navigation drawer. 问题是获得对该导航抽屉的引用。 I was considering getting the view from the context I have because that is all I have. 我正在考虑从我所拥有的上下文中获取观点,因为这就是我所拥有的。

Is there a way to do this or would I have to change some of my code? 有没有办法做到这一点,还是我必须更改一些代码?

First of all, you are creating a clicklistener every time a viewholder is bound. 首先,每次绑定一个Viewholder时都将创建一个clicklistener。 Consider setting a clicklistener to this in the constructor of the viewholder. 考虑在查看器的构造函数中this设置一个clicklistener。 You can use ViewHolder.getAdapterPosition() in the listener. 您可以在侦听器中使用ViewHolder.getAdapterPosition()

To answer the question, you already pass a Context to the adapter, why not also pass it (and the viewholder) the navigation drawer? 要回答这个问题,您已经将Context传递给了适配器,为什么不同时将其(和视图持有者)传递给导航抽屉呢?

get reference of your drawer layout and add the below line before mContext.startActivity(intent); 获取抽屉布局的参考,并在mContext.startActivity(intent)之前添加以下行; drawerLayout.closeDrawers(); cabinetLayout.closeDrawers();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM