In the water article, introduce the recently open source LooperLinearLayoutManager project, and the next article will explain more use cases of RecyclerView.
There are already many solutions for infinite loops. One is to modify the Adapter, and the other is to inherit RecyclerView.LayoutManager to customize LayoutManager or simply inherit LinearLayoutManager to rewrite related methods.
However, the author thinks that the above scheme is still not perfect. The closer to the official scheme should be based on the specific implementation of the existing LinearLayoutManager, and slightly modified on the basis of the source code to support infinite loops. Another advantage of this is that you can add logs at will to facilitate debugging. , to improve the understanding of the corresponding RecyclerView source code.
1. LooperLinearLayoutManager project introduction
LooperLinearLayoutManager
A LayoutManager that supports infinite loops based on the Android official LinearLayoutManager source code modification.
2. Effect display
The git is too large and the compression is severe, resulting in a low frame rate. It is recommended to download the project and run it to check the actual effect.
3. How to use:
Step 1: In the build.gradle file in your root directory, add the jitpack maven repository under the repositories tag:
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
If you use Gradle 7.0, add to the repositories tag of dependencyResolutionManagement in setting.gradle:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
...
maven { url 'https://jitpack.io' }
...
}
}
...
Step 2: Add dependency Add the dependency
dependencies {
implementation 'com.github.xiangang:LooperLinearLayoutManager:v1.0.0-alpha01'
}
4. Example of use
Exactly the same as the original LinearLayoutManager, just replace LinearLayoutManager with LooperLinearLayoutManager.
val bannerAdapter = BannerAdapter(requireContext(), dataList)
bannerRecyclerView.layoutManager = LooperLinearLayoutManager(
requireContext(), RecyclerView.HORIZONTAL, false
)
bannerRecyclerViewVertical.layoutManager = LooperLinearLayoutManager(
requireContext(), RecyclerView.VERTICAL, false
)
5. Use with PagerSnapHelper to achieve an infinite loop function similar to ViewPager
Note that if you use LinearSnapHelper, you will lose the ability of infinite loop due to the limitation of LinearSnapHelper itself. Therefore, LooperLinearLayoutManager recommends only using PagerSnapHelper to achieve an infinite loop effect. If you must use LinearSnapHelper, it is recommended to create a new LooperLinearSnapHelper class to inherit LinearSnapHelper and rewrite findTargetSnapPosition to remove the no-loop restriction.
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.