在Android項(xiàng)目中使用RecyclerView實(shí)現(xiàn)一個(gè)上拉加載更多功能?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
目前成都創(chuàng)新互聯(lián)已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、武安網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
既然有刷新的時(shí)候有兩種狀態(tài)就要定義兩個(gè)狀態(tài)
//普通布局的type static final int TYPE_ITEM = 0; //腳布局 static final int TYPE_FOOTER = 1;
在特定的時(shí)候去顯示:
@Override public int getItemViewType(int position) { //如果position加1正好等于所有item的總和,說(shuō)明是最后一個(gè)item,將它設(shè)置為腳布局 if (position + 1 == getItemCount()) { return TYPE_FOOTER; } else { return TYPE_ITEM; } }
然后根據(jù)這個(gè)type來(lái)inflate不同的布局:
@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == TYPE_ITEM) { View view = View.inflate(mContext, R.layout.item_recycler, null); MyViewHolder holder = new MyViewHolder(view); return holder; } else if (viewType == TYPE_FOOTER) { //腳布局 View view = View.inflate(mContext, R.layout.recycler_load_more_layout, null); FootViewHolder footViewHolder = new FootViewHolder(view); return footViewHolder; } return null; }
以上只是控制了Loading布局的出現(xiàn)可隱藏,還要2中狀態(tài)需要控制就是上面的2個(gè)圖,一個(gè)是正在加載,一個(gè)是加載完成狀態(tài),這兩個(gè)狀態(tài)在adapter里顯然是控制不了的,必須要在請(qǐng)求數(shù)據(jù)的時(shí)候才能知道這兩種狀態(tài),而加載數(shù)據(jù)又是在activity中執(zhí)行的,所以我們需要一個(gè)用來(lái)傳遞狀態(tài)的方法:
/** * 改變腳布局的狀態(tài)的方法,在activity根據(jù)請(qǐng)求數(shù)據(jù)的狀態(tài)來(lái)改變這個(gè)狀態(tài) * * @param state */ public void changeState(int state) { this.footer_state = state; notifyDataSetChanged(); }
用來(lái)接收狀態(tài)的變量:
//上拉加載更多 // static final int PULL_LOAD_MORE = 0; //正在加載更多 static final int LOADING_MORE = 1; //沒(méi)有更多 static final int NO_MORE = 2; //腳布局當(dāng)前的狀態(tài),默認(rèn)為沒(méi)有更多 int footer_state = 1;
然后就可以根據(jù)這幾種狀態(tài)來(lái)控制布局控件的改變了:
@Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof MyViewHolder) { ((MyViewHolder) holder).mTextView.setText(mList.get(position)); } else if (holder instanceof FootViewHolder) { FootViewHolder footViewHolder = (FootViewHolder) holder; if (position == 0) {//如果第一個(gè)就是腳布局,,那就讓他隱藏 footViewHolder.mProgressBar.setVisibility(View.GONE); footViewHolder.tv_line1.setVisibility(View.GONE); footViewHolder.tv_line2.setVisibility(View.GONE); footViewHolder.tv_state.setText(""); } switch (footer_state) {//根據(jù)狀態(tài)來(lái)讓腳布局發(fā)生改變 // case PULL_LOAD_MORE://上拉加載 // footViewHolder.mProgressBar.setVisibility(View.GONE); // footViewHolder.tv_state.setText("上拉加載更多"); // break; case LOADING_MORE: footViewHolder.mProgressBar.setVisibility(View.VISIBLE); footViewHolder.tv_line1.setVisibility(View.GONE); footViewHolder.tv_line2.setVisibility(View.GONE); footViewHolder.tv_state.setText("正在加載..."); break; case NO_MORE: footViewHolder.mProgressBar.setVisibility(View.GONE); footViewHolder.tv_line1.setVisibility(View.VISIBLE); footViewHolder.tv_line2.setVisibility(View.VISIBLE); footViewHolder.tv_state.setText("我是有底線的"); footViewHolder.tv_state.setTextColor(Color.parseColor("#ff00ff")); break; } } }
不要忘了返回的count也要加1,因?yàn)樘砑恿艘粋€(gè)腳布局:
@Override public int getItemCount() { return mList != null ? mList.size() + 1 : 0; }
那么什么時(shí)候去傳遞狀態(tài)到adapter呢?這里就需要給RecyclerView添加滑動(dòng)監(jiān)聽了,當(dāng)他滑動(dòng)到底部的時(shí)候顯示加載中的狀態(tài):
//給recyclerView添加滑動(dòng)監(jiān)聽 mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); /* 到達(dá)底部了,如果不加!isLoading的話到達(dá)底部如果還一滑動(dòng)的話就會(huì)一直進(jìn)入這個(gè)方法 就一直去做請(qǐng)求網(wǎng)絡(luò)的操作,這樣的用戶體驗(yàn)肯定不好.添加一個(gè)判斷,每次滑倒底只進(jìn)行一次網(wǎng)絡(luò)請(qǐng)求去請(qǐng)求數(shù)據(jù) 當(dāng)請(qǐng)求完成后,在把isLoading賦值為false,下次滑倒底又能進(jìn)入這個(gè)方法了 */ if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mAdapter.getItemCount() && !isLoading) { //到達(dá)底部之后如果footView的狀態(tài)不是正在加載的狀態(tài),就將 他切換成正在加載的狀態(tài) if (page < totlePage) { Log.e("duanlian", "onScrollStateChanged: " + "進(jìn)來(lái)了"); isLoading = true; mAdapter.changeState(1); new Handler().postDelayed(new Runnable() { @Override public void run() { getData(); page++; } }, 2000); } else { mAdapter.changeState(2); } } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); //拿到最后一個(gè)出現(xiàn)的item的位置 lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); } });
重點(diǎn)就這幾個(gè),下面給出腳布局:
<?xml version="1.0" encoding="utf-8"?>
Adapter的代碼:
package com.duanlian.recyclerviewloadmoredemo; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.ProgressBar; import android.widget.TextView; import java.util.List; public class MyRecyclerAdapter extends RecyclerView.Adapter{ private Context mContext; private List mList; //普通布局的type static final int TYPE_ITEM = 0; //腳布局 static final int TYPE_FOOTER = 1; // //上拉加載更多 // static final int PULL_LOAD_MORE = 0; //正在加載更多 static final int LOADING_MORE = 1; //沒(méi)有更多 static final int NO_MORE = 2; //腳布局當(dāng)前的狀態(tài),默認(rèn)為沒(méi)有更多 int footer_state = 1; public MyRecyclerAdapter(Context context, List list) { mContext = context; mList = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == TYPE_ITEM) { View view = View.inflate(mContext, R.layout.item_recycler, null); MyViewHolder holder = new MyViewHolder(view); return holder; } else if (viewType == TYPE_FOOTER) { //腳布局 View view = View.inflate(mContext, R.layout.recycler_load_more_layout, null); FootViewHolder footViewHolder = new FootViewHolder(view); return footViewHolder; } return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof MyViewHolder) { ((MyViewHolder) holder).mTextView.setText(mList.get(position)); } else if (holder instanceof FootViewHolder) { FootViewHolder footViewHolder = (FootViewHolder) holder; if (position == 0) {//如果第一個(gè)就是腳布局,,那就讓他隱藏 footViewHolder.mProgressBar.setVisibility(View.GONE); footViewHolder.tv_line1.setVisibility(View.GONE); footViewHolder.tv_line2.setVisibility(View.GONE); footViewHolder.tv_state.setText(""); } switch (footer_state) {//根據(jù)狀態(tài)來(lái)讓腳布局發(fā)生改變 // case PULL_LOAD_MORE://上拉加載 // footViewHolder.mProgressBar.setVisibility(View.GONE); // footViewHolder.tv_state.setText("上拉加載更多"); // break; case LOADING_MORE: footViewHolder.mProgressBar.setVisibility(View.VISIBLE); footViewHolder.tv_line1.setVisibility(View.GONE); footViewHolder.tv_line2.setVisibility(View.GONE); footViewHolder.tv_state.setText("正在加載..."); break; case NO_MORE: footViewHolder.mProgressBar.setVisibility(View.GONE); footViewHolder.tv_line1.setVisibility(View.VISIBLE); footViewHolder.tv_line2.setVisibility(View.VISIBLE); footViewHolder.tv_state.setText("我是有底線的"); footViewHolder.tv_state.setTextColor(Color.parseColor("#ff00ff")); break; } } } @Override public int getItemViewType(int position) { //如果position加1正好等于所有item的總和,說(shuō)明是最后一個(gè)item,將它設(shè)置為腳布局 if (position + 1 == getItemCount()) { return TYPE_FOOTER; } else { return TYPE_ITEM; } } @Override public int getItemCount() { return mList != null ? mList.size() + 1 : 0; } /** * 正常布局的ViewHolder */ class MyViewHolder extends RecyclerView.ViewHolder { TextView mTextView; public MyViewHolder(View itemView) { super(itemView); mTextView = (TextView) itemView.findViewById(R.id.item_textView); } } /** * 腳布局的ViewHolder */ public static class FootViewHolder extends RecyclerView.ViewHolder { private ProgressBar mProgressBar; private TextView tv_state; private TextView tv_line1; private TextView tv_line2; public FootViewHolder(View itemView) { super(itemView); mProgressBar = (ProgressBar) itemView.findViewById(R.id.progressbar); tv_state = (TextView) itemView.findViewById(R.id.foot_view_item_tv); tv_line1 = (TextView) itemView.findViewById(R.id.tv_line1); tv_line2 = (TextView) itemView.findViewById(R.id.tv_line2); } } /** * 改變腳布局的狀態(tài)的方法,在activity根據(jù)請(qǐng)求數(shù)據(jù)的狀態(tài)來(lái)改變這個(gè)狀態(tài) * * @param state */ public void changeState(int state) { this.footer_state = state; notifyDataSetChanged(); } }
activity中的代碼:
package com.duanlian.recyclerviewloadmoredemo; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView mRecycler; private ListmList; private List list; private MyRecyclerAdapter mAdapter; LinearLayoutManager linearLayoutManager; int lastVisibleItem; int page = 0; boolean isLoading = false;//用來(lái)控制進(jìn)入getdata()的次數(shù) int totlePage = 5;//模擬請(qǐng)求的一共的頁(yè)數(shù) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); getData(); } private void initView() { mRecycler = (RecyclerView) findViewById(R.id.recycler); linearLayoutManager = new LinearLayoutManager(this); mRecycler.setLayoutManager(linearLayoutManager); mList = new ArrayList<>(); mAdapter = new MyRecyclerAdapter(this, mList); mRecycler.setAdapter(mAdapter); //給recyclerView添加滑動(dòng)監(jiān)聽 mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); /* 到達(dá)底部了,如果不加!isLoading的話到達(dá)底部如果還一滑動(dòng)的話就會(huì)一直進(jìn)入這個(gè)方法 就一直去做請(qǐng)求網(wǎng)絡(luò)的操作,這樣的用戶體驗(yàn)肯定不好.添加一個(gè)判斷,每次滑倒底只進(jìn)行一次網(wǎng)絡(luò)請(qǐng)求去請(qǐng)求數(shù)據(jù) 當(dāng)請(qǐng)求完成后,在把isLoading賦值為false,下次滑倒底又能進(jìn)入這個(gè)方法了 */ if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mAdapter.getItemCount() && !isLoading) { //到達(dá)底部之后如果footView的狀態(tài)不是正在加載的狀態(tài),就將 他切換成正在加載的狀態(tài) if (page < totlePage) { Log.e("duanlian", "onScrollStateChanged: " + "進(jìn)來(lái)了"); isLoading = true; mAdapter.changeState(1); new Handler().postDelayed(new Runnable() { @Override public void run() { getData(); page++; } }, 2000); } else { mAdapter.changeState(2); } } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); //拿到最后一個(gè)出現(xiàn)的item的位置 lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); } }); } /** * 模擬請(qǐng)求數(shù)據(jù) */ private void getData() { list = new ArrayList<>(); for (int i = 0; i < 10; i++) { list.add("第" + i + "條"); } mList.addAll(list); isLoading = false; mAdapter = new MyRecyclerAdapter(this, mList); mRecycler.setAdapter(mAdapter); } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。