Recycler view showing single item

后端 未结 6 770
栀梦
栀梦 2020-11-28 07:34

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         


        
相关标签:
6条回答
  • 2020-11-28 08:11

    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.

    0 讨论(0)
  • 2020-11-28 08:12

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

    0 讨论(0)
  • 2020-11-28 08:19

    My mistake was I accidentally used:

    LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
    

    instead of:

    LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
    
    0 讨论(0)
  • 2020-11-28 08:24

    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>
    
    0 讨论(0)
  • 2020-11-28 08:28

    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.

    0 讨论(0)
  • 2020-11-28 08:34

    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" />
    
    0 讨论(0)
提交回复
热议问题