Search code examples
androidandroid-recyclerviewandroid-viewpager2

RecyclerView with GridLayoutManager in ViewPAger2 shows nothing in first 3 page


I want to create a custom calendarView with ViewPager2 and RecyclerView . When I run the app its showing nothing in 3 first page! But when I scroll it other pages are fine ! This problem happening in android 7.0.0 and below and for android 7.1.1 and above is ok and works good. I created a custom calendar before in same way but now its not working !

Adapter for 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;
    }
}

Calendar Fragment :

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

}

Inner RecyclerView Adapter :

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

MainActivity :

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

Fragment layout file :

<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>

Cell Layout file :

<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 Layout file :

<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>

Solution

  • Changing the width of the TextView in your cell layout file to wrap_content fixes the issue.

    Not sure why it only affects the preloaded pages, and why only on Android 7 and lower... but setting the TextView to have 1:1 ratio has no visible effect anyway, cos the parent is stretched to fill the space to allow for 7 cells across the screen width.