基于Android官方LinearLayoutManager源码修改的完美支持RecyclerView实现无限循环的LooperLinearLayoutManager

导读:本篇文章讲解 基于Android官方LinearLayoutManager源码修改的完美支持RecyclerView实现无限循环的LooperLinearLayoutManager,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

水一篇,介绍下最近开源的LooperLinearLayoutManager项目,下一篇会讲解RecyclerView的更多使用案例。

无限循环目前已经有挺多方案了,一种是对Adapter进行修改,一种是继承RecyclerView.LayoutManager自定义LayoutManager或干脆继承LinearLayoutManager重写相关方法。

但以上方案笔者认为还是不完美的,更加接近官方的方案应该是基于现有LinearLayoutManager的具体实现,在源码基础上稍作修改支持无限循环,而且这样的另一个好处是还可以随意加日志方便调试,提高对应RecyclerView源码的理解。

1.LooperLinearLayoutManager项目介绍

LooperLinearLayoutManager 基于Android官方LinearLayoutManager源码修改实现的支持无限循环的LayoutManager。

2. 效果展示

git太大,压缩的厉害,导致帧率太低,建议下载工程运行查看实际效果。

基于Android官方LinearLayoutManager源码修改的完美支持RecyclerView实现无限循环的LooperLinearLayoutManager

3. 使用方法:

第一步: 在你的根目录中的 build.gradle 文件中,repositories 标签下添加jitpack maven仓库:

Add it in your root build.gradle at the end of repositories:

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

若使用Gradle 7.0 ,则在setting.gradle中的dependencyResolutionManagement的repositories标签中添加:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
		...
        maven { url 'https://jitpack.io' }
        ...
    }
}
...

第二步: 添加依赖 Add the dependency

dependencies {
	implementation 'com.github.xiangang:LooperLinearLayoutManager:v1.0.0-alpha01'
}

4. 使用示例

跟原来的使用LinearLayoutManager一模一样,只需将LinearLayoutManager替换为LooperLinearLayoutManager。

val bannerAdapter = BannerAdapter(requireContext(), dataList)
bannerRecyclerView.layoutManager = LooperLinearLayoutManager(
    requireContext(), RecyclerView.HORIZONTAL, false
)
bannerRecyclerViewVertical.layoutManager = LooperLinearLayoutManager(
    requireContext(), RecyclerView.VERTICAL, false
)

5. 配合PagerSnapHelper使用实现类似ViewPager的无限循环功能

注意,若使用LinearSnapHelper,将由于LinearSnapHelper本身的限制而失去无限循环的能力。因此LooperLinearLayoutManager建议只搭配PagerSnapHelper实现无限循环的效果。若一定要使用LinearSnapHelper,建议新建一个LooperLinearSnapHelper类继承LinearSnapHelper并重写findTargetSnapPosition解除无循环限制。

val bannerRecyclerView = binding.bannerRecyclerview
val bannerRecyclerViewVertical = binding.bannerRecyclerviewVertical

val bannerAdapter = BannerAdapter(requireContext(), dataList)
bannerRecyclerView.layoutManager = LooperLinearLayoutManager(
    requireContext(), RecyclerView.HORIZONTAL, false
)
bannerRecyclerViewVertical.layoutManager = LooperLinearLayoutManager(
    requireContext(), RecyclerView.VERTICAL, false
)

val snapHelper = PagerSnapHelper()
snapHelper.attachToRecyclerView(bannerRecyclerView)
val snapHelperVertical = PagerSnapHelper()
snapHelperVertical.attachToRecyclerView(bannerRecyclerViewVertical)

bannerRecyclerView.adapter = bannerAdapter
bannerRecyclerViewVertical.adapter = bannerAdapter

License

Copyright 2022 xiangang

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.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/116890.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!