154

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

3

8 Answers 8

516

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

10
  • 57
    Google please add some Lint warnings, make your annoying lint useful this time!
    – nww04
    Commented Sep 8, 2016 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. Commented Dec 4, 2016 at 11:04
  • 9
    let me buy you a cofee!you just saved my sanity!thanks!
    – microwth
    Commented May 25, 2017 at 20:59
  • Doesn't work for me. All items are shown only if Button placed before RecyclerView. Any suggestions? Commented Jan 15, 2018 at 18:25
  • stackoverflow.com/questions/35906652/…
    – ramji
    Commented Apr 3, 2018 at 12:44
24

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.

2
  • 4
    i had the same issue tried scrolling and saw the rest of the items Commented Feb 23, 2017 at 10:37
  • In 2024, this is the way.
    – skywalker
    Commented Jan 22 at 17:22
16

My mistake was I accidentally used:

LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)

instead of:

LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
2
  • 4
    This was my mistake too. Thanks for this. Commented Oct 23, 2020 at 15:44
  • 1
    LOL, sometimes so basic mistakes turn days into nightmares. The same mistake here. Thanks for this answer mate. Commented Aug 12, 2022 at 11:32
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.

1
  • thanks I wasted my 4 hours trying everything other than this Commented Aug 25, 2022 at 16:32
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>
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"
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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.