【领券联盟】笔记:视频36,37,38,39,40-实现加载更多
破小布的小花狗
发表于
2020-04-01 02:01
1570
课堂笔记
课堂笔记
添加控件
- 在 HomePagerFragment 添加 NestedScrollView 解决 RecyclerView 上滑问题
- 添加 TwinklingRefreshLayout 控件加载更多内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
......
<com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
android:id="@+id/home_pager_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
......
<RelativeLayout
......
</RelativeLayout>
<!-- 标题布局 -->
<include layout="@layout/include_home_pager_title_part" />
<androidx.recyclerview.widget.RecyclerView
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout>
</LinearLayout>
首页加载更多逻辑代码
- CategoryPagerPresenterImpl 加载更多时,也要用到 Call< HomePagerContent > ,所以我们把它从 getContentByCategoryId 抽取出来
private Call<HomePagerContent> createTask(int categoryId,int page){
Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
Api api = retrofit.create(Api.class);
String homePagerUrl = UrlUtils.createHomePagerUrl(categoryId, page);
Call<HomePagerContent> task = api.getHomePagerContent(homePagerUrl);
return task;
}
- 加载更多时,我们要把加载内容的页面数 +1
@Override
public void loadMore(int categoryId) {
mCurrentPage = pagesInfo.get(categoryId);
if (mCurrentPage == null) {
mCurrentPage = 1;
}
mCurrentPage++;
Call<HomePagerContent> task = createTask(categoryId, mCurrentPage);
task.enqueue(new Callback<HomePagerContent>() {
@Override
public void onResponse(Call<HomePagerContent> call, Response<HomePagerContent> response) {
int code = response.code();
if (code == HttpURLConnection.HTTP_OK) {
HomePagerContent loadMoreResult = response.body();
//加载更多成功
handleLoadMoreResult(loadMoreResult, categoryId);
} else {
//加载更多网络错误
handleLoadMoreError(categoryId);
}
}
@Override
public void onFailure(Call<HomePagerContent> call, Throwable t) {
//加载更多网络错误
handleLoadMoreError(categoryId);
}
});
}
- 加载更多失败时,页面数 -1
private void handleLoadMoreError(int categoryId) {
for (ICategoryPagerCallback callback : callbacks) {
if (callback.getCategoryId() == categoryId) {
callback.onLoadMoreError();
mCurrentPage --;
}
}
}
- 加载更多内容为空时,页面数也要 -1
private void handleLoadMoreResult(HomePagerContent loadMoreResult, int categoryId) {
for (ICategoryPagerCallback callback : callbacks) {
if (callback.getCategoryId() == categoryId) {
if (loadMoreResult == null || loadMoreResult.getData().size() == 0) {
//加载更多内容为空
callback.onLoadMoreEmpty();
mCurrentPage--;
} else {
//成功加载更多数据
callback.onLoadMoreLoaded(loadMoreResult.getData());
}
}
}
}
首页加载更多数据显示
- 在 HomePagerFragment 绑定(找到)加载更多控件
@BindView(R.id.home_pager_refresh)
public TwinklingRefreshLayout twinklingRefreshLayout;
- initView 时,给 twinklingRefreshLayout 设置属性
//设置不能下拉刷新
twinklingRefreshLayout.setEnableRefresh(false);
//设置可以上拉加载更多
twinklingRefreshLayout.setEnableLoadmore(true);
- 在 initListener 给 twinklingRefreshLayout 添加加载更多的监听,并调用加载更多方法
twinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
@Override
public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
super.onLoadMore(refreshLayout);
if (mCategoryPagerPresent != null) {
mCategoryPagerPresent.loadMore(mMaterialId);
}
}
});
- 这样 View 层的 onLoadMoreLoaded 就有数据了,设置方法把数据给 HomePagerContentAdapter 显示
@Override
public void onLoadMoreLoaded(List<HomePagerContent.DataBean> contents) {
mHomePagerContentAdapter.setLoadMoreData(contents);
if (twinklingRefreshLayout != null) {
//加载更多完成后,结束方法
twinklingRefreshLayout.finishLoadmore();
}
}
- HomePagerContentAdapter 获取数据后,只更新获取到的数据部分
public void setLoadMoreData(List<HomePagerContent.DataBean> contents) {
int size = mData.size();
mData.addAll(contents);
notifyItemRangeChanged(size,contents.size());
}
- 无论加载更多数据是否成功,都要结束加载更多方法
@Override
public void onLoadMoreError() {
if (twinklingRefreshLayout != null) {
twinklingRefreshLayout.finishLoadmore();
}
}
@Override
public void onLoadMoreEmpty() {
if (twinklingRefreshLayout != null) {
twinklingRefreshLayout.finishLoadmore();
}
}
@Override
public void onLoadMoreLoaded(List<HomePagerContent.DataBean> contents) {
mHomePagerContentAdapter.setLoadMoreData(contents);
if (twinklingRefreshLayout != null) {
twinklingRefreshLayout.finishLoadmore();
}
}
创建 ToastUtil
- 防止同一时间多次出现 Toast 使 Toast 时间叠加
public class ToastUtil {
private static Toast mToast;
public static void showToast(String tips) {
if (mToast == null) {
mToast = Toast.makeText(BaseApplication.getAppContext(), tips, Toast.LENGTH_SHORT);
} else {
mToast.setText(tips);
}
mToast.show();
}
}
- 其中 Context
public class BaseApplication extends Application {
private static Context appContext;
@Override
public void onCreate() {
super.onCreate();
appContext = getBaseContext();
}
public static Context getAppContext() {
return appContext;
}
}
- 给 loadMore 的不同情况添加 Toast
@Override
public void onLoadMoreError() {
ToastUtil.showToast("网络错误,请稍后重试。。。");
twinklingRefreshLayout.finishLoadmore();
}
@Override
public void onLoadMoreEmpty() {
ToastUtil.showToast("没有更多内容了");
twinklingRefreshLayout.finishLoadmore();
}
@Override
public void onLoadMoreLoaded(List<HomePagerContent.DataBean> contents) {
mHomePagerContentAdapter.setLoadMoreData(contents);
if (twinklingRefreshLayout != null) {
twinklingRefreshLayout.finishLoadmore();
}
ToastUtil.showToast("加载了"+contents.size()+"个宝贝");
}
课后感悟
- 网上的 ToastUtil 工具类,就是 Context 不同
- 视频:Android Toast工具类