十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章给大家介绍如何在Android项目中使用RecyclerView实现上拉或下拉刷新功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
成都创新互联-专业网站定制、快速模板网站建设、高性价比冠县网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式冠县网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖冠县地区。费用合理售后完善,10多年实体公司更值得信赖。
首页布局:activity_notice
<?xml version="1.0" encoding="utf-8"?>
子item布局:item_base
<?xml version="1.0" encoding="utf-8"?>
脚布局 item_foot
<?xml version="1.0" encoding="utf-8"?>
toolbar
<?xml version="1.0" encoding="utf-8"?>
首页 MainActivity
package com.lxl.refresh; import android.os.Bundle; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import butterknife.ButterKnife; import butterknife.InjectView; /** * Description: * Author:lxl * Date: 2016/10/9 10:15 */ public class MainActivity extends AppCompatActivity { @InjectView(R.id.toolbar) Toolbar toolbar; @InjectView(R.id.recyclerView) RecyclerView recyclerView; @InjectView(R.id.SwipeRefreshLayout) SwipeRefreshLayout swipeRefreshLayout; boolean isSlidingToLast = false; //判断滚动状态 boolean isLoading; private List
在第106行中通过这三个条件判断就可以下拉刷新的执行。
RecyclerView的适配器:RecyclerViewAdapter
package com.lxl.refresh; import android.content.Context; import android.support.v7.widget.RecyclerView.Adapter; import android.support.v7.widget.RecyclerView.ViewHolder; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; import java.util.List; public class RecyclerViewAdapter extends Adapter{ private static final int TYPE_ITEM = 0; private static final int TYPE_FOOTER = 1; private Context context; private List data; protected FootViewHolder footerHolder; public RecyclerViewAdapter(Context context, List data) { this.context = context; this.data = data; } //------------------------------------- public interface onHolderCreate{ //定义一个viewholder回调接口 void created(); } public onHolderCreate getmCreateViewHolder() { return mCreateViewHolder; } public void setmCreateViewHolder(onHolderCreate mCreateViewHolder) { this.mCreateViewHolder = mCreateViewHolder; } private onHolderCreate mCreateViewHolder; //------------------------------------- public interface OnItemClickListener { void onItemClick(View view, int position); void onItemLongClick(View view, int position); } private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } @Override public int getItemCount() { return data.size() == 0 ? 0 : data.size() + 1; //添加脚布局 } @Override public int getItemViewType(int position) { if (position + 1 == getItemCount()) { return TYPE_FOOTER; //脚布局 } else { return TYPE_ITEM; //普通item } } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == TYPE_ITEM) { //创建普通类型的item布局 View view = LayoutInflater.from(context).inflate(R.layout.item_base, parent, false); Log.d("test", "创建普通类型的item布局"); return new ItemViewHolder(view); } else if (viewType == TYPE_FOOTER) { //创建脚布局类型的item布局 View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent, false); footerHolder=new FootViewHolder(view); Log.d("test", "创建脚布局类型的item布局"); return footerHolder; } return null; } @Override public void onBindViewHolder(final ViewHolder holder, int position) { //在onCreateViewHolder执行完成后回调 if (holder instanceof FootViewHolder){ if (mCreateViewHolder!=null){ mCreateViewHolder.created(); } } if (holder instanceof ItemViewHolder) { //holder.tv.setText(data.get(position)); if (onItemClickListener != null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getLayoutPosition(); onItemClickListener.onItemClick(holder.itemView, position); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { int position = holder.getLayoutPosition(); onItemClickListener.onItemLongClick(holder.itemView, position); return false; } }); } } } static class ItemViewHolder extends ViewHolder { TextView tv; public ItemViewHolder(View view) { super(view); tv = (TextView) view.findViewById(R.id.tv_date); } } class FootViewHolder extends ViewHolder { LinearLayout ll; ProgressBar pb; TextView tv; public FootViewHolder(View view) { super(view); ll=(LinearLayout) view.findViewById(R.id.ll); pb=(ProgressBar) view.findViewById(R.id.progressBar); tv=(TextView) view.findViewById(R.id.tv); } } }
在第30行定义一个借口用来通知下拉刷新后隐藏脚布局,主要在onCreateViewHolder执行完成后回调。
关于如何在Android项目中使用RecyclerView实现上拉或下拉刷新功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。