老方法,先上效果图:
1.加入下拉刷新 上拉加载的依赖
1 //下拉刷新 上拉加载 2 implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-7' 3 implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.4-7'
2.控件的布局文件代码,以下的LinearLayout是内容部分,直接替换即可。
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 }