简体   繁体   中英

Calling activity from ViewHolder in RecyclerView?

I've got a RecyclerView which is loading the content of my String arrays which works fine, however I want to open a new activity depending on which view they have pressed.

What I have done is created an array called classes like so:

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

These 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've managed to add the OnClickListener to the ViewHolder and output what class is assigned to each view to the log, however I cannot figure out or get working, how to launch another activity.

The class name would 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;
    }

}

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

By 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 have tested, the method mentioned in the blog worked for me.

Intent intent = new Intent(viewObject.getContext(),ActivityName.class);
viewObject.getContext().startActivity(intent);

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.

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