android - 带有 GridLayoutManager 的 RecyclerView 中的项目的高度不是 wrap_content
问题描述
以下是我用来在 RecyclerView 中存储每个项目的布局的 bank_card 布局:
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/card_margin"
card_view:cardCornerRadius="@dimen/card_bank_radius"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/bank_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/thumbnail"
android:paddingLeft="@dimen/bank_name_padding"
android:paddingRight="@dimen/bank_name_padding"
android:paddingTop="@dimen/bank_name_padding"
android:textColor="@color/bankNameTitle"
android:textSize="@dimen/bank_name" />
</RelativeLayout>
</android.support.v7.widget.CardView>
这是我在 Fragment 中提供的代码onCreateView()
:
rootView = inflater.inflate(R.layout.fragment_qr_code, container, false);
RecyclerView recyclerView = rootView.findViewById(R.id.recycler_view);
bankList = new ArrayList<>();
adapter = new BankAdapter(getActivity() , bankList);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext() , 2);
recyclerView.setLayoutManager(layoutManager);
//dpToPx() returns the value of 8dp in px
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2 , dpToPx() , true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
prepareBanks(); //Adding banks to the RecyclerView
return rootView;
这是GridSpacingItemDecorator
我正在使用的自定义:
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;
private int spacing;
private boolean includeEdge;
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;
outRect.right = (column + 1) * spacing / spanCount;
if (position < spanCount) {
outRect.top = spacing;
}
outRect.bottom = spacing;
} else {
outRect.left = column * spacing / spanCount;
outRect.right = spacing - (column + 1) * spacing / spanCount;
if (position >= spanCount) {
outRect.top = spacing;
}
}
}
}
我期望的输出是 GridLayout 中的每个项目的高度固定为wrap_content
. 但我得到的是适配器中的每个项目都有一定的高度,如下图所示:
解决方案
通过将 的高度设置CardView
为某个高度而不是wrap_content
. 感谢@Xixis 提出答案
推荐阅读
- r - 仅当 R 中的条件为真时打印
- r - 显示错误标签的传单
- python - 有什么方法可以过早停止泡菜过程?
- google-apps-script - 将重定向 URI 添加到自动生成的 Google OAuth 2.0 客户端 ID
- javascript - 我怎样才能得到第一百个货币符号?
- javascript - 具有多个条目的 Webpack 5 ChunkGroup API?
- javascript - 通过自动发送数据获得高 reCaptcha 分数
- function - 在实现结构的默认特征并使用函数初始化其成员时,这些函数是评估一次还是多次?
- ubuntu - 在 Ubuntu 20 中安装 unixodbc-dev
- android - API 调用之间的 Firebase 令牌不一致