private NestedScrollingParentHelper helper = null; private boolean IsRefresh = true; private boolean IsLoad = true; //滑动的总距离 private int totalY = 0; private LinearLayout headerLayout = null; private MyRecyclerView myRecyclerView = null; private LinearLayout footerLayout = null;
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
if (IsRefresh) {
if (dy > 0) {
if (myRecyclerView.isOrientation(0)) {
totalY += dy;
if ((totalY / 2) <= 0) {
scrollTo(0, totalY / 2);
consumed[1] = dy;
} else {
scrollTo(0, 0);
consumed[1] = 0;
}
}
return;
}
}
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
if (totalY < 0 && myRecyclerView.isOrientation(0) || totalY > 0 && myRecyclerView.isOrientation(1)) {
isfling = true;
}
if (IsRefresh) {
if (dy > 0) {
if (myRecyclerView.isOrientation(0)) {
totalY += dy;
if ((totalY / 2) <= 0) {
scrollTo(0, totalY / 2);
consumed[1] = dy;
} else {
scrollTo(0, 0);
consumed[1] = 0;
}
}
return;
}
}
if (IsLoad) {
if (dy < 0) {
if (myRecyclerView.isOrientation(1)) {
totalY += dy;
if ((totalY / 2) >= 0) {
scrollTo(0, totalY / 2);
consumed[1] = dy;
} else {
scrollTo(0, 0);
consumed[1] = 0;
}
}
return;
}
}
}
//子view滑动结束调用
//dyUnconsumed < 0 向下滚
//dyUnconsumed > 0 向上滚
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
if (dyUnconsumed != 0) {
totalY += dyUnconsumed;
scrollTo(0, totalY / 2);
}
}
public void addHeaderView(View headerView, int headerHeight) {
this.headerLayout.removeAllViews();
this.headerLayout.addView(headerView);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, headerHeight);
layoutParams.topMargin = -headerHeight;
this.headerLayout.setLayoutParams(layoutParams);
}
public void addFooterView(View footerView, int footerHeight) {
this.footerLayout.removeAllViews();
this.footerLayout.addView(footerView);
this.footerLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, footerHeight));
}
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
return true;
}
public void onNestedScrollAccepted(View child, View target, int axes) {
helper.onNestedScrollAccepted(child, target, axes);
}
//父view拦截子view的滚动
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
if (totalY < 0 && myRecyclerView.isOrientation(0) || totalY > 0 && myRecyclerView.isOrientation(1)) {
isfling = true;
}
if (IsRefresh) {
if (dy > 0) {
if (myRecyclerView.isOrientation(0)) {
totalY += dy;
if ((totalY / 2) <= 0) {
scrollTo(0, totalY / 2);
consumed[1] = dy;
} else {
scrollTo(0, 0);
consumed[1] = 0;
}
}
return;
}
}
if (IsLoad) {
if (dy < 0) {
if (myRecyclerView.isOrientation(1)) {
totalY += dy;
if ((totalY / 2) >= 0) {
scrollTo(0, totalY / 2);
consumed[1] = dy;
} else {
scrollTo(0, 0);
consumed[1] = 0;
}
}
return;
}
}
}
//子view滑动结束调用
//dyUnconsumed < 0 向下滚
//dyUnconsumed > 0 向上滚
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
if (dyUnconsumed != 0) {
totalY += dyUnconsumed;
scrollTo(0, totalY / 2);
}
}
public void onStopNestedScroll(View child) {
helper.onStopNestedScroll(child);
if (onTouchUpListener != null) {
isfling = false;
onTouchUpListener.touchUp();
}
}
public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
return isfling;
}
public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
return isfling;
}
public int getNestedScrollAxes() {
return helper.getNestedScrollAxes();
}
private class MyRecyclerView extends RecyclerView {
private StaggeredGridLayoutManager staggeredGridLayoutManager = null;
private LinearLayoutManager linearLayoutManager = null;
private GridLayoutManager gridLayoutManager = null;
private boolean isScrollLoad = false;
private boolean isScrollRefresh = false;
public MyRecyclerView(Context context) {
super(context);
setVerticalFadingEdgeEnabled(false);
setHorizontalFadingEdgeEnabled(false);
setVerticalScrollBarEnabled(false);
setHorizontalScrollBarEnabled(false);
setOverScrollMode(OVER_SCROLL_NEVER);
setItemAnimator(new DefaultItemAnimator());
}
private void setMyLayoutManager(LayoutManager layoutManager) {
if (layoutManager instanceof StaggeredGridLayoutManager) {
staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
} else if (layoutManager instanceof GridLayoutManager) {
gridLayoutManager = (GridLayoutManager) layoutManager;
} else if (layoutManager instanceof LinearLayoutManager) {
linearLayoutManager = (LinearLayoutManager) layoutManager;
}
setLayoutManager(layoutManager);
if (!isVertical()) {
throw new NullPointerException("vertical!");
}
}
private boolean isOrientation(int orientation) {//orientation,0代表向下,1代表向上
if (orientation == 0)
return isCanPullDown();
else if (orientation == 1)
return isCanPullUp();
return false;
}
private boolean isCanPullDown() {
return !canScrollVertically(-1);
}
private boolean isCanPullUp() {
return !canScrollVertically(1);
}
// private int scrollLoad() {
// int lastItem = 0;
// int itemCount = 0;
// int spanCount = 1;
// if (staggeredGridLayoutManager != null) {
// lastItem = staggeredGridLayoutManager.findLastVisibleItemPositions(null)[0];
// itemCount = staggeredGridLayoutManager.getItemCount();
// spanCount = staggeredGridLayoutManager.getSpanCount();
// } else if (linearLayoutManager != null) {
// lastItem = linearLayoutManager.findLastVisibleItemPosition();
// itemCount = linearLayoutManager.getItemCount();
// spanCount = 1;
// } else if (gridLayoutManager != null) {
// lastItem = gridLayoutManager.findLastVisibleItemPosition();
// itemCount = gridLayoutManager.getItemCount();
// spanCount = gridLayoutManager.getSpanCount();
// }
// return ((itemCount - 1) / spanCount + 1) - (lastItem / spanCount + 1);
// }
private boolean isVertical() {
if (staggeredGridLayoutManager != null)
return staggeredGridLayoutManager.getOrientation() == StaggeredGridLayoutManager.VERTICAL;
else if (linearLayoutManager != null)
return linearLayoutManager.getOrientation() == LinearLayoutManager.VERTICAL;
else if (gridLayoutManager != null)
return gridLayoutManager.getOrientation() == GridLayoutManager.VERTICAL;
return false;
}
// public void onScrolled(int dx, int dy) {
// if (dy > 0 && !isScrollLoad) {
// if (oLior != null) {
// onScrollListener.scrollLoad(sc````````
`
``
llLoad());//传递滚动到倒数第几行
// }
// }
// }
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有