如何使用GridLayoutManager设置RecyclerView以适应设备屏幕宽度?

3
我有一个RecyclerView,我希望它始终有三列。使用GridLayoutManager时,出现了静态宽度的问题,我不知道如何使其匹配屏幕宽度。
在iOS上,我使用UICollectionView自动布局很容易解决宽度问题,就像这样:

How width should look.

我该如何在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);

但它给出了这个结果,无论设备如何,宽度都相同:

enter image description here

我不想改变图标的大小,只想使宽度间距基本匹配其父容器。
谢谢!
编辑:这是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的宽度设置为match_parent吗?像这样android:layout_width="match_parent"? - Pabi Moloi
@PabiMoloi 是的,它的宽度是一样的,只是向左移动了。 - Peter Ruppert
2
尝试为RecyclerView的项以及RecyclerView本身设置layout_width="match_parent" - atarasenko
@atarasenko 太棒了,解决了! - Peter Ruppert
1个回答

0
尝试将GridRecyclerView设置为匹配约束条件。 在您的情况下,只需将android:layout_width = 0dp即可。

这只是使得网格向左移动了,问题似乎在于GridLayoutManager而不是实际的recyclerView。 - Peter Ruppert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接