带加载更过功能的LoadMoreRecyclerView
2016-07-27 14:55
429 查看
类实现
使用,先给rvView添加底部view监听:
当无网络时,在出现加载圈后,需要进行移除,使用如下语句,即移除最后一个item(footerview)
/** * * 添加加载更多功能 * Created by chs . */ public class LoadMoreRecyclerView extends RecyclerView { private boolean isScrollingToBottom = true; private FloatingActionButton floatingActionButton; private LoadMoreListener mLoadingListener; private ArrayList<View> mFootViews = new ArrayList<>(); public LoadMoreRecyclerView(Context context) { super(context); init(context); } public LoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } private void init(Context context) { View view = LayoutInflater.from(context).inflate(R.layout.listview_footer,null); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); view.setLayoutParams(lp); addFootView(view); mFootViews.get(0).setVisibility(GONE); } public void addFootView(final View view) { mFootViews.clear(); mFootViews.add(view); } public LoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context); } public void applyFloatingActionButton(FloatingActionButton floatingActionButton) { this.floatingActionButton = floatingActionButton; } public void setLoadMoreListener(LoadMoreListener loadMoreListener){ this.mLoadingListener = loadMoreListener; } @Override public void onScrolled(int dx, int dy) { isScrollingToBottom = dy > 0; if (floatingActionButton != null) { if (isScrollingToBottom) { if (floatingActionButton.isShown()) floatingActionButton.hide(); } else { if (!floatingActionButton.isShown()) floatingActionButton.show(); } } } @Override public void onScrollStateChanged(int state) { if (state == RecyclerView.SCROLL_STATE_IDLE && mLoadingListener != null) { LayoutManager layoutManager = getLayoutManager(); int lastVisibleItemPosition; if (layoutManager instanceof GridLayoutManager) { lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); } else if (layoutManager instanceof StaggeredGridLayoutManager) { int[] into = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()]; ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(into); lastVisibleItemPosition = findMax(into); } else { lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); } if (layoutManager.getChildCount() > 0 && lastVisibleItemPosition >= layoutManager.getItemCount() - 1 && layoutManager.getItemCount() > layoutManager.getChildCount()) { View footView = mFootViews.get(0); footView.setVisibility(View.VISIBLE); mLoadingListener.onLoadMore(); } } } private int findMax(int[] lastPositions) { int max = lastPositions[0]; for (int value : lastPositions) { if (value > max) { max = value; } } return max; } public interface LoadMoreListener { void onLoadMore(); } }底部加载更多view,listview_footer.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="3dp"> <ProgressBar android:id="@+id/listview_foot_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/listview_foot_more" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="加载中..."/> </LinearLayout>
使用,先给rvView添加底部view监听:
rvNews.setLoadMoreListener(new LoadMoreRecyclerView.LoadMoreListener() { @Override public void onLoadMore() { //currentPage++; //mPresenter.start(currentPage,type); } });
当无网络时,在出现加载圈后,需要进行移除,使用如下语句,即移除最后一个item(footerview)
<pre style="font-family: 宋体; font-size: 9pt; background-color: rgb(255, 255, 255);"><span style="color:#660e7a;"><strong>mAdapter</strong></span>.notifyItemRemoved(<span style="color:#660e7a;"><strong>mAdapter</strong></span>.getItemCount());
相关文章推荐
- android定时器
- cocoapods 导入AFNetworking,SDWebimage
- yaml介绍
- 正解应该heap---wjj的排列序列
- 关于Html标签的嵌套
- java 异常处理
- Swift之沙盒与数据存储
- sewise HTML5开源播放器参数说明
- android studio 程序员有福了—从layout自动生成viewholder类
- Unity Application.persistentDataPath 为空
- 定时刷新
- java设计模式之访问者模式
- python--multiprocessing多进程总结
- tomcat部署应用仅需ip和port访问
- Inno Setup入门(七)——提供安装语言选项
- Android(java方法)上实现mp4的分割和拼接 (一)
- 8月份的To-Do List
- 一、Docker之旅
- 顺序表应用1:多余元素删除之移位算法
- c++ 内存管理