這篇文章給大家介紹如何在Android項(xiàng)目中使用RecyclerView實(shí)現(xiàn)上拉或下拉刷新功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比冠縣網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式冠縣網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋冠縣地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
首頁(yè)布局:activity_notice
<?xml version="1.0" encoding="utf-8"?>
子item布局:item_base
<?xml version="1.0" encoding="utf-8"?>
toolbar
<?xml version="1.0" encoding="utf-8"?>
首頁(yè) 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; //判斷滾動(dòng)狀態(tài) boolean isLoading; private List
在第106行中通過(guò)這三個(gè)條件判斷就可以下拉刷新的執(zhí)行。
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{ //定義一個(gè)viewholder回調(diào)接口 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) { //創(chuàng)建普通類型的item布局 View view = LayoutInflater.from(context).inflate(R.layout.item_base, parent, false); Log.d("test", "創(chuàng)建普通類型的item布局"); return new ItemViewHolder(view); } else if (viewType == TYPE_FOOTER) { //創(chuàng)建腳布局類型的item布局 View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent, false); footerHolder=new FootViewHolder(view); Log.d("test", "創(chuàng)建腳布局類型的item布局"); return footerHolder; } return null; } @Override public void onBindViewHolder(final ViewHolder holder, int position) { //在onCreateViewHolder執(zhí)行完成后回調(diào) 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行定義一個(gè)借口用來(lái)通知下拉刷新后隱藏腳布局,主要在onCreateViewHolder執(zhí)行完成后回調(diào)。
關(guān)于如何在Android項(xiàng)目中使用RecyclerView實(shí)現(xiàn)上拉或下拉刷新功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。