您好,登錄后才能下訂單哦!
在Android項目中使用RecyclerView實現一個上拉加載更多功能?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
既然有刷新的時候有兩種狀態就要定義兩個狀態
//普通布局的type static final int TYPE_ITEM = 0; //腳布局 static final int TYPE_FOOTER = 1;
在特定的時候去顯示:
@Override public int getItemViewType(int position) { //如果position加1正好等于所有item的總和,說明是最后一個item,將它設置為腳布局 if (position + 1 == getItemCount()) { return TYPE_FOOTER; } else { return TYPE_ITEM; } }
然后根據這個type來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布局的出現可隱藏,還要2中狀態需要控制就是上面的2個圖,一個是正在加載,一個是加載完成狀態,這兩個狀態在adapter里顯然是控制不了的,必須要在請求數據的時候才能知道這兩種狀態,而加載數據又是在activity中執行的,所以我們需要一個用來傳遞狀態的方法:
/** * 改變腳布局的狀態的方法,在activity根據請求數據的狀態來改變這個狀態 * * @param state */ public void changeState(int state) { this.footer_state = state; notifyDataSetChanged(); }
用來接收狀態的變量:
//上拉加載更多 // static final int PULL_LOAD_MORE = 0; //正在加載更多 static final int LOADING_MORE = 1; //沒有更多 static final int NO_MORE = 2; //腳布局當前的狀態,默認為沒有更多 int footer_state = 1;
然后就可以根據這幾種狀態來控制布局控件的改變了:
@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) {//如果第一個就是腳布局,,那就讓他隱藏 footViewHolder.mProgressBar.setVisibility(View.GONE); footViewHolder.tv_line1.setVisibility(View.GONE); footViewHolder.tv_line2.setVisibility(View.GONE); footViewHolder.tv_state.setText(""); } switch (footer_state) {//根據狀態來讓腳布局發生改變 // 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,因為添加了一個腳布局:
@Override public int getItemCount() { return mList != null ? mList.size() + 1 : 0; }
那么什么時候去傳遞狀態到adapter呢?這里就需要給RecyclerView添加滑動監聽了,當他滑動到底部的時候顯示加載中的狀態:
//給recyclerView添加滑動監聽 mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); /* 到達底部了,如果不加!isLoading的話到達底部如果還一滑動的話就會一直進入這個方法 就一直去做請求網絡的操作,這樣的用戶體驗肯定不好.添加一個判斷,每次滑倒底只進行一次網絡請求去請求數據 當請求完成后,在把isLoading賦值為false,下次滑倒底又能進入這個方法了 */ if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mAdapter.getItemCount() && !isLoading) { //到達底部之后如果footView的狀態不是正在加載的狀態,就將 他切換成正在加載的狀態 if (page < totlePage) { Log.e("duanlian", "onScrollStateChanged: " + "進來了"); 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); //拿到最后一個出現的item的位置 lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); } });
重點就這幾個,下面給出腳布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_below" android:layout_width="match_parent" android:layout_height="45dp" > <LinearLayout android:layout_width="fill_parent" android:layout_height="45dp" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/tv_line1" android:layout_width="60dp" android:layout_height="1dp" android:layout_marginRight="10dp" android:background="#ff00ff" android:visibility="gone"/> <ProgressBar android:id="@+id/progressbar" android:layout_width="25dp" android:layout_height="25dp" android:visibility="visible"/> <TextView android:id="@+id/foot_view_item_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:text="上拉加載更多"/> <TextView android:id="@+id/tv_line2" android:layout_width="60dp" android:layout_height="1dp" android:layout_marginLeft="10dp" android:background="#ff00ff" android:visibility="gone"/> </LinearLayout> </RelativeLayout>
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<RecyclerView.ViewHolder> { private Context mContext; private List<String> 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; //沒有更多 static final int NO_MORE = 2; //腳布局當前的狀態,默認為沒有更多 int footer_state = 1; public MyRecyclerAdapter(Context context, List<String> 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) {//如果第一個就是腳布局,,那就讓他隱藏 footViewHolder.mProgressBar.setVisibility(View.GONE); footViewHolder.tv_line1.setVisibility(View.GONE); footViewHolder.tv_line2.setVisibility(View.GONE); footViewHolder.tv_state.setText(""); } switch (footer_state) {//根據狀態來讓腳布局發生改變 // 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的總和,說明是最后一個item,將它設置為腳布局 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); } } /** * 改變腳布局的狀態的方法,在activity根據請求數據的狀態來改變這個狀態 * * @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 List<String> mList; private List<String> list; private MyRecyclerAdapter mAdapter; LinearLayoutManager linearLayoutManager; int lastVisibleItem; int page = 0; boolean isLoading = false;//用來控制進入getdata()的次數 int totlePage = 5;//模擬請求的一共的頁數 @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添加滑動監聽 mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); /* 到達底部了,如果不加!isLoading的話到達底部如果還一滑動的話就會一直進入這個方法 就一直去做請求網絡的操作,這樣的用戶體驗肯定不好.添加一個判斷,每次滑倒底只進行一次網絡請求去請求數據 當請求完成后,在把isLoading賦值為false,下次滑倒底又能進入這個方法了 */ if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mAdapter.getItemCount() && !isLoading) { //到達底部之后如果footView的狀態不是正在加載的狀態,就將 他切換成正在加載的狀態 if (page < totlePage) { Log.e("duanlian", "onScrollStateChanged: " + "進來了"); 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); //拿到最后一個出現的item的位置 lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); } }); } /** * 模擬請求數據 */ 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); } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。