簡體   English   中英

從適配器單擊第一個選項卡recyclerview項時,如何為第二個選項卡設置動畫?

[英]how to animate to 2nd tab when 1st tab recyclerview item is clicked from adapter?

我過去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項時,我仍然想使用幻燈片動畫加載第二個選項卡。 謝謝您的幫助。

您可以使用從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);
}

在適配器onClick

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

在您的活動中嘗試以下代碼

viewPager.setAnimation(your_custom_animation);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM