Make a RecyclerView to wrap its contents when using a CardView with a square ImageView

I am trying to achieve the following structure (image below). As you can see, there are two sections with RecyclerViewand CardView. These two sections are divided into two TextViewby Button.

Each section should correspond to the width of the screen (minus the indent between the cards). Each CardViewhas a square ImageViewin it. So that the height of the CardViewdepends on the width of the screen: card_height = screen_width - indent_between_cards + space_for_card_text. To achieve this behavior, I use a simple SquareImageViewone that looks like this:

public class SquaredImageView extends ImageView {

  public SquaredImageView(Context context) {
      super(context);
  }

  public SquaredImageView(Context context, AttributeSet attrs) {
      super(context, attrs);
  }

  public SquaredImageView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      int width = getMeasuredWidth();
      setMeasuredDimension(width, width);
  }
}

CardView layout is as follows:

<CardView 
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dp"
    android:background="@color/snow"
    android:clickable="true"
    android:orientation="vertical">

    <!-- Image -->
    <com.test.views.SquaredImageView
        android:id="@+id/card_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Content -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="32dp"
        android:orientation="horizontal">

        <!-- Title -->
        <TextView
            android:id="@+id/card_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:singleLine="true"
            android:textColor="@color/stone_dark"
            android:textSize="14sp" />

        <!-- Button -->
        <ImageView
            android:id="@+id/card_menu"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="center_vertical"
            android:clickable="true"
            android:scaleType="center"
            android:src="@drawable/ic_menu" />    
    </LinearLayout>
</CardView>

This means that the adapter RecyclerViewdoes not know the dimensions in advance CardView.

, , , RecyclerView , , .

SquareImageView + RecyclerView , .

SquareImageView. LayoutManager , . , RecyclerView ( height width match_parent).

, : RecyclerView SquareImageView ?

+1

Source: https://habr.com/ru/post/1617327/


All Articles