gpt4 book ai didi

android - Recyclerview网格布局项目剪切问题

转载 作者:行者123 更新时间:2023-12-05 00:15:59 25 4
gpt4 key购买 nike

我想在 recyclerView 中显示具有 2 列网格布局的列表。但是当我应用项目装饰类时,项目将从右侧切割。请检查下面的屏幕截图和代码。如果有人对此有任何想法,请告诉我。

enter image description here

行文件 xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

<variable
name="viewModel"
type="com.sg.gbb.ui.tourplantcollection.viewmodel.PlantCollectionAdapterViewModel" />
</data>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
android:layout_width="164dp"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp"
app:cardElevation="0dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/screen_bg"
android:orientation="vertical">

<ImageView
android:layout_width="match_parent"
android:layout_height="164dp"
android:contentDescription="@string/gbb_app_name"
android:scaleType="fitXY"
android:src="@color/flower_bg" />

<com.sg.gbb.ui.custom.GBBTextView
android:id="@+id/tvOrderOn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:text="@{viewModel.code}"
android:textColor="@color/greyishBrown"
android:textSize="15sp"
app:customFont="@string/gardens_light" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

</layout>

我的recyclerView文件:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

<data>

<variable
name="viewModel"
type="com.sg.gbb.ui.tourplantcollection.viewmodel.PlantCollectionViewModel" />
</data>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center_horizontal"
android:orientation="vertical">

<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@color/black_80_opacity"
android:contentDescription="@string/gbb_app_name" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="@drawable/bg_edit_profile"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">

<com.sg.gbb.ui.custom.GBBTextView
android:id="@+id/titleTextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:text="@string/plant_collection_text"
android:textColor="@color/greyishBrown"
android:textSize="15sp"
app:customFont="@string/gardens_bold" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/plantCollectionRV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:gravity="center"
android:scrollbars="none"
app:adapter="@{viewModel.plantList}"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
tools:listitem="@layout/item_plant_collection" />

</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/divider" />

<com.sg.gbb.ui.custom.GBBButton
android:id="@+id/cancelButton"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="26dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/bg_dismiss_button"
android:onClick="@{() -> viewModel.onBackClicked()}"
android:paddingStart="16dp"
android:paddingTop="12dp"
android:paddingEnd="16dp"
android:paddingBottom="12dp"
android:text="@string/back"
android:textAllCaps="true"
android:textColor="@color/done_text"
android:textSize="@dimen/_12ssp"
app:buttonFont="@string/gardens_bold" />

</LinearLayout>

</RelativeLayout>

</layout>

元素装饰文件:

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

/**
* Created by Android Studio
* User: Ailurus(<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2140484d5453545261474e594c40484d0f424e4c" rel="noreferrer noopener nofollow">[email protected]</a>)
* Date: 2015-10-28
* Time: 15:20
*/
public class EqualSpacingItemDecoration extends RecyclerView.ItemDecoration {

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

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

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

if (position >= 0) {
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
}
}
} else {
outRect.left = 0;
outRect.right = 0;
outRect.top = 0;
outRect.bottom = 0;
}
}
}

最佳答案

修复了卡片 View layout_width

作为android:layout_width="164dp"

这就是问题

您应该将其设置为 match_parent 以获得灵活且未剪切的布局,无论显示器的大小如何。

关于android - Recyclerview网格布局项目剪切问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63027647/

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