[英]how to animate to 2nd tab when 1st tab recyclerview item is clicked from adapter?
I have been looking for a solution past 2 days and yet no solution worked. 我过去2天一直在寻找解决方案,但没有解决方案。 So I had to use Intent to restart the activity when item of recyclerview is clicked.
因此,当单击recyclerview项时,我不得不使用Intent重新启动活动。 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. 虽然这可行,但是当单击第一个选项卡recyclerview项时,我仍然想使用幻灯片动画加载第二个选项卡。 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);
您可以使用从onClick到活动的广播,然后在活动中可以执行以下操作:
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 在适配器onClick
Intent intent = new Intent(MainActivity.BROADCAST_ACTION);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Try below code in your activity 在您的活动中尝试以下代码
viewPager.setAnimation(your_custom_animation);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.