-1
Cardview.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    card_view:cardCornerRadius="4dp">

    <TextView
        android:id="@+id/text_cardview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp" />
</android.support.v7.widget.CardView>


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

</LinearLayout>

I have implemented cardview using the Recyclerview Adapter. I want to get two cards in a row. I tried Gridview but it didn't work. What is the other way to do that?

  • Possible duplicate of [How to build a Horizontal ListView with RecyclerView?](https://stackoverflow.com/questions/28460300/how-to-build-a-horizontal-listview-with-recyclerview) – Dr.jacky Jul 28 '19 at 16:15

2 Answers2

2

You can use GridLayoutManager in Adapter class. use it in adapter constracter.Second parameter is Number of columns.

mGridLayoutManager=new GridLayoutManager(context,2);

later set Layoutmanager in adapter class.You can use this code.

@Override
public void onAttachedToRecyclerView(@NonNull final RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    recyclerView.setLayoutManager(mGridLayoutManager);
}
0

Just set the LinearLayoutManager and orientation is as HORIZONTAL for example

LinearLayoutManager llManager = new LinearLayoutManager(this);
llManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(llManager);
Krishna Sharma
  • 2,828
  • 1
  • 12
  • 23