【问题标题】:Recyclerview scrollbar cliptoPadding false?Recyclerview滚动条cliptoPadding false?
【发布时间】:2018-01-26 02:00:23
【问题描述】:

我有一个 recyclerview,顶部和底部填充为 10dp。

然后我像这样添加clipToPadding=false

<android.support.v7.widget.RecyclerView
    android:id="@+id/contentView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingStart="@dimen/margin_medium"
    android:paddingTop="@dimen/margin_medium"
    android:paddingBottom="@dimen/margin_medium"
    android:clipToPadding="false"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollbars="vertical" />

我注意到我的滚动条实际上​​会尊重顶部和底部填充,并且不会滚动整个高度。

有没有办法让我的滚动条真正上下滚动?

【问题讨论】:

    标签: android android-layout android-recyclerview padding


    【解决方案1】:

    解决此问题的最快方法是选择 RecyclerView 上的新快速滚动条:

    根据文档版本 26.0.0:

    RecyclerView 的新 fastScrollEnabled 布尔标志。如果启用, fastScrollHorizo​​ntalThumbDrawable, fastScrollHorizo​​ntalTrackDrawable, fastScrollVerticalThumbDrawable 和 fastScrollVerticalTrackDrawable 必须设置。

    所以设置两个文件:

    track_drawable.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@color/state_normal_track" />
    </selector>
    

    thumb_drawable.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_pressed="true" android:drawable="@color/colorAccent" />
      <item android:drawable="@color/state_normal_scrollbar_thumb" />
    </selector>
    

    这是我使用的颜色:

    <!-- Fast Scroll track state colors -->
    <color name="state_normal_track">#EEEEEE</color>
    
    <!-- Scroll Bar thumb state colors -->
    <color name="state_normal_scrollbar_thumb">#9E9E9E</color>
    

    终于在你的 RecyclerView 上:

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="10dp"
        android:paddingStart="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:clipToPadding="false"
        app:fastScrollEnabled="true"
        app:fastScrollVerticalTrackDrawable="@drawable/vertical_track_drawable"
        app:fastScrollHorizontalTrackDrawable="@drawable/vertical_track_drawable"
        app:fastScrollVerticalThumbDrawable="@drawable/vertical_thumb_drawable"
        app:fastScrollHorizontalThumbDrawable="@drawable/vertical_thumb_drawable" />
    

    祝你好运,编码愉快!

    【讨论】:

      【解决方案2】:

      您只需将android:scrollbarStyle="outsideOverlay" 添加到您的recyclerView。

      参考-https://developer.android.com/reference/android/view/View.html#attr_android:scrollbarStyle

      你可以用它实现类似的滚动条效果。

      【讨论】:

        猜你喜欢
        • 2014-09-14
        • 1970-01-01
        • 2014-07-04
        • 2014-10-08
        • 2016-03-20
        • 1970-01-01
        • 2016-05-25
        • 2016-11-24
        • 1970-01-01
        相关资源
        最近更新 更多