使用折叠工具栏滚动到回收视图的最后一项

Scrolling to last item of a recyclerview with a collapsing toolbar

我有一个 CoordinatorLayout,其中包含一个 CollapsingToolbarLayout 和一个 RecyclerView。一切看起来都符合预期,除了当我尝试以编程方式滚动到最后一项时,它并没有一直滚动到底部。相反,它这样做:

我不认为这是一个裁剪问题,因为如果我向下滚动,底部的项目就完全存在:

这是主要布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                 xmlns:app="http://schemas.android.com/apk/res-auto"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent"
                                                 android:fitsSystemWindows="true">
    <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginStart="16dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:contentInsetStart="72dp"
                    app:popupTheme="@style/AppTheme.PopupOverlay"/>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/recyclerview_bottom_margin"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

下面是与上面的屏幕截图一起使用的代码:

class TestActivity : AppCompatActivity() {

    private val itemNames = listOf("top item", "next item", "yada", "yada yada", "yada yada yada", "second last item", "last item")

    private val selectedPosition = itemNames.size - 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.recyclerview_with_collapsing_toolbar)
        setSupportActionBar(toolbar)
        supportActionBar?.setTitle(R.string.some_title)

        val recyclerView  = findViewById<RecyclerView>(R.id.recycler_view)
        recyclerView.setHasFixedSize(true)
        val layoutManager = LinearLayoutManager(this)
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = MyAdapter()

        // try to scroll to the initial selected position

        recyclerView.scrollToPosition(selectedPosition)

//        layoutManager.scrollToPosition(selectedPosition)

//        layoutManager.scrollToPositionWithOffset(selectedPosition, resources.getDimensionPixelOffset(R.dimen.item_height))

//            recyclerView.post {
//                recyclerView.smoothScrollToPosition(selectedPosition)
//            }

    }

    inner class MyAdapter: RecyclerView.Adapter<MyViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, itemType: Int): MyViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
            return MyViewHolder(view)
        }

        override fun getItemCount(): Int {
            return itemNames.size
        }

        override fun onBindViewHolder(vh: MyViewHolder, position: Int) {
            vh.words.text = itemNames[position]
            if (selectedPosition == position) {
                vh.parent.setBackgroundColor(Color.MAGENTA)
            } else {
                vh.parent.setBackgroundColor(Color.BLACK)
            }
        }
    }

    inner class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        val parent = itemView
        val words: TextView = itemView.findViewById(R.id.some_text)
    }
}

补充说明:

如何滚动到带有折叠工具栏的布局中 RecyclerView 中的最后一项?

解决此问题的方法是在滚动到给定位置之前折叠工具栏。这可以通过在 scrollToPosition.

之前添加 app_bar_layout.setExpanded(false) 来完成

问题是 NestedScrollingChildHelper#dispatchNestedScroll 中的 getNestedScrollingParentForType(type) returns 对于非触摸滚动为 null,因此当以编程方式完成滚动时不会调度滚动。

所以我们需要在以编程方式滚动之前启用它:

if (!recyclerView.hasNestedScrollingParent(ViewCompat.TYPE_NON_TOUCH)) {
    recyclerView.startNestedScroll(View.SCROLL_AXIS_VERTICAL,ViewCompat.TYPE_NON_TOUCH);
}
// now smooth scroll your recycler view programmatically here.