RecyclerView 未加载所有项目

问题描述

我正在使用包含 7 个字符串项目的列表填充 Recycler 视图,但 Recycler 视图仅加载其中两个

顺便说一下,我的数据是长文本,文本短时它会加载所有项目

这是我的 ContentAdapter.java

public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {
    private LayoutInflater mInflater;
    private List<String> mContent;

    ContentAdapter(Context context,List<String> Content) {
        this.mInflater = LayoutInflater.from(context);
        mContent = Content;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
        View view = mInflater.inflate(R.layout.rvcontent_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ContentAdapter.ViewHolder holder,int position) {
        //loads only two times !?
        holder.txtContentPage.setText(mContent.get(position));
    }

    @Override
    public int getItemCount() {
        return mContent.size(); // size is 7
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView txtContent;
        ViewHolder(View itemView) {
            super(itemView);
            txtContent = itemView.findViewById(R.id.txtContent);
        }
    }
}

rvcontent_item.xml

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
        <TextView
            android:id="@+id/txtContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
</androidx.cardview.widget.CardView>

和活动

        rvContents.setnestedScrollingEnabled(false);
        RecyclerView.LayoutManager layoutManager = new linearlayoutmanager(this,RecyclerView.VERTICAL,false);
        rvContents.setLayoutManager(layoutManager);
        ContentAdapter contentAdapter = new ContentAdapter(this,Data); // Data has 7 items
        rvContents.setAdapter(contentAdapter);

这是我的布局活动xml

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:focusableInTouchMode="true">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rvContentPage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

解决方法

试试

 <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:nestedScrollingEnabled="false"
            android:focusableInTouchMode="true">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rvContentPage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

问题是

<androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rvContentPage"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>

这里的 android:layout_height="match_parent" 是一个错误,因为您在滚动视图中,您的 recyclerview 应该具有高度 wrap_content。以便父级允许它滚动。

,

尝试更改 ConstraintLayout 高度 match_parent 而不是 wrap_content。 如果仍然存在,请从 root 中删除 scrollView,因为 RecyclerView 本身使布局可滚动。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...