简体   繁体   English

从适配器单击第一个选项卡recyclerview项时,如何为第二个选项卡设置动画?

[英]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.

相关问题 仅当在 recyclerView 中编辑第一个时如何编辑第二个 editText - how to edit 2nd editText only if the 1st is edited in recyclerView 河内塔显示输出。 如何显示第一个递归调用的1个选项卡,第二个递归调用的2个选项卡等? - Tower of Hanoi displaying output. How to display 1 tab for 1st recursive call, 2 tabs for 2nd recursive call, etc? 如何从第二活动到第一活动获取数据 - How to get data from 2nd Activity to 1st Activity 如何将代码从第二类添加到第一类 - How to add code from 2nd class to 1st class JComboBox仅在从第1到第2时才更改组件(而不是从第3到第2时) - JComboBox changes components only when going from 1st to 2nd (Not from 3rd to 2nd) 在Java swing中如何在当前窗口(第一帧)中单击按钮时如何打开新窗口(第二帧) - how to open a new window(2nd frame) when a button is clicked in present window(1st frame) in java swing 如何通过单击第一个Java Windows应用程序(Jframe)中的菜单项启动第二个Jframe - How to start 2nd Jframe by click menu item in 1st java windows application (Jframe) 从Android中的第一个应用程序开始第二个应用程序的活动 - Start Activity of 2nd app from 1st app in android 如何将数组值添加到第二个组合框模型项中,从第一个组合框所选项目中获取数组名称? - How can I add array value into 2nd Combo Box model item getting the array name from 1st Combo Box selected item? 当第一个 API 和第二个 API 在测试环境中工作时,如何模拟第三个 API 响应 - How to mock 3rd API response when 1st API and 2nd API is working on test environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM