150

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 RecyclerView.Adapter<RecyclerView.ViewHolder> {


List<chat> chats;
String username;
final int My=0,Their=1;

public ChatAdapter(List<chat> chats) {
    this.chats = chats;
    this.username = PushApp.getApp().getSPF().getStringData(Constants.ANAME);
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder;
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    switch (viewType) {
        case My:
            View v1 = inflater.inflate(R.layout.my_chat_child, parent, false);
            viewHolder = new MyChatHolder(v1);
            break;
        case Their:
            View v2 = inflater.inflate(R.layout.their_chat_child, parent, false);
            viewHolder = new TheirMessageHolder(v2);
            break;
        default:
            View v = inflater.inflate(R.layout.my_chat_child, parent, false);
            viewHolder = new MyChatHolder(v);
            break;
    }

    return viewHolder;

}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case My:
            MyChatHolder myChatHolder = (MyChatHolder) holder;
            myChatHolder.setMyMessage(chats.get(position).getMessage());
            break;

        case Their:
            TheirMessageHolder theirMessageHolder = (TheirMessageHolder) holder;
            theirMessageHolder.setTheirChat(chats.get(position).getFrom(), chats.get(position).getMessage());
            break;
    }
}

@Override
public int getItemViewType(int position) {
    if(username.equalsIgnoreCase(chats.get(position).getFrom()))
        return My;
    return Their;
}

@Override
public int getItemCount() {
    return chats.size();
}}

I have already used this code in other app and its working perfectly. I have checked the chats data which is also perfect.

Here's link to git repo layout files: layout files

Rujul1993
  • 1,631
  • 2
  • 10
  • 10
  • @OShiffer I have added the git repo link – Rujul1993 Apr 12 '16 at 15:26
  • 6
    Try to set `wrap_content` instead of `match_parent` for height and width in your layouts https://github.com/revic1993/androidchat/blob/master/app/src/main/res/layout/my_chat_child.xml#L4 https://github.com/revic1993/androidchat/blob/master/app/src/main/res/layout/their_chat_child.xml#L4 – Slavik Apr 12 '16 at 15:41
  • @Slavik Thanks it worked! – Rujul1993 Apr 12 '16 at 15:45

8 Answers8

506

Don't use match_parent for height for your item view. One item fills whole screen vertically so you don't see another.

Slavik
  • 6,647
  • 3
  • 15
  • 18
  • 53
    Google please add some Lint warnings, make your annoying lint useful this time! – Neon Warge Sep 08 '16 at 10:11
  • 1
    Oh Thanks, Awesome man, in my case I created item with match_parent as height. Therefore all the screen was kept behind the first item. – Naveed Ahmad Dec 04 '16 at 11:04
  • 9
    let me buy you a cofee!you just saved my sanity!thanks! – microwth May 25 '17 at 20:59
  • Doesn't work for me. All items are shown only if `Button` placed before `RecyclerView`. Any suggestions? – Konstantin Konopko Jan 15 '18 at 18:25
  • https://stackoverflow.com/questions/35906652/recycleview-displaying-only-the-first-item/35907680#35907680 – ramji Apr 03 '18 at 12:44
  • This issue only started happening for me after updating from API 23 to API 28 ... this fixed the issue. So its possible that a bug fix in an Android support library related to RecyclerView meant that match_parent no longer did the same as wrap_content. – dodgy_coder Jan 09 '19 at 07:26
  • If you're creating a custom item view layout in XML, ensure the root/containing layout in your XML file has ITS height set to `wrap_content`. BTW, how the hell are we supposed to remember this? – rmirabelle May 16 '19 at 19:06
  • I was frustrated for a day! I smashed into the debugger only to find my data set was correct. I couldn't think what could possibly be wrong??? *UNTIL THIS* – Hemil Jun 06 '19 at 05:54
  • It seems that only height is affected. I use `match_parent` for a `CardView` width in a `GridLayoutManager`, but there's no instance of one item occupying the width of the whole screen. Anyone know why? – Minh Nghĩa Nov 22 '20 at 13:54
  • Make sure you set "android:layout_height="wrap_content" for all the enclosing parents of the recycled item. For example if you have a card view in a root element of ConstraintLayout, set "android:layout_height="wrap_content" for both of them – Met Kiani May 23 '22 at 12:12
23

when you are creating row.xml for recyclerview should follow these things:

  1. Always use "wrap_content" for the height of the row otherwise in "match_parent" it will occupy the whole screen for a single row.

  2. You can also take the height in dp.

Pranav P
  • 1,755
  • 19
  • 39
Abhishek Kumar
  • 1,255
  • 13
  • 21
15

My mistake was I accidentally used:

LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)

instead of:

LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
MiniClem
  • 20
  • 2
Uriel Frankel
  • 14,304
  • 8
  • 47
  • 69
3

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.

abc abc
  • 31
  • 1
1

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>
Searene
  • 25,920
  • 39
  • 129
  • 186
1

After checking the log make sure multiple items are added in the list but in UI its showing only 1 item means check this properties. In item_xml file change property to

Correct Approach

 android:layout_width="match_parent"
 android:layout_height="wrap_content"

wrong Approach

 android:layout_width="match_parent"
 android:layout_height="match_parent"
vinay shetty
  • 895
  • 1
  • 9
  • 15
1

I have this issue today, and after 1001 minutes, I find out this come from this line inside RecyclerView:

android:layout_marginTop="10dp"

My RecyclerView was put inside NestedScrollView, I think that is the indirect cause. So I put here for anyone else meet the same issue. here the image

Alias
  • 39
  • 1
  • 4
0

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" />
Gautam Surani
  • 1,136
  • 10
  • 21