Android ViewPAger2中带有GridLayoutManager的RecyclerView在前3页中未显示任何内容
我想使用ViewPager2和RecyclerView创建一个自定义日历视图。当我运行该应用程序时,它在第3页中没有显示任何内容!但当我滚动它的其他页面是好的!安卓7.0.0及以下版本和安卓7.1.1及以上版本中出现的这个问题是可以解决的,效果良好。我以前用同样的方法创建了一个自定义日历,但现在它不工作了 ViewPager的适配器:Android ViewPAger2中带有GridLayoutManager的RecyclerView在前3页中未显示任何内容,android,android-recyclerview,android-viewpager2,Android,Android Recyclerview,Android Viewpager2,我想使用ViewPager2和RecyclerView创建一个自定义日历视图。当我运行该应用程序时,它在第3页中没有显示任何内容!但当我滚动它的其他页面是好的!安卓7.0.0及以下版本和安卓7.1.1及以上版本中出现的这个问题是可以解决的,效果良好。我以前用同样的方法创建了一个自定义日历,但现在它不工作了 ViewPager的适配器: public class SmartCalendarViewAdapter extends FragmentStateAdapter { public
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);
}
}
片段布局文件:
<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>
<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>
单元布局文件:
<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>
<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>
主活动布局文件:
<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>
<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>
将单元格布局文件中文本视图的宽度更改为
包装内容
可以解决此问题
不知道为什么它只影响预加载的页面,为什么只影响安卓7及更低版本。。。但是,将TextView设置为1:1的比例无论如何都没有明显的效果,因为父视图被拉伸以填充整个屏幕宽度的7个单元格。哦,我的天哪!愚蠢的问题!非常感谢你。我储备了4天!为了找到问题,我改变了一切。还尝试使用ViewPager而不是ViewPager2和GridView而不是RecyclerView,但同样的问题!!jsut用于包装内容
宽度!!Tanks a lotcool-很高兴能够提供帮助:)