gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:05:26 25 4
gpt4 key购买 nike

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

在 iOS 上这很容易,因为我使用 UICollectionViewauto 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>

以下是我在 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);
}
}

最佳答案

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

关于java - 如何使用 GridLayoutManager 设置 RecyclerView 以匹配设备屏幕宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56308575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com