【问题标题】:Items in RecyclerView with GridLayoutManager has height not wrap_content带有 GridLayoutManager 的 RecyclerView 中的项目的高度不是 wrap_content
【发布时间】:2018-08-16 22:18:35
【问题描述】:

以下是我用来在 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 。但我得到的是适配器中的每个项目都有一定的高度,如下图所示:

【问题讨论】:

  • 检查 xml 中的 android:layout_below="@id/thumbnail",因为我没有看到任何 ID 为 thumbnail 的视图。否则还要设置卡片视图的高度
  • 为了简单起见,我删除了那部分代码,以便将其放在 StackOverflow 上。它存在于实际代码中。 PS。它的存在根本不重要。
  • 谢谢。设置高度为我修复了它

标签: android android-recyclerview gridlayoutmanager


【解决方案1】:

通过将 CardView 的高度设置为某个高度而不是 wrap_content 来解决此问题。感谢@Xixis 提出答案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2016-04-30
    • 2020-02-17
    相关资源
    最近更新 更多