Android Horizontal RecyclerView wrap_content

1.5k Views Asked by At

hello I'm trying to use Recyclerview,

below code results only a blank screen, can you please tell me where I'm going wrong

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:gravity="top|center_vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/ImageSelectorRecycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="horizontal" />

    <ImageView
        android:id="@+id/ImageViewSelectPhoto"
        android:layout_width="80dp"
        android:layout_height="76dp"
        android:src="@drawable/ic_add_a_photo_black_361px"
        android:background="@drawable/back"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp" />

</LinearLayout>
3

There are 3 best solutions below

0
On
  1. Since you have provided

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

    for your RecyclerView, its taking the full width and height of your device or parent view. And the ImageView is outside the device screen.

  2. Now you have just declared the RecyclerView in your XML. You need a RecyclerView Row Layout which should define the layout for the RecyclerView items.

To show something in the RecyclerView you can set static data in the Row Layout layout iteself or need to create a RecyclerView adapter and model for dynamic/static data.

0
On

You need to make a list item layout for your Horizontal-scrollview and a seperate adapter also for it.

Try adding android:viewfillport="true"

I too asked a similar type of question ,please have a look to it yu will surely get a simple way to implement this .

Horizontal Listview Not Working from github

2
On

I think it should be similar to this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:gravity="top|center_vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/ImageSelectorRecycler"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent"/>

<ImageView
    android:id="@+id/ImageViewSelectPhoto"
    android:layout_width="80dp"
    android:layout_height="76dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="20dp"
    android:src="@android:drawable/ic_dialog_alert"
    android:background="@android:color/holo_green_dark" />

</LinearLayout>