带有GridLayoutManager的RecyclerView中的项目的高度不为wrap_content

时间:2018-08-16 22:18:35

标签: android android-recyclerview gridlayoutmanager

以下是bank_card布局,该布局用于将每个项目的布局存储在RecyclerView中:

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

RecyclerView items not having wrap_content height

1 个答案:

答案 0 :(得分:0)

通过将 ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; position: fixed; top: 0; width: 100%; } li { float: left; border-right:1px solid #bbb; } li:last-child { border-right: 1px solid #bbb; float: left; } li a { display: block; color: white;    text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover:not(.active) { background-color: #111; } .active { background-color: #4CAF50; } 的高度设置为某个高度而不是CardView来解决此问题。感谢@Xixis提供答案