【问题标题】:Overscroll effect doesn't appear in my recyclerview inside swiperefreshlayoutswiperefreshlayout 内的我的 recyclerview 中没有出现过度滚动效果
【发布时间】:2018-02-06 05:48:21
【问题描述】:

我有一个包含 SwipeRefreshLayour 和 RecyclerView 的片段。

此 recyclerView 不显示过度滚动效果。我尝试了很多这样的东西:

 android:isScrollContainer="true"
 android:overScrollMode="always"

通过样式自定义颜色不起作用

fadingEdge、fadingEdgeLengh、需要FadingEdge...

滑动刷新正在工作,它显示如下:

我不明白:

【问题讨论】:

  • 如果您可以发布您的布局和活动/片段代码(或至少与此问题相关的部分),这将非常有助于回答。
  • 这不是布局或代码问题,我通过 android studio 模板 (ScrollingActivity) 创建了一个带有过度滚动的默认活动,并且在我的应用中它没有过度滚动效果。
  • 好的,我使用默认的 ScrollingActivity 创建了一个新应用程序,现在当同一个 ScrollingActivity 的旧应用程序过度滚动时,它没有过度滚动,我正在使用带有最新支持库的 Android Studio 3.0 beta 2 .也许它导致了这个错误?
  • 默认对我有用
  • 等一下,我会发布视频。请记住,您需要有速度才能触发它。我也在使用最新版本的android studio

标签: android android-recyclerview overscroll


【解决方案1】:

我只是通过覆盖 SwipeRefreshLayout 并使用它而不是默认实现来解决它:

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout

class SwipeRefreshLayoutWithRecyclerViewOverscroll : SwipeRefreshLayout {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    override fun onStartNestedScroll(child: View, target: View, nestedScrollAxes: Int): Boolean =
        (child.canScrollVertically(1) && super.onStartNestedScroll(child, target, nestedScrollAxes))

}

我正在使用一个 RecyclerView,它是一个线性垂直列表,child.canScrollVertically(1) 检查此列表当前是否位于最底部。如果在列表底部,onStartNestedScroll 返回 false,允许 RecyclerView 处理用户的上拉动作,从而显示过度滚动效果。

注意:这仅显示底部边缘过度滚动而不是顶部边缘,因为我不想看到下拉刷新动画与顶部过度滚动边缘。

【讨论】:

    【解决方案2】:

    在我实现这个功能时,(过度滚动在顶部和底部都起作用),我的布局是这样的:

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <...<custom view>...SwipeRefreshWrapper
        android:id="@+id/swiperefresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <RecyclerView
                android:id="@+id/video_recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    
            <include layout="@layout/include_video_list_empty_view"/>
            <include layout="@layout/include_watchlist_empty_view"/>
    
        </FrameLayout>
    <SwipeRefreshWrapper>
    

    而我的 SwipeRefreshWrapper 实现如下:

    /**
    * This Class ensures that a SwipeRefreshLayout will only refresh if the
    * list it wraps cannot scroll up anymore, (that it is at the top). Otherwise
    * when the user attempts to scroll up from the middle of the list, the refresh
    * will trigger. This is necessary for custom List implementations, (or
    * RecyclerViews), only.
    */
    public class SwipeRefreshWrapper extends SwipeRefreshLayout {
         private ScrollResolver mScrollResolver;
    
         public SwipeRefreshWrapper(Context context) {
             super(context);
         }
    
         public SwipeRefreshWrapper(Context context, AttributeSet attrs) {
             super(context, attrs);
         }
    
         public void setScrollResolver(ScrollResolver scrollResolver) {
             mScrollResolver = scrollResolver;
         }
    
         @Override
         public boolean canChildScrollUp() {
             if(mScrollResolver != null){
                 return mScrollResolver.canScrollUp();
             } else {
                 return super.canChildScrollUp();
             }
         }
    
         public static interface ScrollResolver{
             public boolean canScrollUp();
         }
    }
    

    然后在我的 Fragment 中设置 SwipeRefreshLayout:

     /**
     * Initializes PullToRefresh handler
     */
    private void initPullToRefresh() {
        mSwipeRefreshLayout = (SwipeRefreshWrapper) mView.findViewById(R.id.swiperefresh_layout);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
    
        mSwipeRefreshLayout.setScrollResolver(new SwipeRefreshWrapper.ScrollResolver() {
            @Override
            public boolean canScrollUp() {
                return mVideoRecycler.canScrollVertically(-1);
            }
        });
    
        mSwipeRefreshLayout.setOnRefreshListener(...)
    }
    

    【讨论】:

    • 我创建了一个滚动活动,甚至这个带有嵌套滚动的活动也没有过度滚动。我不明白为什么我的应用没有过度滚动效果
    • 在有很多项目的菜单中我有过度滚动效果,所以它只在活动元素上,如 recyclerview 或 nestedscrollview
    • 你能发布你的代码吗?您是否正在覆盖滚动事件?
    • 我没有覆盖此活动的任何滚动。它只是一个嵌套的滚动视图,带有一些元素来显示一些文本。真的是一个简单的活动。即使是java文件也没有滚动词。
    • 我通过模板创建了一个滚动活动,变化为零(我知道这个活动 100% 有过度滚动),但在我的应用程序中没有,所以应该是样式问题不是吗?
    猜你喜欢
    • 1970-01-01
    • 2016-10-24
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多