Android公式LinearLayoutManagerのソースコード修正をベースに無限ループを実現するRecyclerViewに完全対応したLooperLinearLayoutManager

水の記事では、最近オープンソースになった LooperLinearLayoutManager プロジェクトを紹介します。次の記事では、RecyclerView のさらなる使用例について説明します。

無限ループに対する解決策はすでに数多くあります。1 つはアダプターを変更することで、もう 1 つは RecyclerView.LayoutManager を継承して LayoutManager をカスタマイズするか、単純に LinearLayoutManager を継承して関連メソッドを書き換える方法です。

ただし、上記のスキームはまだ完全ではないと著者は考えており、公式のスキームに近づけるには、既存の LinearLayoutManager の特定の実装に基づいて、無限ループをサポートするためにソース コードをベースにわずかに変更する必要があります。これは、デバッグを容易にするために任意にログを追加できるため、対応する RecyclerView ソース コードの理解が向上します。

1. LooperLinearLayoutManager プロジェクトの紹介

LooperLinearLayoutManager Android の公式 LinearLayoutManager ソース コードの修正に基づいて無限ループをサポートする LayoutManager。

2.エフェクト表示

gitが大きすぎて圧縮が厳しいためフレームレートが低いため、プロジェクトをダウンロードして実行して実際の効果を確認することをお勧めします。

3. 使用方法:

ステップ 1: ルート ディレクトリの build.gradle ファイルで、jitpack Maven リポジトリを repositories タグの下に追加します。

リポジトリの最後にあるルート build.gradle に追加します。

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

Gradle 7.0 を使用する場合は、setting.gradle の dependencyResolutionManagement のリポジトリ タグに追加します。

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

ステップ 2: 依存関係を追加する 依存関係を追加する

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

ライセンス

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.

おすすめ

転載: blog.csdn.net/xiangang12202/article/details/122499140