3

I need to start an activity based on the item a user clicks on a RecyclerView. The code below has the position as a reference. Does anyone knows how to get this done? I need something like Intent intent = new Intent (MainActivity.this, Target.class). The target class changes depending on the item clicked of course.

        mRecyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
                @Override public void onItemClick(View view, int position) {

                    Intent intent = new Intent(MainActivity.this, ???);
                    startActivity(intent);

                }
            })
    );
CBA110
  • 982
  • 2
  • 16
  • 36

4 Answers4

1

Well, how about just putting the proper OnClickListener on each item's View in the RecyclerView? Each OnClickListener could store the target Class that you need for handling the navigation. You can handle this in the binding phase of the RecyclerView's adapter, there's not magic to it.

Zsombor Erdődy-Nagy
  • 16,546
  • 16
  • 74
  • 101
1

Select the target class via position:

mRecyclerView.addOnItemTouchListener(
        new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
            @Override public void onItemClick(View view, int position) {
                switch(position){
                case 0:
                    startActivity(new Intent(MainActivity.this, A.class));
                    break;
                case 1:
                    startActivity(new Intent(MainActivity.this, B.class));
                    break;
                default:
                    break;  
                }
            }
        })
);

Of course, you have to define the mapping from position to target class.

SilentKnight
  • 13,063
  • 18
  • 46
  • 76
0

You have collection of objects (probably ArrayList),try to add Object which has field of Class type, and then get it like this:

                Intent intent = new Intent(MainActivity.this, objects.get(position).getClassField());
                startActivity(intent);
Lester
  • 2,284
  • 4
  • 25
  • 36
0

You just need to put a onclicklistener to your viewholder(contain views).

private  MainActivity mAct;

viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAct.animateActivity(anything);

        }
    });


 public void animateActivity(anything any) {


    Intent i = new Intent(this, AssetDescription.class);
    //Some anitmation if you want 
    startActivity(i);
}
Fasiha
  • 480
  • 3
  • 11