Invoking activity from ViewHolder in RecyclerView?

I have a RecyclerView that loads the contents of my String arrays that work fine, however I want to open a new action depending on what kind they clicked.

What I did is create an array called such classes:

<array name="classes"> <item>ClassOne</item> <item>ClassTwo</item> <item>ClassThree</item> <item>ClassFour</item> </array> 

They are stored in an array and passed to my MainActivityList adapter below:

 String[] classes = resource.getStringArray(R.array.classes); MainActivityList adapter = new MainActivityList(titles,content, images, classes); recyclerView.setAdapter(adapter); 

I managed to add OnClickListener to the ViewHolder and display the class assigned to each view in the log, however, I cannot understand or do not work how to start another action.

The class name will be something like ClassOne.class, for example

 public class MainActivityList extends RecyclerView.Adapter<MainActivityList.ViewHolder> { private String[] mTitles; private String[] mContent; private String[] mClasses; private TypedArray mImages; private Context context; public MainActivityList(String[] titles, String[] content, TypedArray images, String[] classes) { this.mTitles = titles; this.mContent = content; this.mImages = images; this.mClasses = classes; } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup,int i) { final View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_main_card, viewGroup, false); ViewHolder vh = new ViewHolder(v); v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ViewHolder vh = (ViewHolder)v.getTag(); Log.v("DEBUG", "Clicked" + vh.classes); } }); return vh; } public void onBindViewHolder(ViewHolder holder, int position) { holder.titleView.setText(mTitles[position]); holder.contentView.setText(mContent[position]); holder.imageView.setImageDrawable(mImages.getDrawable(position)); holder.classes = mClasses[position]; } public class ViewHolder extends RecyclerView.ViewHolder { public TextView titleView; public TextView contentView; public ImageView imageView; public String classes; public ViewHolder(View v) { super(v); titleView = (TextView) v.findViewById(R.id.card_title); contentView = (TextView) v.findViewById(R.id.card_content); imageView = (ImageView)v.findViewById(R.id.card_image); v.setTag(this); } } @Override public int getItemCount() { return mTitles.length; } } 
+5
source share
2 answers

I was also looking for a solution, and I found this post: http://venomvendor.blogspot.sg/2014/07/setting-onitemclicklistener-for-recycler-view.html

Using the OnItemClickListener interface, you should be able to call startActivity in your activity.

 adapter.SetOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(View v , int position) { // This is in an Activity so should be able to start new activity, etc. } }); 

Update: I tested, the method mentioned in the blog worked for me.

+10
source
 Intent intent = new Intent(viewObject.getContext(),ActivityName.class); viewObject.getContext().startActivity(intent); 
+8
source

Source: https://habr.com/ru/post/1201918/


All Articles