首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ViewPAger2中包含GridLayoutManager的RecyclerView在前3页中未显示任何内容

在ViewPAger2中包含GridLayoutManager的RecyclerView在前3页中未显示任何内容
EN

Stack Overflow用户
提问于 2020-11-28 19:06:08
回答 1查看 362关注 0票数 0

我想用ViewPager2和RecyclerView创建一个自定义的calendarView。当我运行应用程序时,它在首页的3个页面中什么都没有显示!但是当我滚动它的时候,其他的页面都没问题!这个问题发生在安卓7.0.0和更低版本,安卓7.1.1和更高版本是正常的,工作良好。我以前用同样的方法创建了一个自定义日历,但现在它不工作了!

ViewPager适配器:

代码语言:javascript
复制
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;
    }
}

日历片段:

代码语言:javascript
复制
    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适配器:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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);
    }
}

片段布局文件:

代码语言:javascript
复制
<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>

单元格布局文件:

代码语言:javascript
复制
<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布局文件:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-28 21:35:54

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

不确定为什么它只影响预加载的页面,为什么只在Android 7和更低版本上...但是,将TextView设置为1:1的比例无论如何都不会有明显的效果,因为父对象被拉伸以填充空间,从而允许7个单元格横跨屏幕宽度。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65048912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档