【问题标题】:How do I set a RecyclerView using GridLayoutManager to match the device screen width?如何使用 GridLayoutManager 设置 RecyclerView 以匹配设备屏幕宽度?
【发布时间】:2019-05-25 20:47:20
【问题描述】:

我有一个RecyclerView,我希望它总是有 3 列。由于某种原因,使用 GridLayoutManager 时的宽度似乎是静态宽度,我不知道如何让它与屏幕匹配。

在 iOS 上这很容易,因为我使用 UICollectionViewauto layout 管理它像这样:

如何在 Android 上获得相同的结果?这是我的content_symptom_tracker.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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"
android:background="@color/bzPrimary"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".SymptomTracker"
tools:showIn="@layout/activity_symptom_tracker">

<com.cryptixltd.peterruppert.brainzaps.GridRecyclerView
    android:id="@+id/symptomRecycler"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:foregroundGravity="center"
    android:layoutAnimation="@anim/grid_layout_animation_from_bottom"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

这是我在 Activity 中设置 Recycler 的方法:

  recyclerView = findViewById(R.id.symptomRecycler);
    int numberOfColumns = 3;
    recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
    adapter = new SymptomAdapter(this, common_symptoms);
    recyclerView.setAdapter(adapter);

但它给了我这个结果,无论设备如何,宽度都是一样的:

我不想改变图标的​​大小,只要让宽度的间距与父级基本匹配即可。

谢谢!

编辑:这里是 GridRecyclerView 类,它是一个自定义类来帮助网格动画:

  public class GridRecyclerView extends RecyclerView {

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


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


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

@Override
protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params,
                                               int index, int count) {
    final LayoutManager layoutManager = getLayoutManager();
    if (getAdapter() != null && layoutManager instanceof GridLayoutManager){

        GridLayoutAnimationController.AnimationParameters animationParams =
                (GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;

        if (animationParams == null) {
            // If there are no animation parameters, create new once and attach them to
            // the LayoutParams.
            animationParams = new GridLayoutAnimationController.AnimationParameters();
            params.layoutAnimationParameters = animationParams;
        }

        // Next we are updating the parameters

        // Set the number of items in the RecyclerView and the index of this item
        animationParams.count = count;
        animationParams.index = index;

        // Calculate the number of columns and rows in the grid
        final int columns = ((GridLayoutManager) layoutManager).getSpanCount();
        animationParams.columnsCount = columns;
        animationParams.rowsCount = count / columns;

        // Calculate the column/row position in the grid
        final int invertedIndex = count - 1 - index;
        animationParams.column = columns - 1 - (invertedIndex % columns);
        animationParams.row = animationParams.rowsCount - 1 - invertedIndex / columns;

    } else {
        // Proceed as normal if using another type of LayoutManager
        super.attachLayoutAnimationParameters(child, params, index, count);
    }
}

}

【问题讨论】:

  • 嘿,你有没有试过设置recyclerview的宽度来匹配父级?像这样 android:layout_width="match_parent" ?
  • @PabiMoloi 是的,宽度相同,只是向左移动。
  • 尝试为RecyclerView的项目设置layout_width="match_parent",并为RecyclerView本身设置layout_width="match_parent"
  • @atarasenko 太棒了!

标签: java android android-recyclerview gridlayoutmanager


【解决方案1】:

尝试设置 GridRecyclerView 以匹配约束。 在您的情况下,只需制作 android:layout_width=0dp。

【讨论】:

  • 这只是让网格向左移动,问题似乎出在 GridLayoutManager 而不是实际的 recyclerView
猜你喜欢
  • 2021-02-14
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 2015-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多