如何使用GridLayoutManager设置RecyclerView以匹配设备屏幕宽度?

时间:2019-05-25 20:47:20

标签: java android android-recyclerview gridlayoutmanager

我有一个RecyclerView,我希望始终有3列。出于某种原因,使用GridLayoutManager时的宽度似乎是静态宽度,我不确定如何使它与屏幕匹配。

在iOS上,我使用UICollectionView很容易,而auto layout这样管理:

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>

这是我在“活动”中设置回收站的方法:

  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类,它是一个自定义类,可以帮助使用Grid Animations:

  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);
    }
}

}

1 个答案:

答案 0 :(得分:0)

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