Java – call the activity from viewholder in recyclerview?
•
Java
I have a recyclerview that loads the contents of my string array and works normally, but I want to open a new activity according to the view they pressed
What I do is create an array called class, as shown below:
<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:
String[] classes = resource.getStringArray(R.array.classes); MainActivityList adapter = new MainActivityList(titles,content,images,classes); recyclerView.setAdapter(adapter);
I've tried to add onclicklistener to viewholder and output the classes assigned to the log for each view, but I can't figure out or start working on how to start another activity
For example, the class name would be similar to classone class
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;
}
}
Solution
I'm also looking for solutions. I found this article:
By using the OnItemClickListener interface, you should be able to invoke startActivity. in the 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 have tested the method mentioned in the blog for me
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
