是否可以隐藏RecyclerView中最后一项的ItemDecoration?

时间:2017-06-22 20:05:41

标签: android android-recyclerview

我正在使用RecyclerView.ItemDecoration类在列表中创建分隔符,但我想隐藏列表中最后一项的分隔符。这可能不需要自己实施分隔线吗?

3 个答案:

答案 0 :(得分:5)

你可以试试这个,

public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
    private Drawable mDivider;

    public SimpleDividerItemDecoration(Context context) {
        mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        int childCount = parent.getAdapter().getItemCount();
        for (int i = 0; i < childCount; i++) {

            if (i == (childCount - 1)) {
                continue;
            }

            View child = parent.getChildAt(i);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + mDivider.getIntrinsicHeight();

            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}

答案 1 :(得分:0)

尝试由@Muthukrishnan Rajendran启发的Kotlin版本

class CommentDetailItemDecoration(
        context: Context
) : RecyclerView.ItemDecoration() {
    val drawable: Drawable = ContextCompat.getDrawable(context, R.drawable.ft_item_divider)!!

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        val left = parent.paddingLeft
        val right = parent.width - parent.paddingRight

        val childCount = parent.adapter!!.itemCount
        for (i in 0 until childCount - 1) {
            val child = parent.getChildAt(i)
            if (child != null) {
                val params = child.layoutParams as RecyclerView.LayoutParams
                val top = child.bottom + params.bottomMargin
                val bottom = top + drawable.intrinsicHeight
                drawable.setBounds(left, top, right, bottom)
                drawable.draw(c)
            }
        }
    }
}

答案 2 :(得分:-1)

<强> [UPDATE]
当您在RecyclerView中的项目视图具有不透明的背景时,此工作,例如,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ffffff">
 ... 
</LinearLayout>

RecyclerItemDecoration.getItemOffsets由RecyclerView调用以测量子位置。此解决方案将最后一个分隔符放在最后一个项目后面。因此,RecyclerView中的项目视图应该有一个背景来覆盖最后一个分隔符,这使它看起来像隐藏。

感谢@Rebecca Hsieh的澄清

您也可以这样做,(表单here):

recyclerView.addItemDecoration(
  new DividerItemDecoration(context, layoutManager.getOrientation())) {
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        // hide the divider for the last child
        if (position == parent.getAdapter().getItemCount() - 1) {
            outRect.setEmpty();
        } else {
            super.getItemOffsets(outRect, view, parent, state);
        }
    }
});