Android RecyclerView实现下拉刷新和上拉加载更多
使用官方的刷新控件SwipeRefreshLayout来实现下拉刷新,当RecyclerView滑到底部实现下拉加载(进度条效果用RecyclerView加载一个布局实现)
需要完成控件的下拉监听和上拉监听,其中,下拉监听通过SwipRefreshLayout的setOnRefreshListener()方法监听,而上拉刷新,需要通过监听列表的滚动,当列表滚动到底部时触发事件,具体代码如下
主布局
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refresh_layout" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_list" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.v4.widget.SwipeRefreshLayout>
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener { private SwipeRefreshLayout refreshLayout; private RecyclerView recyclerView; private LinearLayoutManager layoutManager; private RecyclerAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout); recyclerView = (RecyclerView) findViewById(R.id.recycler_list); layoutManager = new LinearLayoutManager(this); refreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary);//设置刷新时进度条 颜色,最多四种 refreshLayout.setOnRefreshListener(this); mAdapter = new RecyclerAdapter();//自定义的适配器 recyclerView.setAdapter(mAdapter); recyclerView.setLayoutManager(layoutManager); recyclerView.addOnScrollListener(new OnRecyclerScrollListener()); } /** * 用于下拉刷新 */ @Override public void onRefresh() { } /** * 用于上拉加载更多 */ public class OnRecyclerScrollListener extends RecyclerView.OnScrollListener { int lastVisibleItem = 0; @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (mAdapter != null && newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mAdapter.getItemCount()) { //滚动到底部了,可以进行数据加载等操作 } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); lastVisibleItem = layoutManager.findLastVisibleItemPosition(); } } }
下面是实现上拉时进度条转动的效果
item_list_footer.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_item_footer_load_more" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:gravity="center" android:text="上拉加载更多" /> <ProgressBar android:id="@+id/pb_item_footer_loading" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:visibility="gone"/> </RelativeLayout>
适配器
public class RecyclerAdapter extends RecyclerView.Adapter<ViewHolder> { private static final int TYPE_CONTENT = 0; private static final int TYPE_FOOTER = 1; private ArrayList<DataBean> dataList; private ProgressBar pbLoading; private TextView tvLoadMore; public RecyclerAdapter() { dataList = new ArrayList<>(); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == TYPE_CONTENT) { return new ContentViewHolder(LayoutInflater.from(parent.getContext()).inflate (R.layout.item_list_content, parent, false)); } else if (viewType == TYPE_FOOTER) {//加载进度条的布局 return new FooterViewHolder(LayoutInflater.from(parent.getContext()).inflate (R.layout.item_list_footer, parent, false)); } return null; } @Override public void onBindViewHolder(ViewHolder holder, int position) { int type = getItemViewType(position); if (type == TYPE_CONTENT) { DataBean bean = dataList.get(position); ((ContentViewHolder) holder).tvId.setText("" + bean.getId()); ((ContentViewHolder) holder).tvName.setText(bean.getName()); } else if (type == TYPE_FOOTER) { pbLoading = ((FooterViewHolder) holder).pbLoading; tvLoadMore = ((FooterViewHolder) holder).tvLoadMore; } } /** * 获取数据集加上一个footer的数量 */ @Override public int getItemCount() { return dataList.size() + 1; } @Override public int getItemViewType(int position) { if (position + 1 == getItemCount()) { return TYPE_FOOTER; } else { return TYPE_CONTENT; } } /** * 获取数据集的大小 */ public int getListSize() { return dataList.size(); } /** * 内容的ViewHolder */ public static class ContentViewHolder extends ViewHolder { private TextView tvId, tvName; public ContentViewHolder(View itemView) { super(itemView); tvId = (TextView) itemView.findViewById(R.id.tv_item_id); tvName = (TextView) itemView.findViewById(R.id.tv_item_name); } } /** * footer的ViewHolder */ public static class FooterViewHolder extends ViewHolder { private TextView tvLoadMore; private ProgressBar pbLoading; public FooterViewHolder(View itemView) { super(itemView); tvLoadMore = (TextView) itemView.findViewById(R.id.tv_item_footer_load_more); pbLoading = (ProgressBar) itemView.findViewById(R.id.pb_item_footer_loading); } } /** * 显示正在加载的进度条,滑动到底部时,调用该方法,上拉就显示进度条,隐藏"上拉加载更多" */ public void showLoading() { if (pbLoading != null && tvLoadMore != null) { pbLoading.setVisibility(View.VISIBLE); tvLoadMore.setVisibility(View.GONE); } } /** * 显示上拉加载的文字,当数据加载完毕,调用该方法,隐藏进度条,显示“上拉加载更多” */ public void showLoadMore() { if (pbLoading != null && tvLoadMore != null) { pbLoading.setVisibility(View.GONE); tvLoadMore.setVisibility(View.VISIBLE); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
- Android实现上拉加载更多以及下拉刷新功能(ListView)
- Android RecyclerView 上拉加载更多及下拉刷新功能的实现方法
- Android ListView实现上拉加载更多和下拉刷新功能
- Android下拉刷新上拉加载更多左滑动删除
- Android XListView下拉刷新和上拉加载更多
- Android RecyclerView下拉刷新和上拉加载更多
- Android 仿硅谷新闻下拉刷新/上拉加载更多
- Android中Listview下拉刷新和上拉加载更多的多种实现方案
- android使用PullToRefresh框架实现ListView下拉刷新上拉加载更多
- Android实践之带加载效果的下拉刷新上拉加载更多
相关文章
详解android studio游戏摇杆开发教程,仿王者荣耀摇杆
这篇文章主要介绍了android studio游戏摇杆开发教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-05-05android设备不识别awk命令 缺少busybox怎么办
这篇文章主要为大家详细介绍了android设备不识别awk命令,缺少busybox的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-04-04Android学习笔记之AndroidManifest.xml文件解析(详解)
这篇文章主要介绍了Android学习笔记之AndroidManifest.xml文件解析,需要的朋友可以参考下2015-10-10深入解析Android App的LayoutInflate布局
这篇文章主要介绍了Android App的LayoutInflate布局,对LayoutInflate编写中经常被无解及产生错误的地方进行了深入说明,需要的朋友可以参考下2016-04-04
最新评论