上拉加載下拉刷新

老方法,先上效果圖:android

1.加入下拉刷新 上拉加載的依賴app

1 //下拉刷新 上拉加載
2 implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-7'
3 implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.4-7'

2.控件的佈局文件代碼,如下的LinearLayout是內容部分,直接替換便可。ide

 1 <com.scwang.smartrefresh.layout.SmartRefreshLayout
 2         android:id="@+id/srl_control"
 3         android:layout_width="match_parent"
 4         android:layout_height="match_parent"
 5         app:srlAccentColor="@color/colorTransparent"
 6         app:srlPrimaryColor="@color/colorTransparent"
 7         app:srlEnablePreviewInEditMode="true">
 8 
 9         <com.scwang.smartrefresh.layout.header.ClassicsHeader
10             android:layout_width="match_parent"
11             android:layout_height="wrap_content"/>
12 
13 
14         <LinearLayout
15             android:background="@color/colorTheme"
16             android:layout_width="match_parent"
17             android:layout_height="610dp"/>
18 
19         <com.scwang.smartrefresh.layout.footer.ClassicsFooter
20             android:layout_width="match_parent"
21             android:layout_height="wrap_content"/>
22 
23     </com.scwang.smartrefresh.layout.SmartRefreshLayout>

3.下拉和上拉的事件我封裝在私有類中,直接在onCreate調用該類佈局

 1 private void initSmartRefresh(){
 2         //下拉刷新
 3         refreshLayout.setOnRefreshListener(new OnRefreshListener() {
 4             @Override
 5             public void onRefresh(RefreshLayout refreshlayout) {
 6                 refreshlayout.setEnableRefresh(true); //啓用刷新
 7                 //刷新的事件邏輯
 8                 try {
 9                     Thread.sleep(3000);
10                     refreshlayout.finishRefresh();//結束刷新
11                 } catch (InterruptedException e) {
12                     e.printStackTrace();
13                 }
14             }
15         });
16 
17         //上拉加載
18         refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
19             @Override
20             public void onLoadmore(RefreshLayout refreshlayout) {
21                 refreshlayout.setEnableLoadmore(true);//啓用加載
22                 //加載的事件邏輯
23                 try {
24                     Thread.sleep(3000);
25                     refreshlayout.finishLoadmore(); //結束加載
26                 } catch (InterruptedException e) {
27                     e.printStackTrace();
28                 }
29             }
30         });
31     }
相關文章
相關標籤/搜索