How to open a different activity on recyclerView item onclick
Simply you can do it easy... You just need to get the context of your activity, here, from your View.
//Create intent getting the context of your View and the class where you want to go
Intent intent = new Intent(view.getContext(), YourClass.class);
//start the activity from the view/context
view.getContext().startActivity(intent); //If you are inside activity, otherwise pass context to this funtion
Remember that you need to modify AndroidManifest.xml and place the activity...
<activity
android:name=".YourClass"
android:label="Label for your activity"></activity>
You can (but don't need to because the ViewHolder
class is not static) create field context as is shown below:
private final Context context;
public MyViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
...
}
and on your onClick
method just call sth like below:
@Override
public void onClick(View v) {
final Intent intent;
switch (getAdapterPostion()){
case 0:
intent = new Intent(context, FirstActivity.class);
break;
case 1:
intent = new Intent(context, SecondActivity.class);
break;
...
default:
intent = new Intent(context, DefaultActivity.class);
break;
}
context.startActivity(intent);
}
or
@Override
public void onClick(View v) {
final Intent intent;
if (getAdapterPosition() == sth){
intent = new Intent(context, OneActivity.class);
} else if (getPosition() == sth2){
intent = new Intent(context, SecondActivity.class);
} else {
intent = new Intent(context, DifferentActivity.class);
}
context.startActivity(intent);
}
you can implement your adapter's onClickListener:
public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder>implements View.OnClickListener
and use interface with method in it
public interface mClickListener {
public void mClick(View v, int position);
}
and in your onClick method call the method in the interface and pass it the view and position
in your main activity implement that interface
public class MainActivity extends ActionBarActivity implements AdapterClass.mClickListener
and override that method
@Override
public void onCommentsClick(View v, int position) {
final Intent intent = new Intent(this, OtherActivity.class);
}
as its better to manage your activity transition by the activity not other classes