在android应用中怎么添加一个上拉刷新下拉加载功能
在android应用中怎么添加一个上拉刷新下拉加载功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站制作、成都网站建设、外贸营销网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的资阳网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
下拉刷新
首先我们给出如下几个参数,后面要用:
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;
既然是刷新,我们的滚动肯定是在父view之前的。所以我们需要在onNestedPreScroll这个方法里面写上我们所需要改动的x,y值。
我们需要用父view去拦截它。
我们需要判断dy的值是否大于0,因为大于0是刷新操作,小于0是加载操作。然后我们需要判断recyclerview是否是纵向的而不是横向的。
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; } }
上拉加载
上面我也说了onNestedPreScroll这个方法中判断dy<0才是加载操作。所以综上所述,代码变成了这样:
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滑动结束后,实行如下操作:
//子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); } }
其实最主要的两个方法已经解决了,其他到没什么了,这边,我把nestedscrolling的8个接口的功能和自定义recyclerview放出来。已变大家参考。希望大家都能实现自己的刷新加载。告别swiperefreshlayout。
添加header和footer
这里我们参考listview自带的addheaderview和addfooterview。代码如下:
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(); }
自定义recyclerview
既然是自己写的刷新加载框架,总不能还有自定义layout中在放个recyclerview。多麻烦,自定义一个,直接放在里面,然后分别放个header和footer 就没必要每次有页面用到刷新都要写一个布局。3个布局解决整个项目的刷新和加载。话不多说,代码如下:
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());//传递滚动到倒数第几行 // } // } // } }
关于在android应用中怎么添加一个上拉刷新下拉加载功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
本文标题:在android应用中怎么添加一个上拉刷新下拉加载功能
标题路径:http://myzitong.com/article/ipdphe.html