I am facing a strange error where recyclerview is showing only a single item. Below is code for my recyclerview adapter :
public class ChatAdapter extends Re
content_main.xml as follows
android:layout_width="match_parent"
android:layout_height="match_parent"
IN row_list.xml file make following changes
android:layout_width="match_parent"
android:layout_height="wrap_content"
I make above changes it runs.
Don't use match_parent
for height for your item view. One item fills whole screen vertically so you don't see another.
My mistake was I accidentally used:
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
instead of:
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
Try changing the layout used in your item view to FrameLayout
. Here is an example.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="?listPreferredItemHeight"
android:clickable="true"
android:focusable="true"
android:foreground="?selectableItemBackground">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"/>
</FrameLayout>
when you are creating row.xml for recyclerview should follow these things:
Always use "wrap_content" for the height of the row otherwise in "match_parent" it will occupy the whole screen for a single row.
You can also take the height in dp.
1) if your recyclerview is vertical then set height of recyclerview match_parent
and row_item.xml
height also match_parent
2) if your recyclerview is horizontal then set Width of recyclerview match_parent
and row_item.xml
Width also match_parent
for ex:- Horizontal RecyclerView
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp" />
row_item.xml
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/rect"
android:gravity="center"
android:maxLines="2"
android:padding="10dp"
android:textColor="@color/white"
android:textSize="25sp" />