angcyo / DslTabLayout

:hearts: Android界最万能的TabLayout(不仅仅是TabLayout), 支持任意类型的item, 支持Drawable类型的指示器,智能开启滚动,支持横竖向布局等
https://github.com/angcyo/DslAdapter
MIT License
1.55k stars 145 forks source link

老哥,像垂直TabLayout库的这种效果有无? #58

Closed SaltedFish-Extreme closed 2 years ago

SaltedFish-Extreme commented 2 years ago

https://github.com/qstumn/VerticalTabLayout

左边的垂直tablayout联动右边的rv,互相联动,但是我不想rv滑动的时候左边的tab滑动又带动rv滑动一遍😂而且那个库的联动效果代码还挺多的😂

https://user-images.githubusercontent.com/54784104/155846112-2a49c07e-8c73-4796-ad22-3d6713ca72a7.mp4

angcyo commented 2 years ago

已经更新了相应的Demo代码, 可以在com.angcyo.dsltablayout.demo.sample.VerticalHintActivity类中查看.

效果图:

SaltedFish-Extreme commented 2 years ago

@angcyo 老哥,有没有用普通adapter的例子,这个dsl是真的看不懂😂或者框架能不能提供一个简单的adapter,我只需要设置数据就行了😂就像TabAdapter一样

class NavigationTabAdapter(private val dataList: List<NavigationResponse>) : TabAdapter {

    override fun getIcon(position: Int): ITabView.TabIcon? = null

    override fun getBadge(position: Int): ITabView.TabBadge? = null

    override fun getBackground(position: Int): Int = -1

    override fun getTitle(position: Int): ITabView.TabTitle {
        return ITabView.TabTitle.Builder()
            //标签文字
            .setContent(dataList[position].name)
            //标签文字选中及未选中颜色
            .setTextColor(ContextCompat.getColor(context, R.color.color_vertical_tab_layout_text), ContextCompat.getColor(context, R.color.gray_8f))
            .build()
    }

    override fun getCount(): Int = dataList.size

}
SaltedFish-Extreme commented 2 years ago

毕竟这个只需要设置tab的文字,其余的都在xml里设置了,要是有个简单的adapter就好了😂

angcyo commented 2 years ago

已经提供了一个普通RecyclerView.Adapter的示例代码版本.

SaltedFish-Extreme commented 2 years ago

已经提供了一个普通RecyclerView.Adapter的示例代码版本.

老哥,不对啊,我一开始以为你那个adapter是给tablayout设置的,我照着你的demo写了一遍才发现原来是给右侧rv设置的😂才发现你是把左侧的tablayout给写死了,我是从服务器获取数据之后,将字符串列表动态填充到tablayout里面😂我好像没找到你这个动态设置tablayout内容的方法😥

SaltedFish-Extreme commented 2 years ago

啊,我好像发现了一个,在DynamicActivity动态添加的,我试试去😂

angcyo commented 2 years ago

啊,我好像发现了一个,在DynamicActivity动态添加的,我试试去😂

啊? 原来你想问的是这个? 动态addView就阔以了.

SaltedFish-Extreme commented 2 years ago

啊? 原来你想问的是这个? 动态addView就阔以了.

是的,我RecyclerView是用BaseRecyclerViewAdapterHelper来填充的,那个TabAdapter只是给TabLayout填充数据的😂

SaltedFish-Extreme commented 2 years ago

@angcyo 老哥,已经用上了,效果可以,只是我感觉要是选择tab的选项卡,右侧的rv能平滑滚动就好了😁

https://user-images.githubusercontent.com/54784104/155940841-c7703b02-fea7-4e0d-8412-ee8e7d8e30f5.mp4

SaltedFish-Extreme commented 2 years ago

我之前是用的一个老哥写的左侧垂直tablayout和右侧rv联动的方法,不知道能不能对你有帮助😂

linkLeftRight()

/** 联动左边标签和右边rv */
    private fun linkLeftRight() {
        rv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (scroll && (newState == RecyclerView.SCROLL_STATE_IDLE)) {
                    scrollRecyclerView()
                }
                rightLinkLeft(newState)
            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                if (scroll) {
                    scrollRecyclerView()
                }
            }
        })

        verticalTabLayout.addOnTabSelectedListener(object : VerticalTabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabView?, position: Int) {}

            override fun onTabSelected(tab: TabView?, position: Int) {
                clickTab = true
                selectTab(position)
            }
        })
    }

    /** 右边rv联动左边标签 */
    private fun rightLinkLeft(newState: Int) {
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            if (clickTab) {
                clickTab = false
                return
            }
            val firstPosition: Int = linearLayoutManager.findFirstVisibleItemPosition()
            if (firstPosition != currentIndex) {
                currentIndex = firstPosition
                setChecked(currentIndex)
            }
        }
    }

    /** 滚动rv */
    private fun scrollRecyclerView() {
        scroll = false
        val indexDistance: Int = currentIndex - linearLayoutManager.findFirstVisibleItemPosition()
        if (indexDistance > 0 && indexDistance < rv.childCount) {
            val top: Int = rv.getChildAt(indexDistance).top
            rv.smoothScrollBy(0, top)
        }
    }

    /** 滚动右边rv,以选择左边标签 */
    private fun setChecked(position: Int) {
        if (clickTab) {
            clickTab = false
        } else {
            verticalTabLayout.setTabSelected(currentIndex)
        }
        currentIndex = position
    }

    /** 选择左边标签,以滚动右边rv */
    private fun selectTab(position: Int) {
        currentIndex = position
        rv.stopScroll()
        smoothScrollToPosition(position)
    }

    /** rv平滑滚动到指定位置 */
    private fun smoothScrollToPosition(position: Int) {
        val firstPosition: Int = linearLayoutManager.findFirstVisibleItemPosition()
        val lastPosition: Int = linearLayoutManager.findLastVisibleItemPosition()
        when {
            position <= firstPosition -> {
                rv.smoothScrollToPosition(position)
            }
            position <= lastPosition -> {
                val top: Int = rv.getChildAt(position - firstPosition).top
                rv.smoothScrollBy(0, top)
            }
            else -> {
                rv.smoothScrollToPosition(position)
                scroll = true
            }
        }
    }
angcyo commented 2 years ago

你只需要合理控制RV的滚动方式即可。

SaltedFish-Extreme commented 2 years ago

你只需要合理控制RV的滚动方式即可。

是这样,换成recyclerView.smoothScrollToPosition(index)就行了,麻烦老哥了😉