在使用
我正在像这样在父片段中使用子片段
这是父片段的代码。
FragmentStateAdapter
之前,我使用的是FragmentPagerAdapter
,一切都很好,但当我从FragmentPagerAdapter
迁移到FragmentStateAdapter
以减少内存使用时,我遇到的问题是:RecyclerView
在顶部位置时,单击列表中的任何项都无法工作。如果我将RecyclerView
向下滚动一点,则onClick就可以正常工作。这个问题在使用SwipeRefreshLayout
时并没有发生。当我从RecyclerView
的父级中移除SwipeRefreshLayout
时,它就可以正常工作了。我该如何在SwipeRefreshLayout
中使用它?我正在像这样在父片段中使用子片段
![enter image description here](https://istack.dev59.com/yJ3gM.webp)
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冲突
我查看了这些答案,但没有解决我的问题。