Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling activity from ViewHolder in RecyclerView?

Tags:

java

android

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;
    }

}
like image 284
K20GH Avatar asked Sep 05 '14 14:09

K20GH


People also ask

What is ViewHolder in RecyclerView?

A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive findViewById results. While LayoutParams belong to the LayoutManager , ViewHolders belong to the adapter.

What is the relationship between RecyclerView adapter and RecyclerView ViewHolder?

Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView . A class that extends ViewHolder that will be used by the adapter.

When onCreateViewHolder is Called?

onCreateViewHolder is called when you need a new View. If there is an available Recycled View that can be provided and be bound with new data, then onBindViewHolder is called :) 96.


2 Answers

Intent intent = new Intent(viewObject.getContext(),ActivityName.class);
viewObject.getContext().startActivity(intent);
like image 153
r j Avatar answered Oct 05 '22 20:10

r j


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.

like image 25
paradite Avatar answered Oct 05 '22 18:10

paradite