I have a navigation drawer that has a RecyclerView inside of it. When clicking on an item in the RecyclerView I successfully can navigate to my desired activity using this 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.
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. Consider setting a clicklistener to this
in the constructor of the viewholder. You can use ViewHolder.getAdapterPosition()
in the listener.
To answer the question, you already pass a Context
to the adapter, why not also pass it (and the viewholder) the navigation drawer?
get reference of your drawer layout and add the below line before mContext.startActivity(intent); drawerLayout.closeDrawers();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.