我有一个片段,其中包含一个 SwipeRefreshLayour,里面有一个 RecyclerView。
此 recyclerView 不显示过度滚动效果。我尝试了很多这样的事情:
android:isScrollContainer="true"
android:overScrollMode="always"
通过样式自定义颜色不起作用
fadingEdge,fadingEdgeLengh,需要FadingEdge ...
滑动刷新正在工作,它显示了这一点:
我不明白:
我有一个片段,其中包含一个 SwipeRefreshLayour,里面有一个 RecyclerView。
此 recyclerView 不显示过度滚动效果。我尝试了很多这样的事情:
android:isScrollContainer="true"
android:overScrollMode="always"
通过样式自定义颜色不起作用
fadingEdge,fadingEdgeLengh,需要FadingEdge ...
滑动刷新正在工作,它显示了这一点:
我不明白:
我只是通过覆盖 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 处理用户的上拉动作,从而显示过度滚动效果。
注意:这只显示底部边缘过度滚动而不是顶部边缘,因为我不想看到下拉刷新动画和顶部过度滚动边缘。
在我实现这个功能时,(过度滚动在顶部和底部都起作用),我的布局是这样的:
<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();
}
}
然后在我的片段中设置 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(...)
}