本文共 8664 字,大约阅读时间需要 28 分钟。
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //实际项目中这里一般是用网络请求获取数据 mList.add("下拉刷新"); mAdapter.notifyDataSetChanged(); mSwipeRefreshLayout.setRefreshing(false); }});
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mAdapter.getItemCount()) { mSwipeRefreshLayout.setRefreshing(true); //实际项目中这里一般是用网络请求获取数据 mList.add("上拉加载"); //为了有刷新的效果,延迟关闭刷新效果 mSwipeRefreshLayout.postDelayed(new Runnable() { @Override public void run() { mSwipeRefreshLayout.setRefreshing(false); mAdapter.notifyDataSetChanged(); } }, 2000); } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); //判断是当前layoutManager是否为LinearLayoutManager // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法 if (linearLayoutManager instanceof LinearLayoutManager) { //获取最后一个可见view的位置 lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); //获取第一个可见view的位置// int firstItemPosition = linearLayoutManager.findFirstVisibleItemPosition(); } } });
package com.xmr.recycleandswiperefresh;import android.content.Context;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.LinearLayout;import android.widget.TextView;import java.util.List;/** * Created by Administrator on 2016/9/13. */public class RefreshAdapter1 extends RecyclerView.Adapter{ private Context mContext; private List mDatas; private static final int TYPE_ITEM = 0; //普通Item View private static final int TYPE_FOOTER = 1; //底部FootView //已经到底了 public static final int PULLUP_LOAD_MORE = 0; //正在加载中 public static final int LOADING_MORE = 1; //上拉加载状态-默认为0 private int load_more_status = 0; public RefreshAdapter1(Context mContext, List mDatas) { this.mContext = mContext; this.mDatas = mDatas; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //进行判断显示类型,来创建返回不同的View if (viewType == TYPE_ITEM) { View view = LayoutInflater.from(parent.getContext()).inflate( R.layout.item_recycler_refresh, null);// view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,// LayoutParams.WRAP_CONTENT)); return new ItemHolder(view); } // type == TYPE_FOOTER 返回footerView else if (viewType == TYPE_FOOTER) { View view = LayoutInflater.from(parent.getContext()).inflate( R.layout.item_recycler_footer, null);// view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,// LayoutParams.WRAP_CONTENT)); return new FooterHolder(view); } return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof ItemHolder) { ItemHolder itemHolder = (ItemHolder) holder; itemHolder.textView.setText(mDatas.get(position)); } else if (holder instanceof FooterHolder) { FooterHolder footHolder = (FooterHolder) holder; //这句如果不注释,那么底部就会有添加的一个item footHolder.linearLayout.setVisibility(View.GONE); switch (load_more_status) { case PULLUP_LOAD_MORE: footHolder.linearLayout.setVisibility(View.GONE);// footHolder.textView.setText("已经到底了"); break; case LOADING_MORE: footHolder.linearLayout.setVisibility(View.VISIBLE); footHolder.textView.setText("正在加载更多数据..."); break; } } } @Override public int getItemCount() { return mDatas == null ? 0 : mDatas.size() + 1; } @Override public int getItemViewType(int position) { // 最后一个item设置为footerView if (position + 1 == getItemCount()) { return TYPE_FOOTER; } else { return TYPE_ITEM; } } class ItemHolder extends RecyclerView.ViewHolder { public TextView textView; public ItemHolder(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.tv_item_recycler_refresh); } } class FooterHolder extends RecyclerView.ViewHolder { public TextView textView; public LinearLayout linearLayout; public FooterHolder(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.tv_item_recycler_footer); linearLayout = (LinearLayout) itemView.findViewById(R.id.ll_item_recycler_footer); } } /** * //上拉加载更多 * PULLUP_LOAD_MORE=0; * //正在加载中 * LOADING_MORE=1; * //加载完成已经没有更多数据了 * NO_MORE_DATA=2; * * @param status */ public void changeMoreStatus(int status) { load_more_status = status; notifyDataSetChanged(); }}
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mAdapter.getItemCount()) { Log.i(">>>>>", ">>>>" + lastVisibleItem + ">>>" + mAdapter.getItemCount()); mAdapter.changeMoreStatus(mAdapter.LOADING_MORE); //为了有刷新的效果,延迟2s修改状态 new Handler().postDelayed(new Runnable() { @Override public void run() { mAdapter.changeMoreStatus(RefreshAdapter1.PULLUP_LOAD_MORE); //实际项目中这里一般是用网络请求获取数据 mList.add("上拉加载"); } }, 2000); } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); //判断是当前layoutManager是否为LinearLayoutManager // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法 if (linearLayoutManager instanceof LinearLayoutManager) { //获取最后一个可见view的位置 lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); //获取第一个可见view的位置// int firstItemPosition = linearLayoutManager.findFirstVisibleItemPosition(); } } }); }