gpt4 book ai didi

android - RecyclerView 项目的高度和宽度相等

转载 作者:行者123 更新时间:2023-12-04 10:46:08 26 4
gpt4 key购买 nike

从下面的图片中可以看出,我几乎实现了我想要的。然而,RecyclerView项目没有相同的大小。特别是第二个RecyclerView列的大小与第一列不同。

请注意,我已尝试从 How to make RecyclerView's grid item to have equal height and width? 实现解决方案,但我无法解决我的问题。

Result of my code

RecyclerView 项目:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.jackandphantom.circularimageview.RoundedImage
android:id="@+id/launcher_icon"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

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

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/medium_margin"
android:layout_marginRight="@dimen/medium_margin" />

</LinearLayout>

fragment .java:
 public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);

RecyclerView recyclerView = view.findViewById(R.id.recycler);
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new ItemOffsetDecoration(getActivity(), R.dimen.medium_margin));
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(layoutManager);

RecyclerViewAdapter adapter = new RecyclerViewAdapter(getActivity());
recyclerView.setAdapter(adapter);

return view;
}

ItemOffsetDecoration.java
public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {
private int mItemOffset;

private ItemOffsetDecoration(int itemOffset) {
mItemOffset = itemOffset;
}

public ItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId) {
this(context.getResources().getDimensionPixelSize(itemOffsetId));
}

@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent,
@NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);

int position = parent.getChildLayoutPosition(view);

GridLayoutManager manager = (GridLayoutManager) parent.getLayoutManager();

if (manager != null && position < manager.getSpanCount()) outRect.top = mItemOffset;

if (position % 2 != 0) {
outRect.right = mItemOffset;
}

outRect.left = mItemOffset;
outRect.bottom = mItemOffset;
}
}

最佳答案

其实这不是height / width的问题的 RecyclerView .是你的问题ItemDecoration .尝试改变你的装饰如下:

@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent,
@NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);

int position = parent.getChildLayoutPosition(view);

GridLayoutManager manager = (GridLayoutManager) parent.getLayoutManager();

if (manager != null && position < manager.getSpanCount())
outRect.top = mItemOffset;

if (position % 2 == 0) {
outRect.right = mItemOffset / 2; // Right offset for left child
} else {
outRect.left = mItemOffset / 2; // Left offset for Right child
}

outRect.bottom = mItemOffset;
}

样本结果:每边均等的空间

enter image description here

关于android - RecyclerView 项目的高度和宽度相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59697449/

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