RecycleView定义分割线

1 自定义分割线

public class ListViewDecoration extends RecyclerView.ItemDecoration {

    private Drawable mDrawable;

    public ListViewDecoration() {
        mDrawable = ResCompat.getDrawable(App.getInstance(), R.drawable.divider_recycler);
    }

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

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount - 1; 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 + mDrawable.getIntrinsicHeight();
            mDrawable.setBounds(left, top, right, bottom);
            mDrawable.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State
            state) {
        outRect.set(0, 0, 0, mDrawable.getIntrinsicHeight());
    }
}

2  divider_recycler

<?xml version="1.0" encoding="utf-8"?>
<!--
    Copyright 2017 Yan Zhenjie

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#1A737373"/>

    <size android:height="2px"/>

</shape>

3 使用

  mDetailRv.setLayoutManager(new LinearLayoutManager(this));
        mDetailRv.addItemDecoration(new ListViewDecoration());

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RecyclerView 和 ListView 是 Android 开发中常用的两个列表控件,它们的实现上有以下区别: 1. ViewHolder 的实现方式不同: RecyclerView 使用 ViewHolder 模式来优化列表项的展示,ViewHolder 模式是一种缓存机制,用于减少每次滚动时创建新视图的数量。在 RecyclerView 中,ViewHolder 是一个内部类,它在 Adapter 中被实现。ViewHolder 会在列表项第一次创建时被创建并绑定到 RecyclerView 上,之后它会被重用。 ListView 也使用 ViewHolder 模式,但它需要手动实现 ViewHolder,而且 ViewHolder 必须是一个单独的类。 2. 布局管理器的实现方式不同: RecyclerView 使用布局管理器来定义列表项的布局方式,它可以实现各种布局方式,如线性布局、网格布局和瀑布流布局等。布局管理器是 RecyclerView 的一个内部类,需要在 Adapter 中设置。 ListView 使用 Adapter 中的 getView() 方法来定义列表项的布局方式,只能实现线性布局。 3. RecyclerView 支持动画效果: RecyclerView 支持添加、移除和移动列表项时的动画效果,可以使用默认的动画效果或自定义动画效果。 ListView 不支持动画效果。 4. RecyclerView 支持 ItemDecoration: RecyclerView 提供了一个 ItemDecoration 接口,用于在列表项之间添加分割线、边距等装饰效果。 ListView 不支持 ItemDecoration。 综上所述,RecyclerView 在实现上比 ListView 更灵活、更高效、更易于扩展和定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值