一种是SwipeRefreshLayout+RecyclerView在同一个界面的,
可以通过重写swipe、或者重写recycler控件,来解决。
这也是比较容易的
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener(){
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int topRowVerticalPosition =
(recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
swipeRefreshLayout.setEnabled(topRowVerticalPosition >= 0);
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
});
public class FixedRecyclerView extends RecyclerView {
public FixedRecyclerView(Context context) {
super(context);
}
public FixedRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FixedRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean canScrollVertically(int direction) {
// check if scrolling up
if (direction < 1) {
boolean original = super.canScrollVertically(direction);
return !original && getChildAt(0) != null && getChildAt(0).getTop() < 0 || original;
}
return super.canScrollVertically(direction);
}
}
ublic class SwipeRefreshLayout extends android.support.v4.widget.SwipeRefreshLayout
{
public SwipeRefreshLayout(Context context)
{
super(context);
}
public SwipeRefreshLayout(Context context,AttributeSet attrs)
{
super(context,attrs);
}
@Override
public boolean canChildScrollUp()
{
View target=getChildAt(0);
if(target instanceof AbsListView)
{
final AbsListView absListView=(AbsListView)target;
return absListView.getChildCount()>0
&&(absListView.getFirstVisiblePosition()>0||absListView.getChildAt(0)
.getTop()else
return ViewCompat.canScrollVertically(target,-1);
}
}
还有一种是存在ViewPager的页面,
比如像这种,不单存在ViewPage,而且外层还加了一个滚动视图ScrollView。
需求是,当item处于顶端,再刷新。
也就是需要从Activity里监听Fragment中item的状态。
/*通过Fragmenbt的滚动距离回调
* 注意:这里只能处理当ScrollLayout完全隐藏后,Fragment的ScrollView才能开始滚动事件,
* 上滑的时候却优先执行ScrollLayout的方法
*/
public void getonScroll(int i) {
srl.setEnabled(i<450)
}
Fragment中回调
如果RecyclerView外层还有ScrollView,还要处理滑动冲突
方法是给RecyclerView设置android:nestedScrollingEnabled="false"
属性,将滚动事件交给父滚动控件,并且在监听的时候监听scrollView的滚动事件
scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {
Log.e("i======", "i:" + i + ",i1:" + i1 + ",i2:" + i2 + ",i3:" + i3);
if (i1>300) {
activity.getonScroll(i1);
}
}
});
或者监听RecyclerView
//这里获取不到recycler的监听事件,因为ScrollView冲突问题,在布局将滚动焦点交给了ScrollView处理
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
activity.getonScroll(dy);
}
});
想看详细源码可以参考:https://github.com/wapchief/imitationLOL
是一个仿掌上英雄联盟的练手项目,欢迎star。