|
|
@@ -6,7 +6,9 @@ import android.graphics.Typeface
|
|
|
import android.graphics.drawable.Drawable
|
|
|
import android.util.AttributeSet
|
|
|
import android.util.TypedValue
|
|
|
+import android.view.GestureDetector
|
|
|
import android.view.LayoutInflater
|
|
|
+import android.view.MotionEvent
|
|
|
import android.view.View
|
|
|
import android.view.ViewGroup
|
|
|
import android.widget.LinearLayout
|
|
|
@@ -55,6 +57,15 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
private var tabPaddingHorizontal: Int = 4.dp()
|
|
|
private var tabPaddingVertical: Int = 0
|
|
|
private var fixedTabScrollableMode: Boolean = false
|
|
|
+ private var scrollToTop: (()->Unit)? = null
|
|
|
+ private val gestureDetector: GestureDetector by lazy {
|
|
|
+ GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
|
|
|
+ override fun onDoubleTap(e: MotionEvent): Boolean {
|
|
|
+ scrollToTop?.invoke()
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
|
|
|
init {
|
|
|
val typedArray: TypedArray = context.obtainStyledAttributes(
|
|
|
@@ -179,6 +190,7 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
) {
|
|
|
this.viewPager2 = viewPager2
|
|
|
this.tabFragmentStateAdapter = tabFragmentStateAdapter
|
|
|
+ this.scrollToTop = scrollToTop
|
|
|
viewPager2?.let {
|
|
|
TabLayoutMediator(
|
|
|
tabLayout, it
|
|
|
@@ -189,6 +201,10 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
} else {
|
|
|
tab.setCustomView(R.layout.layout_common_tab_item_match_parent)
|
|
|
}
|
|
|
+ tab.view.setOnTouchListener { v, event ->
|
|
|
+ gestureDetector.onTouchEvent(event)
|
|
|
+ false
|
|
|
+ }
|
|
|
updateTabView(
|
|
|
tab,
|
|
|
currentItem == position,
|
|
|
@@ -199,9 +215,6 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
}.attach()
|
|
|
}
|
|
|
tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
|
|
|
- private var lastClickTime = 0L
|
|
|
- private var lastClickPosition = -1
|
|
|
-
|
|
|
override fun onTabSelected(tab: TabLayout.Tab?) {
|
|
|
updateTabView(tab, true, null, showIndicator)
|
|
|
}
|
|
|
@@ -211,14 +224,6 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
}
|
|
|
|
|
|
override fun onTabReselected(tab: TabLayout.Tab?) {
|
|
|
- val currentTime = System.currentTimeMillis()
|
|
|
- val position = tab?.position ?: 0
|
|
|
- if (lastClickPosition == position && currentTime - lastClickTime < 300) {
|
|
|
- scrollToTop?.invoke()
|
|
|
- }
|
|
|
-
|
|
|
- lastClickTime = currentTime
|
|
|
- lastClickPosition = position
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
@@ -240,6 +245,7 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
scrollToTop: (() -> Unit)? = null
|
|
|
) {
|
|
|
this.viewPager2 = viewPager2
|
|
|
+ this.scrollToTop = scrollToTop
|
|
|
viewPager2?.let {
|
|
|
TabLayoutMediator(
|
|
|
tabLayout, it
|
|
|
@@ -250,6 +256,10 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
} else {
|
|
|
tab.setCustomView(R.layout.layout_common_tab_item_match_parent)
|
|
|
}
|
|
|
+ tab.view.setOnTouchListener { v, event ->
|
|
|
+ gestureDetector.onTouchEvent(event)
|
|
|
+ false
|
|
|
+ }
|
|
|
updateTabView(
|
|
|
tab,
|
|
|
currentItem == position,
|
|
|
@@ -259,8 +269,6 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
}.attach()
|
|
|
}
|
|
|
tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
|
|
|
- private var lastClickTime = 0L
|
|
|
- private var lastClickPosition = -1
|
|
|
|
|
|
override fun onTabSelected(tab: TabLayout.Tab?) {
|
|
|
updateTabView(tab, true)
|
|
|
@@ -271,14 +279,6 @@ class CommonTabLayout @JvmOverloads constructor(
|
|
|
}
|
|
|
|
|
|
override fun onTabReselected(tab: TabLayout.Tab?) {
|
|
|
- val currentTime = System.currentTimeMillis()
|
|
|
- val position = tab?.position ?: 0
|
|
|
- if (lastClickPosition == position && currentTime - lastClickTime < 300) {
|
|
|
- scrollToTop?.invoke()
|
|
|
- }
|
|
|
-
|
|
|
- lastClickTime = currentTime
|
|
|
- lastClickPosition = position
|
|
|
}
|
|
|
})
|
|
|
}
|