簡體   English   中英

RecyclerView onClick 事件不適用於 SwipeRefreshLayout?

[英]RecyclerView onClick event is not working with SwipeRefreshLayout?

在使用FragmentStateAdapter之前。 我當時使用的是FragmentPagerAdapter ,然后一切都很好,但是當我從FragmentPagerAdapter遷移到FragmentStateAdapter以減少 memory 的使用時。 我面臨的問題是,當RecyclerView位於頂部 position 時,單擊列表中的任何項目都不起作用。 如果我將RecyclerView向下滾動一點,那么 onClick 可以正常工作。 SwipeRefreshLayout沒有發生這個問題。 當我從RecyclerView的父級刪除SwipeRefreshLayout時。 它工作正常。 我如何將它與SwipeRefreshLayout一起使用?

我在這樣的父片段中使用子片段在此處輸入圖像描述

這是父片段的代碼。

public class DailyFragment extends Fragment {
    private FragmentDailyBinding binding;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentDailyBinding.inflate(inflater, container, false);
        MyChildFragmentAdapter childFragmentAdapter = new MyChildFragmentAdapter(this.getChildFragmentManager(), this.getLifecycle()); // I'm showing these fragment in Parent fragment. So that I'm using getChildFragmentManager() 
        childFragmentAdapter.addFragment(new FirstFragment(),"first");
        childFragmentAdapter.addFragment(new SecondFragment(),"second");
        childFragmentAdapter.addFragment(new ThirdFragment(),"third");
        childFragmentAdapter.addFragment(new FourthFragment(),"fourth");
        childFragmentAdapter.addFragment(new FifthFragment(),"fifth");
        binding.dailyViewPager.setAdapter(childFragmentAdapter);
        new TabLayoutMediator(binding.dailyTabLayout, binding.dailyViewPager, (tab, position) -> tab.setText(childFragmentAdapter.fragmentsArrayListTitle.get(position))).attach();
        binding.dailyViewPager.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
        return binding.getRoot();
    }


    // FragmentStateAdapter
    public static class MyChildFragmentAdapter extends FragmentStateAdapter {
        private final ArrayList<Fragment> fragmentsArrayList = new ArrayList<>();
        private final ArrayList<String> fragmentsArrayListTitle = new ArrayList<>();

        public MyChildFragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
            super(fragmentManager, lifecycle);
        }



        public void addFragment(Fragment fragment, String title) {
            fragmentsArrayList.add(fragment);
            fragmentsArrayListTitle.add(title);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragmentsArrayList.get(position);
        }

        @Override
        public int getItemCount() {
            return fragmentsArrayList.size();
        }

    }
}

這是FirstFragment()的這個 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".ChieldFragment.dailyChield.MahadevFragment">
    
<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/mahadevSwipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/mahadevRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>

我的問題類似於這個問題

ViewPager2 與 SwipeRefreshLayout 沖突

我檢查了這些答案,但沒有解決我的問題。

不久前我遇到了同樣的問題。 我通過將 NestedScrollView 添加為 SwipeRefreshLayout 子項來修復我的問題,然后簡單地將您的回收器布局作為 NestedScrollView 的子項。 請參見下面的示例:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/mahadevSwipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        // I didn't put mandatory constraints, but you'll have to
        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/mahadevRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.core.widget.NestedScrollView>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

或者,您也可以保持代碼不變,並使用滾動偵聽器添加自定義滾動行為。 告訴我如果我的第一個猜測不起作用,我會給你一個自定義滾動處理的例子。

嘗試將app:layout_behavior="@string/appbar_scrolling_view_behavior"添加到協調器布局下的所有視圖。

暫無
暫無

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

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