自定义LayoutManager仿探探切卡片效果

探探效果:
demo效果如下:
LayoutManager自定义

对于自定义LayoutManager我们主要处理好三件事情,就所有的问题就都迎刃而解了:

布局每个ItemView
处理滑动事件
缓存重用ItemView

LayoutManager自定义时常用函数介绍

这里定义RecyclerView里面每个item默认的LayoutParams

@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
    return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}

重写这个函数来布局RecyclerView当前需要显示的item,确定每个item的位置

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
    super.onLayoutChildren(recycler, state);
    //TODO:来布局RecyclerView当前需要显示的item
}
处理滑动需要重写的函数:
//是否可以水平滑动
@Override
public boolean canScrollHorizontally() {
    return true;
}

//canScrollHorizontally返回true的基础上,RecyclerView有手指水平滑动的时候回调
//该函数 * 注意处理完之后要返回消费的距离
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
    //TODO:滑动逻辑
    return dx;
}

//是否可以垂直滑动
@Override
public boolean canScrollVertically() {
    return true;
}

//canScrollVertically返回true的基础上,RecyclerView有手指垂直滑动的时候回调该函数 * 注意处理完之后要返回消费的距离
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
    //TODO:滑动逻辑
    return dy;
}
缓存重用ItemView
/* detach轻量回收View */
detachAndScrapAttachedViews(recycler);
detachAndScrapView(view, recycler);
/** * recycle真的回收一个View ,该View再次回来需要执行onBindViewHolder方法 */
removeAndRecycleView(View child, Recycler recycler)
removeAndRecycleAllViews(Recycler recycler);
/** * 超级轻量回收一个View,马上就要添加回来,和attachView()对应 */
detachView(view);
/** * 将detachView(view) detach的View attach回来 */
attachView(view);
/** * detachView 后 没有attachView的话 就要真的回收掉他们 */
recycler.recycleView(viewCache.valueAt(i));
步骤一:写一个普通recyclerView垂直列表。
步骤二:自定义LayoutManager,重写onLayoutChildren自行处理子view位置,根据position不同处理各自的大小和偏移。
public class SlideCardLayoutManager extends RecyclerView.LayoutManager {
    private final int MAX_CARD_COUNT = 4;
    private final float SCALE_RATIO = 0.08f;
    private final float TRANSITION_Y = 30f;

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    //必须要重写,对item设置放置规则
    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        //直接使用父类的回收功能
        detachAndScrapAttachedViews(recycler);

        int itemCount = getItemCount();  //当前adapter中总共item数
        int bottomPosition;

        if (itemCount < MAX_CARD_COUNT) {
            bottomPosition = 0;
        } else {
            bottomPosition = itemCount - MAX_CARD_COUNT;
        }

        //遍历最下层position到最上层postion的view
        for (int i=bottomPosition;i

使用该layoutManager,就可以实现从小到大层叠放置卡片效果了,只是,这个时候没有拖动事件。

步骤三:自定义ItemTouchHelper,重写onChildDraw方法,在拖拽过程中遍历子view做放大效果。
public class SlideCardCallBack extends ItemTouchHelper.SimpleCallback {
    private RecyclerView.Adapter adapter;
    private ArrayList datas;
    private final float SCALE_RATIO = 0.08f;
    private final float TRANSITION_Y = 30f;
    /**
     *     构造方法设置可滑动方向,上下左右 0x1111,参数一:拖拽  参数er:滑动
     *     public static final int UP = 1;
     *     public static final int DOWN = 1 << 1;
     *     public static final int LEFT = 1 << 2;
     *     public static final int RIGHT = 1 << 3;
     */
    public SlideCardCallBack(RecyclerView.Adapter adapter, ArrayList datas) {
        super(0, 15); //15表示支持各个方向拖拽
        this.adapter = adapter;
        this.datas = datas;
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        return false;
    }

    //滑动动画结束后的操作
    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        //这里需要将最顶层滑出去的data移除,然后添加到最底层,保证切换能循环进行
        Object remove = datas.remove(viewHolder.getLayoutPosition());
        datas.add(0, remove);
        adapter.notifyDataSetChanged();
    }

    //绘制拖拽过程中方法的效果
    @Override
    public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {

        //dx  dy分别表示当前拖拽view相对于原来横纵坐标偏移
        double distance = Math.sqrt(dX*dX+dY*dY);
        double maxDistance = recyclerView.getWidth() * 0.5f;
        double fraction = distance / maxDistance;  //偏移放大系数
        if (fraction > 1) {
            fraction = 1;  //放大系数最大为1
        }
        //遍历,为每个child执行动画
        int itemCount = recyclerView.getChildCount();
        for (int i=0;i

给recyclerView设置ItemTouchHelper,设置拖拽事件,并在拖拽过程中,将每一个子view做放大处理。

MainActivity代码:
class MainActivity : AppCompatActivity() {
    private var recyclerView: RecyclerView?= null
    private var adapter: CardAdapter?= null
    private var datas = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerView = findViewById(R.id.recyclerview)
        adapter = CardAdapter(applicationContext)
        recyclerView?.layoutManager = SlideCardLayoutManager()
        recyclerView?.adapter = adapter

        initDatas()
    }

    private fun initDatas() {
        datas.add(CardBean(R.mipmap.wanghong1))
        datas.add(CardBean(R.mipmap.wanghong2))
        datas.add(CardBean(R.mipmap.wanghong3))
        datas.add(CardBean(R.mipmap.wanghong4))
        datas.add(CardBean(R.mipmap.wanghong5))
        datas.add(CardBean(R.mipmap.wanghong6))
        adapter?.setDatas(datas)

        ItemTouchHelper(SlideCardCallBack(adapter, datas)).attachToRecyclerView(recyclerView)
    }
}
Adapter代码:
class CardAdapter(val context: Context): RecyclerView.Adapter() {
    private var datas = ArrayList()

    fun setDatas(datas: ArrayList) {
        this.datas = datas
        notifyDataSetChanged()
    }

    class CardViewHolder(itemView: View) : ViewHolder(itemView) {
        var imageView:ImageView?= null

        init {
            imageView = itemView.findViewById(R.id.iv_cover)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder {
        return CardViewHolder(LayoutInflater.from(context).inflate(R.layout.item, parent, false))
    }

    override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
        holder.imageView?.setImageResource(datas[position].coverRes)
    }

    override fun getItemCount() = datas.size
}

你可能感兴趣的:(自定义LayoutManager仿探探切卡片效果)