【问题标题】:RecyclerView smoothScrollToPosition() is not working with nestedScrollView. I want to scroll to the bottom whenever the fragment is createdRecyclerView smoothScrollToPosition() 不适用于nestedScrollView。每当创建片段时,我都想滚动到底部
【发布时间】:2020-06-03 21:15:24
【问题描述】:

我有一个嵌套的滚动视图,里面有一个图像视图和一个回收器视图。每当调用 onResume() 片段方法时,我希望回收器视图一直滚动到底部。 当前的行为是它像普通屏幕一样打开屏幕并且根本没有滚动。

我尝试了各种解决方案,但没有任何效果。

这是布局和片段类的代码。请帮我。什么都没有发生。

<androidx.core.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:fillViewport="true"
            android:focusableInTouchMode="true"
            android:fitsSystemWindows="true"
            app:layout_constraintBottom_toTopOf="@+id/btnCall"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            app:layout_constraintVertical_chainStyle="spread_inside">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/constraintLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:descendantFocusability="blocksDescendants"
                app:layout_constraintBottom_toTopOf="@+id/btnYouGave"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0"
                app:layout_constraintVertical_chainStyle="spread_inside">

                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/ivTransactionProfileImage"
                    android:layout_width="102dp"
                    android:layout_height="102dp"
                    android:layout_marginTop="16dp"
                    android:src="@drawable/ic_panda"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerViewTrnsaction"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    app:layout_constraintVertical_bias="0.0"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/ivTransactionProfileImage" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.core.widget.NestedScrollView>





override fun onResume() {
    super.onResume()
    mainActivity.supportActionBar?.title = args.name
    recyclerViewTrnsaction.layoutManager = LinearLayoutManager(activity)

    launch {
        context?.let {
            val transactions = getAllTransactions(args.id)
            recyclerViewTrnsaction.adapter = TransactionAdapter(transactions)
            nestedScrollView.smoothScrollTo(0, nestedScrollView.height)
            recyclerViewTrnsaction.smoothScrollToPosition(recyclerViewTrnsaction.adapter!!.itemCount)
        }
    }
}

【问题讨论】:

    标签: android kotlin android-recyclerview smooth-scrolling android-nestedscrollview


    【解决方案1】:

    尝试使用此滚动 NestedScrollView 而不是 RecyclerView

    将平滑滚动代码替换为

    nestedScrollView.post {
       nestedScrollView.fullScroll(View.FOCUS_DOWN)
     }
    

    【讨论】:

    • 嗨,Max,非常感谢,终于成功了。一个问题,这个“post”关键字有什么作用?,我是Android新手。有什么办法可以降低滚动速度,即增加动画时间?
    • 请帮我理解“post”关键字的作用?我对 android 和编程很陌生。
    • @AnmolSinghSahi 看看here
    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多