美文网首页
双层嵌套RecyclerView整屏滑动

双层嵌套RecyclerView整屏滑动

作者: 小赵不在 | 来源:发表于2020-12-22 14:20 被阅读0次

    1. 布局

    <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@mipmap/bg_aux_middle"
            app:layout_constraintBottom_toBottomOf="@+id/cl_chinese"
            app:layout_constraintDimensionRatio="0.653"
            app:layout_constraintHeight_percent="0.581"
            app:layout_constraintHorizontal_bias="0.05"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@+id/cl_writing"
            app:layout_constraintVertical_bias="0"
            >
    
            <TextView
                android:id="@+id/tv_sync"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/px_15"
                android:text="同步大讲堂"
                android:textColor="@color/white"
                android:layout_centerHorizontal="true"
                android:textSize="@dimen/sp_22"
                android:textStyle="bold" />
    
            <ImageView
                android:id="@+id/tv_sync_class_more"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentBottom="true"
                android:layout_marginTop="@dimen/px_20"
                android:layout_marginEnd="@dimen/px_25"
                android:paddingBottom="@dimen/px_20"
                android:src="@mipmap/icon_sync_more" />
    
            <TextView
                android:id="@+id/tv_page_pre"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginEnd="@dimen/px_50"
                android:layout_toLeftOf="@+id/tv_page_next"
                android:drawableLeft="@mipmap/icon_page_pre"
                android:drawablePadding="@dimen/px_5"
                android:gravity="center"
                android:paddingTop="@dimen/px_5"
                android:paddingBottom="@dimen/px_15"
                android:text="上一页"
                android:textColor="@color/white"
                android:textSize="@dimen/sp_18" />
    
            <TextView
                android:id="@+id/tv_page_next"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginEnd="@dimen/px_50"
                android:layout_toLeftOf="@+id/tv_sync_class_more"
                android:drawableLeft="@mipmap/icon_page_next"
                android:drawablePadding="@dimen/px_5"
                android:gravity="center"
                android:paddingTop="@dimen/px_5"
                android:paddingBottom="@dimen/px_15"
                android:text="下一页"
                android:textColor="@color/white"
                android:textSize="@dimen/sp_18" />
    
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_sync"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@id/tv_page_next"
                android:layout_below="@+id/tv_sync" />
        </RelativeLayout>
    

    2. Activity中的代码 双层嵌套RecyclerView整屏滑动

     private val scrollDistance2NextPage = 50
    
        inner class CrossRvScrollListener : RecyclerView.OnScrollListener() {
            var preScrollState = 0
            var preY = 0f
            val SCREEN_HEIGHT = 1080//d60s
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                val state = preScrollState
                preScrollState = newState
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                }
                if (rv_sync.getChildAt(0) == null) return
                if (state == RecyclerView.SCROLL_STATE_IDLE && newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                    val position =
                        (rv_sync.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
                    preY = rv_sync.getChildAt(0).y - position * SCREEN_HEIGHT
                } else if (state == RecyclerView.SCROLL_STATE_DRAGGING && newState != RecyclerView.SCROLL_STATE_DRAGGING) {
                    val position =
                        (rv_sync.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
                    val y: Float = rv_sync.getChildAt(0).y - position * SCREEN_HEIGHT
                    val scrolledY = y - preY
                    var newPosition = -10
                    if (scrolledY < 0) { //向右
                        newPosition = if (scrolledY <= 0 - scrollDistance2NextPage) {
                            position + 1
                        } else {
                            position
                        }
                    }
                    if (scrolledY > 0) { //向左
                        newPosition = if (scrolledY < scrollDistance2NextPage) {
                            position + 1
                        } else {
                            position
                        }
                    }
                    if (newPosition != -10) {
                        rv_sync.smoothScrollToPosition(newPosition)
                    }
                }
            }
        }
    
        private fun prePage() {
            val position = (rv_sync.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
            if (position == 0) {
                ToastUtils.showShort("当前已是第一页")
                return
            }
            rv_sync.smoothScrollToPosition(position - 1)
        }
    
        private fun nextPage() {
            val position = (rv_sync.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
            if (position == rv_sync.adapter?.itemCount!! - 1) {
                ToastUtils.showShort("当前已是最后一页")
                return
            }
            rv_sync.smoothScrollToPosition(position + 1)
        }
    

    相关文章

      网友评论

          本文标题:双层嵌套RecyclerView整屏滑动

          本文链接:https://www.haomeiwen.com/subject/xhaagktx.html