This commit is contained in:
2024-08-02 10:34:16 +08:00
parent d305c7809c
commit 9e889a2f14
42 changed files with 6797 additions and 27 deletions

1
ViewPager2Delegate/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,33 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 33
buildToolsVersion "30.0.2"
defaultConfig {
minSdkVersion 28
targetSdkVersion 33
versionCode 1
versionName "1.2"
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
//https://github.com/angcyo/DslTabLayout
implementation project(':TabLayout')
//https://mvnrepository.com/artifact/androidx.viewpager2/viewpager2
implementation 'androidx.viewpager2:viewpager2:1.0.0'
}
//apply from: "$gradleHost/master/publish.gradle"

View File

21
ViewPager2Delegate/proguard-rules.pro vendored Normal file
View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.angcyo.tablayout.delegate2" />

View File

@@ -0,0 +1,66 @@
package com.angcyo.tablayout.delegate2
import androidx.viewpager2.widget.ViewPager2
import com.angcyo.tablayout.DslTabLayout
import com.angcyo.tablayout.ViewPagerDelegate
import kotlin.math.absoluteValue
/**
* 兼容[ViewPager2]
* Email:angcyo@126.com
* @author angcyo
* @date 2019/12/14
*/
open class ViewPager2Delegate(
val viewPager: ViewPager2,
val dslTabLayout: DslTabLayout?,
val forceSmoothScroll: Boolean? = null
) : ViewPager2.OnPageChangeCallback(), ViewPagerDelegate {
companion object {
/**
* [forceSmoothScroll] 为 null, 只有切换左右page时, 才有VP的动画, 否则没有.
* */
fun install(
viewPager: ViewPager2,
dslTabLayout: DslTabLayout?,
forceSmoothScroll: Boolean? = null
): ViewPager2Delegate {
return ViewPager2Delegate(viewPager, dslTabLayout, forceSmoothScroll)
}
}
init {
viewPager.registerOnPageChangeCallback(this)
dslTabLayout?.setupViewPager(this)
}
override fun onGetCurrentItem(): Int {
return viewPager.currentItem
}
override fun onSetCurrentItem(
fromIndex: Int,
toIndex: Int,
reselect: Boolean,
fromUser: Boolean
) {
if (fromUser) {
val smoothScroll = forceSmoothScroll ?: ((toIndex - fromIndex).absoluteValue <= 1)
viewPager.setCurrentItem(toIndex, smoothScroll)
}
}
override fun onPageScrollStateChanged(state: Int) {
dslTabLayout?.onPageScrollStateChanged(state)
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
dslTabLayout?.onPageScrolled(position, positionOffset, positionOffsetPixels)
}
override fun onPageSelected(position: Int) {
dslTabLayout?.onPageSelected(position)
}
}