Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dividers didn't display in RecyclerView after addItemDecoration() called

A RecyclerView is nested in a CardView. I tried to call addItemDecoration() to add dividers into RecycylerView,but it didn't work.

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="@color/white_color"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    card_view:cardElevation="3dp">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_saleproduct_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_saleproduct_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:visibility="visible" />

    </android.support.v4.widget.SwipeRefreshLayout>
</android.support.v7.widget.CardView>

The java code:

    mSwipe = (SwipeRefreshLayout)view.findViewById(R.id.swipe_saleproduct_list);
    mRecyclerView= (RecyclerView) view.findViewById(R.id.recycler_saleproduct_list);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(UIUtils.getContext()));
    mRecyclerView.addItemDecoration(new DividerItemDecoration(UIUtils.getContext(), DividerItemDecoration.VERTICAL_LIST));
    SaleProductListAdapter saleProductListAdapter = new SaleProductListAdapter(UIUtils.getContext(), mRecyclerView, list, mSwipe);
    mRecyclerView.setAdapter(saleProductListAdapter);
like image 796
梁航斌 Avatar asked May 15 '15 06:05

梁航斌


1 Answers

This below is a good example, take a look at it:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {


    private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private Drawable mDivider;

    private int mOrientation;

    public DividerItemDecoration(Context context, int orientation) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        setOrientation(orientation);
    }

    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    public void drawVertical(Canvas c, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }
}

And here is my Fragment, it works well, which divider is clear and thin:

public static class RecyclerViewFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_recyclerview, container, false);

        RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));

        RecyclerViewAdapter adapter = new RecyclerViewAdapter(getActivity(), getResources()
            .getStringArray(R.array.countries));
        recyclerView.setAdapter(adapter);

        return root;
    }
}

I hope you will be inspired.

like image 50
SilentKnight Avatar answered Sep 17 '22 15:09

SilentKnight