Android 공식 LinearLayoutManager 소스 코드 수정을 기반으로 무한 루프를 구현하기 위해 RecyclerView를 완벽하게 지원하는 LooperLinearLayoutManager

물 기사에서는 최근 오픈 소스인 LooperLinearLayoutManager 프로젝트를 소개하고, 다음 기사에서는 RecyclerView의 더 많은 사용 사례를 설명하겠습니다.

무한 루프에 대한 솔루션은 이미 많이 있습니다.하나는 Adapter를 수정하는 것이고 다른 하나는 RecyclerView.LayoutManager를 상속하여 LayoutManager를 사용자 정의하거나 단순히 LinearLayoutManager를 상속하여 관련 메서드를 다시 작성하는 것입니다.

그러나 저자는 위의 scheme이 아직 완벽하지 않다고 생각하며 공식 scheme에 가까워질수록 기존 LinearLayoutManager의 특정 구현을 기반으로 하고 무한 루프를 지원하도록 소스 코드를 기반으로 약간 수정해야 합니다. 이는 디버깅을 용이하게 하기 위해 마음대로 로그를 추가할 수 있다는 것입니다. , 해당 RecyclerView 소스 코드에 대한 이해를 향상시키기 위해.

1. LooperLinearLayoutManager 프로젝트 소개

LooperLinearLayoutManager Android의 공식 LinearLayoutManager 소스 코드 수정을 기반으로 무한 루프를 지원하는 LayoutManager입니다.

2. 효과 표시

git 용량이 너무 크고 압축이 심해 프레임률이 낮으니 해당 프로젝트를 다운받아 실행하여 실제 효과를 확인하는 것을 권장합니다.

3. 사용 방법:

1단계: 루트 디렉터리의 build.gradle 파일에서 repositories 태그 아래에 jitpack maven 리포지토리를 추가합니다.

리포지토리 끝에 루트 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