让RecyclerView上面的部件跟随其一起滑动实现方法

  • 在开发中遇到这样一个问题,在recyclerview上方有其他的部件,然后需要让RecyclerView和这个部件一起滑动,不需要RecyclerView单独滑动。比如一篇文章,下面有很多条评论,这时就需要全部滑动,而不是让评论单独滑动。

实现方法

  • 首先使用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>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  •   CoordinatorLayout作为顶层布局与NestedScrollView配合使用,可以用来协调子Vie...
    lmz14阅读 11,167评论 3 44
  • 本文所讲RecyclerView 是来自support 库 26 版本,本文主要来源于自身开发及组内同事遇到问...
    freddyyao阅读 18,149评论 6 86
  • Android UI相关开源项目库汇总OpenDigg 抽屉菜单MaterialDrawer ★7337 - 安卓...
    黄海佳阅读 8,759评论 3 77
  • 开篇:关于iOS内购整体流程网上能找到很多。我抽丝剥茧,着重说一下二次验证及收据回传的数据问题。 二次验证 关于二...
    花果山松鼠阅读 6,833评论 5 5
  • “别让孩子输在起跑线上”不知是哪个缺心眼的“砖家”说的,不过反正现在这句话成了我国孩子家长一道紧箍咒,紧紧得锁在...
    北半球陆灯阅读 225评论 0 0