Recycleview實現下拉刷新&上拉加載更多


一.下拉刷新
話不多說,前段時間做項目的時候剛用到過,分享出來集思廣益,歡迎指出不足。本文主要利用SwipeRefreshLayout嵌套Recycleview實現簡單的下拉刷新功能。

1.XML文件:

                       <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swiperefreshlayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:scrollbars="vertical" /> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout> 

2.代碼:Activity的OnCreate方法里只要添加幾行代碼就可實現下拉刷新的效果

                 swiperefreshlayout.setOnRefreshListener(this); swiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //發送網絡請求 sendRequest(); //停止刷新 swiperefreshlayout.setRefreshing(false); //刷新RecycleView mRecycleViewAdapter.notifyDataSetChanged(); } }); 

二.上拉加載更多
看過網上很多關於上拉加載更多的文章,最后總結測試了一下,就有了適合自己的簡單的上拉加載更多。主要采用Recycleview設置一個FootViewHolder,並監聽Recycleview的滑動狀態來實現效果。直接貼代碼吧(Copy不能運行,只能自己截取有用的部分加以利用,耐着性子看完,相信會有收獲哦)

1.XML文件:

                    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swiperefreshlayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:scrollbars="vertical" /> </android.support.v4.widget.SwipeRefreshLayout> 

2.代碼(主要為網絡請求到的數據Copy無效,其他各位看官隨意蹂躪):

1.Activity: public class MyActivity extends AppCompatActivity { private NoticeAdapter adapter; private RecyclerView noticeRecyclerview; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); initData(); } private void initData() { //news為你第一次發送網絡請求獲取到的List集合,這里就不貼了 adapter = new NoticeAdapter(this,news); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); noticeRecyclerview.setLayoutManager(linearLayoutManager); noticeRecyclerview.setAdapter(adapter); noticeRecyclerview.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); noticeRecyclerview.addOnScrollListener(monScrollListener); adapter.notifyDataSetChanged(); } //本段可直接Copy,作用是監聽Recycleview是否滑動到底部 private int mLastVisibleItemPosition; private RecyclerView.OnScrollListener monScrollListener = new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); } if (adapter != null) { if (newState == RecyclerView.SCROLL_STATE_IDLE && mLastVisibleItemPosition + 1 == adapter.getItemCount()) { //發送網絡請求獲取更多數據 sendMoreRequest(); } } } }; private void sendMoreRequest() { //可直接查看下面onResponse方法,網絡請求操作不必細看 String url = Constant.BaseUrl + "getNews"; final String s = MD5Tools.MD5(MD5Tools.MD5("NEW")); OkHttpUtils.post().url(url) .addParams("FKEY", s) .build() .execute(new StringCallback() { @Override public void onError(Call call, Exception e, int id) { } @Override public void onResponse(String response, int id) { NoticeBean noticeBean = new Gson().fromJson(response.toString(), NoticeBean.class); //result == 1表示請求成功 if (noticeBean.getResult()== 1){ //判斷請求的數據集合是否不為空 if (noticeBean.getNews().size()!=0){ //不為空,則添加數據 adapter.addList(noticeBean.getNews()); }else { //為空,則調用adapter里面的方法隱藏FootView adapter.setIsLoadMore(); } } } }); } 

}

2.Adapter:

 public class NoticeAdapter extends RecyclerView.Adapter{ private List<NoticeBean.NewsBean> list; private Context mContext; //上拉加載更多布局 public static final int view_Foot = 1; //主要布局 public static final int view_Normal = 2; //是否隱藏 public boolean isLoadMore = false; public NoticeAdapter(Context mContext, List<NoticeBean.NewsBean> list) { this.list = list; this.mContext = mContext; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType ==view_Normal ){ NoticeItemView noticeItemView = new NoticeItemView(mContext); NoticeViewHolder viewHolder = new NoticeViewHolder(noticeItemView); return viewHolder; }else { FootView view = new FootView(mContext); FootViewHolder footViewHolder = new FootViewHolder(view); return footViewHolder; } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (position == getItemCount()-1){ FootView itemView = (FootView) holder.itemView; if (isLoadMore){ itemView.setData(); }else { itemView.setVisibility(View.VISIBLE); } }else { NoticeItemView itemView = (NoticeItemView) holder.itemView; itemView.setData(list.get(position)); } } @Override public int getItemCount() { return list.size()+1; } @Override public int getItemViewType(int position) { if (position == getItemCount()-1){ return view_Foot; }else { return view_Normal; } } public void addList(List<NoticeBean.NewsBean> news) { list.addAll(news); notifyDataSetChanged(); } public void setIsLoadMore() { this.isLoadMore = true; notifyDataSetChanged(); } static class NoticeViewHolder extends RecyclerView.ViewHolder { public NoticeViewHolder(View itemView) { super(itemView); } } static class FootViewHolder extends RecyclerView.ViewHolder { public FootViewHolder(View itemView) { super(itemView); } } 

}

3.FootView為自定義控件,NoticeItemView和FootView大同小異,就不貼代碼了,FootView代碼如下

       public class FootView extends RelativeLayout { @BindView(R.id.foot_view_progressbar) ProgressBar footViewProgressbar; public FootView(Context context) { this(context, null); } public FootView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { View.inflate(getContext(), R.layout.foot_view, this); ButterKnife.bind(this,this); } public void setData() { footViewProgressbar.setVisibility(GONE); } 

}

4.FootView的XML文件:

        <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ProgressBar android:id="@+id/foot_view_progressbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/> 

</LinearLayout>

四.后續會更新源碼至GitHub,有興趣的同學可關注微信公眾號MiHomes。

末尾:移動互聯&人力資源交流群,可加微信zy666128入群交流。



作者:MiHomes
鏈接:https://www.jianshu.com/p/2c50d0b100fb
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM