Hasni
Hasni

Reputation: 355

Delete the spaces between items in Horizontal RecyclerView java

i have a small problem in RecyclerView when use it Horizontally, the problem in that there's a long spaces between items.

like this

item1 _________ item2 ___________ item3 ....

image show the spacing between items

what i want is to remove this space and then the items look like this :

item1 item2 item3

the RecyclerView in my xml layout

<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycleview1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
        </LinearLayout>

the custom view :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="4dp"
        android:gravity="center_horizontal">

        <TextView
            android:id="@+id/textview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:gravity="center_horizontal|center_vertical"
            android:textStyle="bold"
            android:textSize="15dp"/>
    </LinearLayout>
</LinearLayout>

and the java code in using in onCreate :

 recycleview1.setAdapter(new Recyclerview1Adapter(fontListmap));
            recycleview1.setLayoutManager(new LinearLayoutManager(this));
            recycleview1.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false));

can you help me solving this ??

Upvotes: 1

Views: 235

Answers (1)

Narendra_Nath
Narendra_Nath

Reputation: 5173

Change the following lines in your custom view to

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/teal200"
        android:padding="4dp"
        android:gravity="center_horizontal">

        <TextView
            android:id="@+id/textview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:gravity="center_horizontal|center_vertical"
            android:textStyle="bold"
            android:textSize="15dp"/>
    </LinearLayout>
</LinearLayout>

This is because every time a view is being inflated it is taking the height and width as match parent. Better would be to use custom values( 40dp 100dp etc) to get an idea about what is the width and height of each element in your custom view.

Upvotes: 3

Related Questions