無圖無真相
項目中需要實現類似效果,找了幾個庫都不行,封裝一下供大家copy
CenterSelectTabLayout繼承自Recyclerview,CenterSelectTabLayoutAdapter繼承自RecyclerView.Adapter,使用起來和RecyclerView基本一樣
如何使用:
- add dependency
implementation 'com.sign.centerselecttablayout:centerselecttablayout:1.0.1'
- init view
<com.sign.centerselecttablayout.view.CenterSelectTabLayout
android:id="@+id/centerSelectTabLayout"
android:layout_width="match_parent"
android:layout_height="60dp" />
val exampleAdapter = ExampleAdapter()
centerSelectLayout.adapter = exampleAdapter
- init adapter ( 爲了處理centerSelectLayout.scrollToPosition(),需要實現這個方法 )
/**
* Return 選中tab的一半寬度 單位像素
*/
abstract fun getHalfOfSelectTabWidth(scrollPosition: Int): Int
- init listener ( 設置當tab被選中時的監聽 )
exampleAdapter.centerSelectTabLayoutListener = object :
CenterSelectTabLayoutAdapter.CenterSelectTabLayoutListener(
centerSelectLayout,
exampleAdapter
) {
override fun onTabSelect(position: Int) {
super.onTabSelect(position)
}
}
- scrollToPosition or smoothScrollToPosition ( 請求數據完畢或其它你需要設置初始選中tab的情況 )
centerSelectLayout.smoothScrollToPosition(10)
//centerSelectLayout.scrollToPosition(10)