Recycler view showing single item

AndroidAndroid Recyclerview

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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.

Solution 3 - Android

My mistake was I accidentally used:

LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)

instead of:

LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

Solution 4 - Android

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>

Solution 5 - Android

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.

Solution 6 - Android

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"

Solution 7 - Android

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

Solution 8 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRujul1993View Question on Stackoverflow
Solution 1 - AndroidSlavikView Answer on Stackoverflow
Solution 2 - AndroidAbhishek KumarView Answer on Stackoverflow
Solution 3 - AndroidUriel FrankelView Answer on Stackoverflow
Solution 4 - AndroidSeareneView Answer on Stackoverflow
Solution 5 - Androidabc abcView Answer on Stackoverflow
Solution 6 - Androidvinay shettyView Answer on Stackoverflow
Solution 7 - AndroidAliasView Answer on Stackoverflow
Solution 8 - AndroidGautam SuraniView Answer on Stackoverflow