gpt4 book ai didi

android - 如何使 RecyclerView 网格项具有相同的高度和宽度?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:02:07 26 4
gpt4 key购买 nike

我将我的 RecyclerView 定义为网格,方法是使用 GridLayoutManger 设置它的布局,它的跨度数为 3,已经可以正常工作了。我还在我的回收站 View 中添加了一个项目装饰器,它将管理网格项目之间的间距,这也可以正常工作。

public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

private int spanCount;
private int spacing;
private boolean includeEdge;

public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column

if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
}
}

我为 Recycler View 添加项目装饰的实现:

mBuddiesGrid = (RecyclerView) findViewById(R.id.buddies_grid);
mLayoutManager = new GridLayoutManager(getApplicationContext(), 3);
mBuddiesGrid.addItemDecoration(new GridSpacingItemDecoration(3, (int) dpToPx(10) , true));
mBuddiesAdapter = new BuddiesGridAdapter(getApplicationContext(), new Buddies());
mBuddiesGrid.setLayoutManager(mLayoutManager);

我的网格项目 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView
android:layout_weight="1"
android:cropToPadding="false"
android:id="@+id/profile_picture"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<TextView
android:text="Theodore"
android:includeFontPadding="false"
android:textColor="@android:color/black"
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

通过在网格项之间添加空格将调整网格项的宽度,这可能导致无法获得我的项 View 的比例大小。我想要获得的是动态调整我的网格项目高度等于它的宽度。我该怎么做?

最佳答案

我使用 ConstraintLayout 完成了此操作:

<androidx.constraintlayout.widget.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="wrap_content"
>

<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="1:1"
tools:src="@drawable/ic_chat_bubble_black_24dp"
/>

</androidx.constraintlayout.widget.ConstraintLayout>

关于android - 如何使 RecyclerView 网格项具有相同的高度和宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32063565/

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