Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set two columns recyclerview?

Tags:

android

kotlin

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/PetPhoto_imageview"
        android:layout_width="200dp"
        android:layout_height="200dp"

        app:layout_constraintBottom_toTopOf="@+id/PetItem_textview"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed"
        app:srcCompat="@drawable/dog" />

    <TextView

        android:id="@+id/PetItem_textview"
        android:layout_width="199dp"
        android:layout_height="38dp"
        android:layout_alignBottom="@+id/PetPhoto_imageview"
        android:layout_alignParentStart="true"
        android:layout_marginStart="0dp"

        android:alpha="0.3"
        android:background="@color/black"
        android:gravity="bottom"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold"
        tools:text="100" />
</RelativeLayout>

</android.support.v7.widget.CardView>

Those are my layout files, then picture below was they look like now, I want to set those pictures in two columns.

like image 614
Zhao Avatar asked Oct 28 '18 21:10

Zhao


Video Answer


2 Answers

Set from xml file add two property in RecyclerView

app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
like image 131
Dinesh Avatar answered Sep 25 '22 01:09

Dinesh


If i understand correctly, you want to have the items in your recyclerview able to show in two columns. Try adding this when you set layoutManager

recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
like image 44
Martin Lund Avatar answered Sep 24 '22 01:09

Martin Lund