【问题标题】:How to auto fit recyclerview items to the width of screen android?如何将recyclerview项目自动适应屏幕android的宽度?
【发布时间】:2021-10-27 20:31:26
【问题描述】:

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

预期布局:

实际布局:

这是我的 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>

我怎样才能做到这一点?

【问题讨论】:

  • 你的recyclerview是水平方向的吗?
  • 是否要将每个 recyclerview 行项目左对齐(开始)?
  • @DhavalSolanki 是的
  • 我想将它们扩展到屏幕的宽度@Fartab
  • 每行有两个项目吗?并希望每个都扩大到父母宽度的一半?

标签: android android-recyclerview


【解决方案1】:

这个自定义类会帮助你https://gist.github.com/heinrichreimer/39f9d2f9023a184d96f8

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.ViewGroup;

public class SpanningLinearLayoutManager extends LinearLayoutManager {

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

public SpanningLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);
}

public SpanningLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
    return spanLayoutSize(super.generateDefaultLayoutParams());
}

@Override
public RecyclerView.LayoutParams generateLayoutParams(Context c, AttributeSet attrs) {
    return spanLayoutSize(super.generateLayoutParams(c, attrs));
}

@Override
public RecyclerView.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
    return spanLayoutSize(super.generateLayoutParams(lp));
}

@Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
    return super.checkLayoutParams(lp);
}

private RecyclerView.LayoutParams spanLayoutSize(RecyclerView.LayoutParams layoutParams){
    if(getOrientation() == HORIZONTAL){
        layoutParams.width = (int) Math.round(getHorizontalSpace() / (double) getItemCount());
    }
    else if(getOrientation() == VERTICAL){
        layoutParams.height = (int) Math.round(getVerticalSpace() /  (double) getItemCount());
    }
    return layoutParams;
}

@Override
public boolean canScrollVertically() {
    return false;
}
@Override
public boolean canScrollHorizontally() {
    return false;
}

private int getHorizontalSpace() {
    return getWidth() - getPaddingRight() - getPaddingLeft();
}

private int getVerticalSpace() {
    return getHeight() - getPaddingBottom() - getPaddingTop();
}
}

将 RecyclerView layoutManager 设置为 SpanningLinearLayoutManager 来代替 LinearLayoutManager

【讨论】:

  • 已经够近了。如何均匀分布项目并仍然使内容可滚动?我有超过 10 件物品,它们会变得非常接近。 @SSB
  • @SSB 你能看看这个issue
【解决方案2】:

如果我理解正确,您需要一个水平回收站视图.. 所以设置你的布局管理器如下:

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

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

【讨论】:

  • 嘿。你能看看这个issue
【解决方案3】:

您只需使用 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);
}

【讨论】:

    【解决方案4】:

    如果您希望 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>
    

    【讨论】:

      【解决方案5】:

      使用此代码:

      recyclerView.layoutManager = GridLayoutManager(context, splitcount)
      

      这对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-23
        • 1970-01-01
        • 2015-11-25
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        相关资源
        最近更新 更多