Android RecyclerView設(shè)置下拉刷新的實現(xiàn)方法
1 集成 SwipeRefreshLayout
1.1 xml布局文件中使用
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refresh" android:layout_width = "match_parent" android:layout_height = "match_parent" > <android.support.v7.widget.RecyclerView android:id = "@+id/rv_list" android:layout_width = "match_parent" android:layout_height = "match_parent" android:background = "#FF504F4F" /></android.support.v4.widget.SwipeRefreshLayout >
	
1.2 設(shè)置下拉刷新樣式
SwipeRefreshLayout swipeRefreshView = (SwipeRefreshLayout) findViewById(R.id.refresh);// 設(shè)置顏色屬性的時候一定要注意是引用了資源文件還是直接設(shè)置16進(jìn)制的顏色,因為都是int值容易搞混// 設(shè)置下拉進(jìn)度的背景顏色,默認(rèn)就是白色的swipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);// 設(shè)置下拉進(jìn)度的主題顏色swipeRefreshView.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);
	
1.3 設(shè)置下拉刷新加載監(jiān)聽
final Handler handler = new Handler();// 下拉時觸發(fā)SwipeRefreshLayout的下拉動畫,動畫完畢之后就會回調(diào)這個方法swipeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {  @Override  public void onRefresh() {    // 開始刷新,設(shè)置當(dāng)前為刷新狀態(tài)    //swipeRefreshLayout.setRefreshing(true);    // 這里是主線程    // 一些比較耗時的操作,比如聯(lián)網(wǎng)獲取數(shù)據(jù),需要放到子線程去執(zhí)行    new Thread(){      @Override      public void run () {        super.run();        //同步加載網(wǎng)絡(luò)數(shù)據(jù)         //加載數(shù)據(jù) 完畢后 關(guān)閉刷新狀態(tài) 切回主線程        handler.postDelayed(new Runnable() {          @Override          public void run() {            // 加載完數(shù)據(jù)設(shè)置為不刷新狀態(tài),將下拉進(jìn)度收起來            swipeRefreshView.setRefreshing(false);          }        }, 100);      }    }.start();  }});如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選