Andrei Meriacre
Andrei Meriacre

Reputation: 114

LinearLayout horizontal orientation doesn't work for RecycleView elements

I try to align all my recycle view elements by 2 on the etch line, but they align vertically only by one element on the line.

here is my code:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
    android:id="@+id/card_pdf"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="12dp"
    android:layout_marginTop="12dp"
    android:layout_marginEnd="12dp"
    android:layout_marginBottom="12dp"
    android:layout_weight="1">
<TextView
android:id="@+id/tv_pdf_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ceva"
android:textSize="25sp"
android:padding="6dp"/>
</androidx.cardview.widget.CardView>
</LinearLayout>

Result I get: Elements align veryical

Upvotes: 0

Views: 218

Answers (2)

Harsh Patel
Harsh Patel

Reputation: 712

if you want to set items in recyclerview side by side in 2 columns then you have to use recyclerview grid layout manager. for example : android-gridlayoutmanager-example

Upvotes: 1

Mwasz
Mwasz

Reputation: 186

You seem to not understand the concept or a RecyclerView. A ViewHolder contains only 1 item (so the layout you've created affects only one ViewHolder, not the whole RecyclerView, You will never achieve what You want with that).

Instead, add those lines to Your RecyclerView in xml:

app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"

GridLayoutManager lays out items in a grid. spanCount defines how many columns will there be in that grid.

Also there is no need for a LinearLayout in your item layout. You should have just the CardView and the TextView.

Upvotes: 1

Related Questions