0

I am trying a very simple application that uses a RecyclerView with a GridLayoutManager. What I want to achieve is to maintain the column-width of the card-items and change the margins between them (I need to keep my column width fixed). But what happens is the width of the column itself changes when I switch between portrait and landscape.

If I understand this correctly, I need to measure the width of the screen divide it by the width of each card and set the span count of the GridLayoutManager to the calculated value. But I seem to be missing something.

Also, I tried using the AutoFitRecyclerView (http://blog.sqisland.com/2014/12/recyclerview-autofit-grid.html) and there are a lot of posts RecyclerView GridLayoutManager: how to auto-detect span count? , Changing number of columns in RecyclerView gridlayout that help auto-detect the span count, but this is not what I want to achieve. I would like to have the same column-width while adjusting the margins between the columns.

Any pointers or a solution would be most appreciated.

Here is the grid-item's layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="210dp"
    android:layout_height="338dp"
    android:layout_margin="12dp">

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

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="270dp"
            android:background="#1F000000"/>

        <TextView
            android:id="@+id/title_text"
            android:layout_width="164dp"
            android:layout_height="wrap_content"
            android:layout_below="@id/thumbnail"
            android:layout_marginBottom="13dp"
            android:layout_marginLeft="13dp"
            android:layout_marginRight="32dp"
            android:layout_marginTop="13dp"
            android:ellipsize="end"
            android:maxLines="2"
            android:textColor="#393c3d"
            android:textSize="14sp" />
    </RelativeLayout>

</android.support.v7.widget.CardView>
Community
  • 1
  • 1
user1841702
  • 2,683
  • 6
  • 35
  • 53
  • post the layout? – Shmuel Mar 22 '17 at 02:18
  • @Shmuel I just added the GridItem's layout, I would like to retain the width and height of the CardView regardless of orientation. There is a 12dp margin that shouldn't be shouldn't be fixed. – user1841702 Mar 22 '17 at 02:24

1 Answers1

0

Maybe you can override CardView's onMeasure method and setMeasuredDimension to 210dp and 338dp ,so that when Recyclerview calculate your Cardview's width and height ,it will return the absolute width and height.

lomg
  • 136
  • 2