AaBbCc
AaBbCc

Reputation: 39

RecyclerView Not Loading all items

I am populating Recycler View with a List of 7 string items but Recycler view only loads two of them

by the way my data is long text and it loads all items when text is short

this is my 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>

and Activity

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

and this is my layout activity 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>

Upvotes: 0

Views: 1007

Answers (2)

rahat
rahat

Reputation: 2056

Try with

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

The issue is

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

Here android:layout_height="match_parent" is a bug, since you are inside a scroll view your recyclerview should have height wrap_content. So that the parent will allow it to scroll.

Upvotes: 1

Abrar Malekji
Abrar Malekji

Reputation: 111

Try changing ConstraintLayout height match_parent instead of wrap_content. and if persists, Remove scrollView from root because RecyclerView itself makes layout scrollable.

Upvotes: 0

Related Questions