上拉加载下拉刷新

一时失言乱红尘 2023-08-17 17:09 224阅读 0赞

老方法,先上效果图:

1264306-20190925171702811-981213350.gif

1.加入下拉刷新 上拉加载的依赖

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

2.控件的布局文件代码,以下的LinearLayout是内容部分,直接替换即可。

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

3.下拉和上拉的事件我封装在私有类中,直接在onCreate调用该类

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

转载于:https://www.cnblogs.com/Mr-Deng/p/11586105.html

发表评论

表情:
评论列表 (有 0 条评论,224人围观)

还没有评论,来说两句吧...

相关阅读