【问题标题】:How to add fading edge only at the bottom of Scrollview/Recyclerview?如何仅在 Scrollview/Recyclerview 的底部添加褪色边缘?
【发布时间】:2020-06-18 09:37:53
【问题描述】:

我正在使用requiresFadingEdge=true在底部添加褪色效果,但问题是它在顶部也显示褪色效果?向下滚动时如何禁用顶部的淡入淡出效果?

我在底部附上了屏幕截图。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/grey">
  <android.support.v7.widget.RecyclerView
        android:id="@+id/list_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="?attr/actionBarSize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:spanCount="2"
        tools:itemCount="14"
        tools:layoutManager="android.support.v7.widget.GridLayoutManager"
        tools:listitem="@layout/layout_thermal_grid" />

 <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/pad_150dp"
        android:layout_alignParentBottom="true"
        android:requiresFadingEdge="vertical"
        android:layout_marginBottom="@dimen/pad_50dp"
        android:fadeScrollbars="true"
        android:fadingEdgeLength="@dimen/pad_120dp"></View>


</RelativeLayout>

【问题讨论】:

    标签: android android-recyclerview scrollview fading


    【解决方案1】:

    你可以这样使用:

    class BottomFadingEdgeScrollView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0
    ) : RecyclerView(context, attrs, defStyleAttr) {
    
        override fun getTopFadingEdgeStrength() = 0f
    }
    

    然后在你的布局文件中:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="@color/grey">
    
      <your.package.with.custom.widgets.BottomFadingEdgeScrollView
            android:id="@+id/list_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="?attr/actionBarSize"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:spanCount="2"
            tools:itemCount="14"
            tools:layoutManager="android.support.v7.widget.GridLayoutManager"
            tools:listitem="@layout/layout_thermal_grid" 
    
            android:fadingEdgeLength="@dimen/pad_120dp"
            android:requiresFadingEdge="vertical" />
    </RelativeLayout>
    

    【讨论】:

    • 仅供参考:这个解决方案引入了一个小错误,因为它覆盖了默认的defStyleAttrs,它的值是 R.attr.recyclerViewStyle,而不是 0。视图构造函数上的 JvmOverloads 通常会导致这样的错误,所以我的建议只定义两个参数之一,而不是使用 JvmOverload 注释
    猜你喜欢
    • 2011-09-15
    • 1970-01-01
    • 2011-02-08
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 2013-06-14
    相关资源
    最近更新 更多