【问题标题】:how to animate to 2nd tab when 1st tab recyclerview item is clicked from adapter?从适配器单击第一个选项卡 recyclerview 项目时如何动画到第二个选项卡?
【发布时间】:2018-07-07 06:01:05
【问题描述】:

过去 2 天我一直在寻找解决方案,但没有解决方案。因此,当单击 recyclerview 项目时,我不得不使用 Intent 重新启动活动。下面是代码:

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();
        }
    });

然后在活动中使用下面的代码来选择这样的第二个选项卡。

viewPager.setCurrentItem(1, false);
tabLayout.setupWithViewPager(viewPager);

虽然这可行,但我仍然想在单击第一个选项卡 recyclerview 项目时使用幻灯片动画加载第二个选项卡。感谢您的帮助。

【问题讨论】:

    标签: java android android-recyclerview android-viewpager android-tablayout


    【解决方案1】:

    您可以使用从 onClick 到活动的广播,然后在活动中您可以执行类似这样的操作viewPager.setCurrentItem(1, true);

    活动中

     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);
    }
    

    在适配器上点击

    Intent intent = new Intent(MainActivity.BROADCAST_ACTION);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    

    在您的活动中尝试以下代码

    viewPager.setAnimation(your_custom_animation);
    

    【讨论】:

    • 你能给我一些示例代码吗?我对广播了解不多。我想我很快就会学会。
    • 很抱歉回复晚了,但它仍在加载旧方式。我按照你的建议做了。
    • 你想使用自定义幻灯片动画来选择标签吗?
    • 当我从“TAB1”单击“TAB2”时,是相同的动画。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多