这篇文章给大家介绍在Android项目中使用RecyclerView实现一个上拉加载下拉刷新功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
10年积累的成都网站设计、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有斗门免费网站建设让你可以放心的选择与我们合作。
核心实现
package com.example.fly.recyclerviewrefresh.base; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.example.fly.recyclerviewrefresh.R; import java.util.ArrayList; import java.util.List; /** * 类名: * 类描述:下拉刷新和上拉加载 * 创建人:fly * 创建日期: 2017/2/2. * 版本:V1.0 */ public abstract class RefreshActivityextends BaseActivity implements SwipeRefreshLayout.OnRefreshListener { protected SwipeRefreshLayout swipeRefreshLayout; protected RecyclerView recyclerView; protected LinearLayoutManager linearLayoutManager; protected A adapter; protected List lists = new ArrayList<>(); protected T dataBeanLoadMore = createBean(); protected boolean isLoadDataIng; // 默认为false 是否正在加载数据, false 不在加载, true 正在加载 protected boolean isScrollY; // 是否在Y轴(垂直方向)上滑动, false 没滑动, true 滑动 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(setLayout()); swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); swipeRefreshLayout.setOnRefreshListener(this); swipeRefreshLayout.setColorSchemeResources(Refresh.colors); // 设置变化的颜色 recyclerView = (RecyclerView) findViewById(R.id.recyclerView); linearLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(linearLayoutManager); loadMore(); } /**作用就是能在父类中初始化公共的控件如上面的onCreate(@Nullable Bundle savedInstanceState)初始化的控件*/ protected abstract int setLayout(); public abstract T createBean(); // public abstract A createAdapter(); /**上拉加载*/ protected void loadMore() { recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { /** * * @param recyclerView 当前在滚动的RecyclerView * @param newState 当前滚动状态 * newState有三种值: //静止,没有滚动 public static final int SCROLL_STATE_IDLE = 0; //正在被外部拖拽,一般为用户正在用手指滚动 public static final int SCROLL_STATE_DRAGGING = 1; //自动滚动开始 public static final int SCROLL_STATE_SETTLING = 2; */ @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_IDLE && isLoadDataIng == false ) { int lastVisibleItem = linearLayoutManager.findLastCompletelyVisibleItemPosition(); // 获取最后一个item的角标 int totalItemCount = linearLayoutManager.getItemCount(); // 获取item的总数 if (lastVisibleItem == (totalItemCount - 1)) { // 判断滑到最后一个item if (!swipeRefreshLayout.isRefreshing()) { //不再下拉刷新状态 if (isScrollY) { // 在Y轴(垂直方向)上有滑动 // TODO 执行上拉加载 onShowRefresh(); loadMoreData(); } } } } } /** * * @param recyclerView 当前滚动的view * @param dx 水平滚动距离 * @param dy 垂直滚动距离 */ @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (dy > 0) { isScrollY = true; }else{ isScrollY = false; } } }); } /**加载数据*/ public abstract void loadMoreData(); /**显示加在更多的View*/ protected void onShowRefresh() { isLoadDataIng = true; // 正在加载数据 lists.add(dataBeanLoadMore); adapter.notifyDataSetChanged(); } /**隐藏加在更多的View*/ protected void onHintLoadMore() { lists.remove(dataBeanLoadMore); isLoadDataIng = false; // 不在加载数据 adapter.notifyDataSetChanged(); } }
package com.example.fly.recyclerviewrefresh; import android.os.Handler; import android.os.Bundle; import com.example.fly.recyclerviewrefresh.adapter.RecyclerAdapter; import com.example.fly.recyclerviewrefresh.base.Refresh; import com.example.fly.recyclerviewrefresh.base.RefreshActivity; import com.example.fly.recyclerviewrefresh.bean.DataBean; /** * 类名: * 类描述:RecyclerView刷新 方法二 * 创建人:fly * 创建日期: 2017/2/2. * 版本:V1.0 */ public class MainActivity extends RefreshActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); for (int i = 0; i < 20; i++) { DataBean dataBean = new DataBean(Refresh.COMMON); dataBean.number = String.valueOf(i); lists.add(dataBean); } adapter = new RecyclerAdapter(lists,this); recyclerView.setAdapter(adapter); } @Override public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { lists.clear(); adapter.notifyDataSetChanged(); swipeRefreshLayout.setRefreshing(false); } },1000); } @Override protected int setLayout() {return R.layout.activity_main;} @Override public DataBean createBean() {return new DataBean(Refresh.LOAD_MORE);} @Override public void loadMoreData() { for (int i = 0; i < 5; i++) { DataBean dataBean = new DataBean(Refresh.COMMON); dataBean.number = String.valueOf(i) + "fly"; lists.add(dataBean); } adapter.notifyDataSetChanged(); onHintLoadMore(); } }
关于在Android项目中使用RecyclerView实现一个上拉加载下拉刷新功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。