上拉更新,下拉刷新
第三方控件 :https://github.com/scwang90/SmartRefreshLayout
首先添加依赖
implementation ‘com.scwang.smartrefresh:SmartRefreshLayout:1.1.0‘
//布局 <com.scwang.smartrefresh.layout.SmartRefreshLayout android:layout_below ="@id/include" android:id="@+id/srl_layout" android:layout_width="match_parent" android:layout_height="match_parent" app:srlEnableAutoLoadMore="false"> //下拉刷新 <com.scwang.smartrefresh.layout.header.ClassicsHeader app:srlTextRelease="@string/Refresh" android:layout_width="match_parent" android:layout_height="wrap_content" /> <androidx.recyclerview.widget.RecyclerView android:scrollbars="none" android:layout_below ="@id/include" android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:fadeScrollbars="false" android:nestedScrollingEnabled="false" android:visibility="visible"/> //上拉加载更多 <com.scwang.smartrefresh.layout.footer.ClassicsFooter app:srlTextRelease="@string/Release" app:srlTextPulling="@string/up" android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.scwang.smartrefresh.layout.SmartRefreshLayout>
使用框架 /** * 这个是上拉刷新和加载框架 */ private RefreshLayout srl_layout; //记录刷新时间 Date delta = null; //服务器获取数据的页码 private int pageNum = 1; //判断是否是第一次进入 private static boolean isFirstEnter = true; //声明 srl_layout = findViewById(R.id.srl_layout); //需要时间的话写这个 mClassicsHeader = (ClassicsHeader) srl_layout.getRefreshHeader(); //获取现在的时间 Date nowDate = new Date(System.currentTimeMillis()); //如果是第一次进入那就记录现在的时间为下次刷新做准备 if (isFirstEnter) { mClassicsHeader.setLastUpdateTime(nowDate); delta = nowDate; isFirstEnter = false; //不是第一次就走这个 } else { 如果第一次进入没有刷新那没就没有刷新时间那就用现在获取到的 否则就需要用现在获取到的时间和上次刷新的时间做减法算出上次是什么时间刷新的 if (delta == null) { mClassicsHeader.setLastUpdateTime(nowDate); delta = nowDate; } else { Date daltaDate = new Date(System.currentTimeMillis() - delta.getTime()); delta = daltaDate; mClassicsHeader.setLastUpdateTime(daltaDate); } } mClassicsHeader.setTimeFormat(new SimpleDateFormat("更新于 MM-dd HH:mm", Locale.CHINA)); mClassicsHeader.setTimeFormat(new DynamicTimeFormat("最后更新: %s")); //进入刷新 srl_layout.autoRefresh(); //监听事件 //下拉刷新 srl_layout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(RefreshLayout refreshlayout) { myFeedback.clear(); pageNum = 1; getData(); mAdapter.notifyDataSetChanged(); refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败 } }); //上拉加载更多 srl_layout.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore(RefreshLayout refreshlayout) { pageNum++; getData(); mAdapter.notifyDataSetChanged(); refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败 } });
相关推荐
jiejie 2020-06-02
huavhuahua 2020-09-05
magic00 2020-08-03
风萧萧梦潇 2020-06-14
pengjin 2020-05-14
kururunga 2020-05-07
fengyeezju 2020-04-26
PrisonJoker 2020-04-16
fengyeezju 2020-04-07
hqulyc 2020-03-05
fengyeezju 2020-02-09
fengyeezju 2020-02-02
Urchindong 2020-01-21
fengyeezju 2020-01-11
绿豆饼 2020-01-05
csdnuuu 2019-12-27
pengjin 2019-12-21