android 自動選中中心tab,滾動停止後選中tab自動回滾到佈局中心的CenterSelectTabLayout

github地址

無圖無真相
在這裏插入圖片描述

項目中需要實現類似效果,找了幾個庫都不行,封裝一下供大家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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章