簡體   English   中英

使用折疊的工具欄滾動到recyclerview的最后一項

[英]Scrolling to last item of a recyclerview with a collapsing toolbar

我有一個CoordinatorLayout ,其中包含一個CollapsingToolbarLayoutRecyclerView 一切看起來都應該如此,只是當我嘗試以編程方式滾動到最后一項時,並沒有一直走到最底端。 而是這樣做:

在此處輸入圖片說明

我不認為這是一個裁剪問題,因為如果我向下滾動,最下面的項目將完全存在:

在此處輸入圖片說明

這是主要布局:

<?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)
    }
}

附加條款:

  • 如果我擺脫了CollapsingToolbarLayout那么它顯示整個最后一項。

  • 我在上面的代碼中留下了一些其他嘗試(注釋掉了)。 他們都沒有工作。

  • 這個示例僅涉及一個簡短的靜態列表,並且始終滾動到同一項目,但是我實際上正在處理的代碼要復雜一些。

  • 設計師真的希望所有東西看起來都完全符合設計,並且我不能隨意更改視覺設計。

如何滾動到帶有折疊工具欄的布局內的RecyclerView中的最后一項?

問題是NestedScrollingChildHelper#dispatchNestedScroll中的getNestedScrollingParentForType(type)對於非觸摸滾動返回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. 

解決此問題的方法是在滾動到給定位置之前折疊工具欄。 這可以通過在scrollToPosition之前添加app_bar_layout.setExpanded(false)來完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM