讓RecyclerView上面的部件跟隨其一起滑動實現(xiàn)方法

  • 在開發(fā)中遇到這樣一個問題,在recyclerview上方有其他的部件,然后需要讓RecyclerView和這個部件一起滑動,不需要RecyclerView單獨滑動。比如一篇文章,下面有很多條評論,這時就需要全部滑動,而不是讓評論單獨滑動。

實現(xiàn)方法

  • 首先使用NestedScrollView,讓其包裹RecycleView和它上面的部件
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:src="@drawable/xiaoxi"/>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
  • 這時RecyclerView就能跟著NestedScrollView一起滑動了,但是有個問題,就是RecycleRView顯示不完全,這里的解決辦法是:給NestedSrcollView添加屬性android:fillViewport="true"

完整代碼

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:src="@drawable/xiaoxi"/>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ??CoordinatorLayout作為頂層布局與NestedScrollView配合使用,可以用來協(xié)調(diào)子Vie...
    lmz14閱讀 11,447評論 3 44
  • 本文所講RecyclerView 是來自support 庫 26 版本,本文主要來源于自身開發(fā)及組內(nèi)同事遇到問...
    freddyyao閱讀 18,321評論 6 86
  • Android UI相關(guān)開源項目庫匯總OpenDigg 抽屜菜單MaterialDrawer ★7337 - 安卓...
    黃海佳閱讀 8,829評論 3 77
  • 開篇:關(guān)于iOS內(nèi)購整體流程網(wǎng)上能找到很多。我抽絲剝繭,著重說一下二次驗證及收據(jù)回傳的數(shù)據(jù)問題。 二次驗證 關(guān)于二...
    花果山松鼠閱讀 6,944評論 5 5
  • “別讓孩子輸在起跑線上”不知是哪個缺心眼的“磚家”說的,不過反正現(xiàn)在這句話成了我國孩子家長一道緊箍咒,緊緊得鎖在...
    北半球陸燈閱讀 254評論 0 0

友情鏈接更多精彩內(nèi)容