Android 自定义 LayoutManager 实现头重式效果

前言

Android 自定义 LayoutManager 实现头重式效果_第1张图片

    之前学习了自定义RecyclerView的LayoutManager,一直无用武之地,最近看“爱奇艺阅读”时发现了个不错的RecyclerView LayoutManger样式,于是自己来实现一波,正好巩固下。

实现功能:

  • 头重式UI
  • 滑动item变化流畅
  • 子视图回收复用
  • 支持按页滑动,头页与左边界对齐
  • 可限制滑动后filling的页数
  • 支持无限轮播
  • 自定义子视图缩放大小及间距

效果图

Android 自定义 LayoutManager 实现头重式效果_第2张图片


使用说明

Dependency:

Step 1. Add the JitPack repository to your build file

allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

Step 2. Add the dependency

dependencies {
            implementation 'com.github.ljm17:TopHeavyLayoutManager:1.1.0'
    }

Explain:

构造方法:

//TopHeavyLayoutManager
TopHeavyLayoutManager();
TopHeavyLayoutManager(int space);
TopHeavyLayoutManager(float childScale, float coverScale);
TopHeavyLayoutManager(int space, float childScale, float coverScale);

//TopSnapHelper
TopSnapHelper(int fillingLimit);
参数 介绍
space 相邻child view之间的间距,默认 60 px
childScale 尾部child view的缩放值,默认 0.5f
coverScale 头部被覆盖的child view的缩放值,默认0.8f
fillingLimit 手指离开后filling的页数限制

如果想要实现无限循环,只需在RecyclerView.Adapter的getItemCount中返回Integer.MAX_VALUE,设置无限循环后,默认首个展示的item position为Integer.MAX_VALUE/2,若需调整请调用mRecyclerView.scrollToPosition(position)方法

*注意:不宜设置ItemDecoration,以免引起布局偏差。

Example:

mRecyclerView.setLayoutManager(new TopHeavyLayoutManager());
mRecyclerView.setAdapter(new xxxAdapter());
TopSnapHelper helper = new TopSnapHelper(2);
helper.attachToRecyclerView(mRecyclerView);

项目地址

    https://github.com/ljm17/TopHeavyLayoutManager
    如有问题欢迎指出,若对你有帮助给个star把~

你可能感兴趣的:(Android 自定义 LayoutManager 实现头重式效果)