如何自动将Recyclerview项目调整为适合Android屏幕的宽度?

时间:2019-06-18 07:14:15

标签: android android-recyclerview

我正在尝试实现以下布局。我要以占据整个屏幕宽度的方式放置项目:

预期布局: enter image description here

实际布局:

enter image description here

这是我的Recyclerview项目的代码:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

  <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/size_8dp"
    android:paddingStart="@dimen/size_20dp"
    android:paddingEnd="@dimen/size_20dp"
    android:paddingBottom="@dimen/size_8dp">

    <ImageView
      android:id="@+id/legendsItemIcon"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintBottom_toTopOf="@+id/text_legend_status_name"
      />
    <TextView
      android:id="@+id/text_legend_status_name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      tools:text="Low"
      android:textSize="@dimen/text_size_8sp"
      app:layout_constraintTop_toBottomOf="@+id/legendsItemIcon"
      app:layout_constraintStart_toStartOf="@+id/legendsItemIcon"
      app:layout_constraintEnd_toEndOf="@+id/legendsItemIcon"
      app:layout_constraintBottom_toBottomOf="parent"
      android:textColor="@color/legends_item_status_text_color"
      android:layout_marginTop="@dimen/size_4dp"
      />

  </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

我该如何实现?

4 个答案:

答案 0 :(得分:1)

此自定义类将帮助您https://gist.github.com/heinrichreimer/39f9d2f9023a184d96f8

D.Independent

将RecyclerView layoutManager设置为SpanningLinearLayoutManager而不是LinearLayoutManager

答案 1 :(得分:1)

如果我理解正确,则需要水平的回收站视图。 因此,将您的布局管理器设置如下:

    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
    recyclerView.setLayoutManager(mHorizontalLayoutManager);

如果您的商品超过了屏幕尺寸,它将可以滚动

答案 2 :(得分:0)

您只需使用GridLayoutManager而不是LinearLayoutManager

 /**
 * Creates a vertical GridLayoutManager
 *
 * @param context Current context, will be used to access resources.
 * @param spanCount The number of columns in the grid
 */
public GridLayoutManager(Context context, int spanCount) {
    super(context);
    setSpanCount(spanCount);
}

答案 3 :(得分:0)

如果你想让 Recyclerview 包裹它的内容(全高度拉伸包裹它的内容)。只需用 NestedScrollView 包装它。我测试了它的工作。

<androidx.core.widget.NestedScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  >

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <!-- This Recyclerview will fit content.
    So it become full height stretch wrap its content-->
    <RecyclerView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Bayar SPP"
        style="@style/FontStyleSubtitle"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recyclerView" />

   </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>
相关问题