標籤:
首先是主要布局
<android.support.v4.widget.SwipeRefreshLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/swipeRefreshLayout"android:layout_width="match_parent"android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recylerView" android:layout_width="wrap_content" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView></android.support.v4.widget.SwipeRefreshLayout>
然後是擷取控制項以及設定監聽
findViewById......//設定下拉重新整理swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { if(isrefreshing){//判斷是否正在重新整理 Log.d(TAG,"ignore manually update!"); } else{ do....... } } });
說了下拉重新整理,那麼就有上拉載入更多,其實個人覺得上拉載入更多RecyclerView以及SwipeRefreshLayout都沒有做得很好,也是這個控制項用起來不是很舒服的地方。簡單的介紹一種方式
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); int lastVisibleItem = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition(); int totalItemCount = mLayoutManager.getItemCount(); //lastVisibleItem >= totalItemCount - 4 表示剩下4個item自動載入,各位自由選擇 // dy>0 表示向下滑動 if (lastVisibleItem >= totalItemCount - 4 && dy > 0) { if(isLoadingMore){ Log.d(TAG,"ignore manually update!"); } else{ loadPage();//這裡多線程也要手動控制isLoadingMore isLoadingMore = false; } } } });
總結:
1、新控制項用起來是很舒服,但是很多功能沒有全部書寫好
2、期望官方解決上拉載入更多的問題
《android recyclerview的簡單使用》