ViewPAger2 中带有 GridLayoutManager 的 RecyclerView 在前 3 页中没有显示任何内容

RecyclerView with GridLayoutManager in ViewPAger2 shows nothing in first 3 page

我想用 ViewPager2 和 RecyclerView 创建自定义 calendarView 。当我 运行 该应用程序在首页的第 3 个页面上什么也没有显示!但是当我滚动它时,其他页面都很好!此问题发生在 android 7.0.0 及以下版本以及 android 7.1.1 及更高版本中是正常的并且运行良好。我之前以同样的方式创建了一个自定义日历,但现在它不起作用了!

ViewPager 适配器:

public class SmartCalendarViewAdapter extends FragmentStateAdapter {

    public SmartCalendarViewAdapter(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

    public SmartCalendarViewAdapter(@NonNull Fragment fragment) {
        super(fragment);
    }

    @NonNull
    @Override
    public Fragment createFragment(int pos) {
        return SmartCalendarFragment.getInstance(pos);
    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }
}

日历片段:

    public class SmartCalendarFragment extends Fragment {

    private RecyclerView mCalendarRecyclerView;
    private TextView txtPos;
    private int mPosition;


    public static SmartCalendarFragment getInstance(int position) {
        SmartCalendarFragment smartCalendarFragment = new SmartCalendarFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("mPosition", position);
        smartCalendarFragment.setArguments(bundle);
        return smartCalendarFragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();
        if (bundle != null) {
            mPosition = bundle.getInt("mPosition");
        }
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.smart_calendar_fragment_layout, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        findViews(view);
        ArrayList<Integer> models = new ArrayList<>();
        for (int i = 1; i <= 42; i++) {
            models.add(i);
        }
        mCalendarRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 7));
        CalendarCellAdapter adapter = new CalendarCellAdapter(models);
        mCalendarRecyclerView.setAdapter(adapter);
        txtPos.setText(String.valueOf(mPosition));
    }

    private void findViews(View view) {
        mCalendarRecyclerView = view.findViewById(R.id.smart_calendar_recyclerView);
        txtPos = view.findViewById(R.id.txtPos);
    }

}

内部 RecyclerView 适配器:

public class CalendarCellAdapter extends RecyclerView.Adapter<CalendarCellAdapter.CellViewHolder> {
    private List<Integer> mSmartCalendarCellModels;

    public CalendarCellAdapter(List<Integer> smartCalendarCellModels) {
        mSmartCalendarCellModels = smartCalendarCellModels;
    }

    @NonNull
    @Override
    public CellViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_attendance_calendar_cell_layout,parent,false);
        return new CellViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CellViewHolder cellViewHolder, int i) {
        cellViewHolder.bindView(mSmartCalendarCellModels.get(i));
    }

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

    class CellViewHolder extends RecyclerView.ViewHolder {
        TextView mTextView;
        public CellViewHolder(@NonNull View itemView) {
            super(itemView);
            mTextView=itemView.findViewById(R.id.item_attendance_cell_txtDate);
        }
        void bindView(Integer model){
            mTextView.setText(String.valueOf(model));
        }
    }
}

主要活动:

public class MainActivity extends AppCompatActivity {

    ViewPager2 mViewPager2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewPager2=findViewById(R.id.viewPager2);
        SmartCalendarViewAdapter adapter=new SmartCalendarViewAdapter(this);
        mViewPager2.setOffscreenPageLimit(1);
        mViewPager2.setAdapter(adapter);
        mViewPager2.setCurrentItem(Integer.MAX_VALUE/2,false);
    }
}

片段布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/smart_calendar_fragment_root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layoutDirection="rtl"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtPos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/smart_calendar_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:overScrollMode="never" />


</LinearLayout>

单元格布局文件:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/item_attendance_cell_root"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <TextView
        android:id="@+id/item_attendance_cell_txtDate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="w,1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:padding="4dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        tools:text="1" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity 布局文件:

<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.viewpager2.widget.ViewPager2
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPager2"
        android:background="#ADABF3"
        app:layout_constraintTop_toTopOf="parent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

将单元格布局文件中 TextView 的宽度更改为 wrap_content 可解决此问题。

不确定为什么它只影响预加载的页面,为什么只影响 Android 7 和更低...但是将 TextView 设置为具有 1:1 比率无论如何都没有明显效果,因为父级被拉伸以填充 space 以允许整个屏幕宽度有 7 个单元格。