I have been looking for a solution past 2 days and yet no solution worked. So I had to use Intent to restart the activity when item of recyclerview is clicked. Below is the code:
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Context context = view.getContext();
Intent intent = new Intent(context, MyActivity.class);
SharedData.setSelectedCategory(category.getName());
intent.putExtra("category", category.getName());
context.startActivity(intent);
((MyActivity) context).finish();
}
});
and then used below code in the activity to select the 2nd tab like this.
viewPager.setCurrentItem(1, false);
tabLayout.setupWithViewPager(viewPager);
While this works but I still would like to use slide animation to load 2nd tab when 1st tab recyclerview item is clicked. Thank you for your help.
You can use broadcast from onClick to activity, then in an activity you can do something like this viewPager.setCurrentItem(1, true);
In Activity
public static final String BROADCAST_ACTION = "BROADCAST_ACTION";
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null &&
intent.getAction().equals(BROADCAST_ACTION)) {
viewPager.setCurrentItem(1, true);
}
}
};
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(
MainActivity.this).registerReceiver(broadcastReceiver, new IntentFilter(BROADCAST_ACTION));
}
@Override
protected void onStop() {
super.onStop();
LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(broadcastReceiver);
}
In adapter onClick
Intent intent = new Intent(MainActivity.BROADCAST_ACTION);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Try below code in your activity
viewPager.setAnimation(your_custom_animation);
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.