效果图:
上拉加载用BaseRecyclerViewAdapterHelper,下拉刷新用SmartRefreshLayout
1、导入包
BaseRecyclerViewAdapterHelper 导入
https://www.jianshu.com/p/dee739e19628
SmartRefreshLayout 导入
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0'
下拉刷新:
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
articleList.clear();
page_size=0;
getArticleData(page_size);
refreshLayout.finishRefresh(1500);
}
});
上拉加载:
articleAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
@Override
public void onLoadMoreRequested() {
++page_size;
getArticleData(page_size);
}
}, newBowenRecycleList);
全部代码
2、定义列表
2.1、xml布局
列表布局 (fragment_newbowen.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorGray"
android:orientation="vertical">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/newBowen_recycle_list"
android:layout_width="match_parent"
android:overScrollMode="never"
android:layout_marginBottom="60dp"
android:layout_height="match_parent" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
item布局(item_newbowen)
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="2dp"
android:orientation="vertical"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/newBowen_img_head"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:src="@drawable/index_head" />
<TextView
android:id="@+id/newBowen_txt_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp" />
<TextView
android:id="@+id/newBowen_txt_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:layout_toRightOf="@+id/newBowen_img_head"
android:text="鸿洋" />
<TextView
android:id="@+id/newBowen_txt_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/newBowen_txt_author"
android:background="@drawable/radio_theme"
android:textColor="@color/theme"
android:visibility="invisible" />
<TextView
android:id="@+id/newBowen_txt_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="一天前" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/newBowen_txt_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:ellipsize="end"
android:lines="3"
android:text="鸿洋"
android:textColor="@color/colorBlack" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginBottom="3dp">
<TextView
android:id="@+id/newBowen_txt_superChapterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="公众号" />
<View
android:layout_width="1dp"
android:layout_height="10dp"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:layout_toRightOf="@+id/newBowen_txt_superChapterName"
android:background="@color/colorBlack" />
<TextView
android:id="@+id/newBowen_txt_chapterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/newBowen_txt_superChapterName"
android:text="公众号" />
<ImageView
android:visibility="visible"
android:id="@+id/newBowen_img_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="@drawable/all_more" />
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
2.2、创建bean (Article.class)
public class Article {
private DataBean data;
private int errorCode;
private String errorMsg;
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public static class DataBean {
private int curPage;
private int offset;
private boolean over;
private int pageCount;
private int size;
private int total;
private List<DatasBean> datas;
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public boolean isOver() {
return over;
}
public void setOver(boolean over) {
this.over = over;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List<DatasBean> getDatas() {
return datas;
}
public void setDatas(List<DatasBean> datas) {
this.datas = datas;
}
public static class DatasBean {
private String apkLink;
private String author;
private int chapterId;
private String chapterName;
private boolean collect;
private int courseId;
private String desc;
private String envelopePic;
private boolean fresh;
private int id;
private String link;
private String niceDate;
private String origin;
private String prefix;
private String projectLink;
private long publishTime;
private int superChapterId;
private String superChapterName;
private String title;
private int type;
private int userId;
private int visible;
private int zan;
private List<?> tags;
public String getApkLink() {
return apkLink;
}
public void setApkLink(String apkLink) {
this.apkLink = apkLink;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getChapterId() {
return chapterId;
}
public void setChapterId(int chapterId) {
this.chapterId = chapterId;
}
public String getChapterName() {
return chapterName;
}
public void setChapterName(String chapterName) {
this.chapterName = chapterName;
}
public boolean isCollect() {
return collect;
}
public void setCollect(boolean collect) {
this.collect = collect;
}
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getEnvelopePic() {
return envelopePic;
}
public void setEnvelopePic(String envelopePic) {
this.envelopePic = envelopePic;
}
public boolean isFresh() {
return fresh;
}
public void setFresh(boolean fresh) {
this.fresh = fresh;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getNiceDate() {
return niceDate;
}
public void setNiceDate(String niceDate) {
this.niceDate = niceDate;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getProjectLink() {
return projectLink;
}
public void setProjectLink(String projectLink) {
this.projectLink = projectLink;
}
public long getPublishTime() {
return publishTime;
}
public void setPublishTime(long publishTime) {
this.publishTime = publishTime;
}
public int getSuperChapterId() {
return superChapterId;
}
public void setSuperChapterId(int superChapterId) {
this.superChapterId = superChapterId;
}
public String getSuperChapterName() {
return superChapterName;
}
public void setSuperChapterName(String superChapterName) {
this.superChapterName = superChapterName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getVisible() {
return visible;
}
public void setVisible(int visible) {
this.visible = visible;
}
public int getZan() {
return zan;
}
public void setZan(int zan) {
this.zan = zan;
}
public List<?> getTags() {
return tags;
}
public void setTags(List<?> tags) {
this.tags = tags;
}
}
}
}
2.3、adapter(ArticleAdapter.class)
public class ArticleAdapter extends BaseQuickAdapter<Article.DataBean.DatasBean, BaseViewHolder> {
public ArticleAdapter(int layoutResId, @Nullable List<Article.DataBean.DatasBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(@NonNull BaseViewHolder helper, Article.DataBean.DatasBean item) {
if (item.getSuperChapterName().equals("公众号")) {
helper.setText(R.id.newBowen_txt_label, item.getSuperChapterName()).setVisible(R.id.newBowen_txt_label, true);
}
if (TextUtils.isEmpty(item.getAuthor())){
helper.setText(R.id.newBowen_txt_author, "佚名");
}else {
helper.setText(R.id.newBowen_txt_author, item.getAuthor());
}
helper.setText(R.id.newBowen_txt_detail, item.getTitle()).addOnClickListener(R.id.newBowen_img_more);
helper.setText(R.id.newBowen_txt_superChapterName, item.getSuperChapterName());
helper.setText(R.id.newBowen_txt_chapterName, item.getChapterName());
helper.setText(R.id.newBowen_txt_time, item.getNiceDate());
}
}
2.4、Fragment(BewBiwebFragment.java)
/**
* 最新博文
*/
public class NewBowenFragment extends Fragment {
@BindView(R.id.newBowen_recycle_list)
RecyclerView newBowenRecycleList;
@BindView(R.id.refreshLayout)
SmartRefreshLayout refreshLayout;
private View view;
private ArrayList<String> imagePath;//轮播图图片集合
private List<Carousel.DataBean> carouselList;
private List<String> titles;
private Banner banner;
private List<Article.DataBean.DatasBean> articleList;
private ArticleAdapter articleAdapter;
private Loading loading = new Loading();
private int page_size = 0;
private Unbinder unbinder;
private Boolean isOne = true;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_newbowen, container, false);
unbinder = ButterKnife.bind(this, view);
articleList = new ArrayList<>();
initView();
return view;
}
/*
*初始化页面
*/
private void initView() {
initItemView();
getBannerData();
getArticleData(page_size);
}
/**
* 列表视图
*/
//列表初始化
private void initItemView() {
newBowenRecycleList.setLayoutManager(new LinearLayoutManager(getContext()));
articleAdapter = new ArticleAdapter(R.layout.item_newbowen, articleList);
newBowenRecycleList.setAdapter(articleAdapter);
itemClick(); //点击事件
itemPull_refresh(); //上拉加载
itemDrop_down();
itemHeadView();//头部布局
}
//列表点击事件
private void itemClick() {
//单击事件
articleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Intent clickIntent = new Intent(getContext(), PageDetailActivity.class);
clickIntent.putExtra("Url", articleList.get(position).getLink());
clickIntent.putExtra("Title", articleList.get(position).getTitle());
clickIntent.putExtra("NiceDate", articleList.get(position).getNiceDate());
clickIntent.putExtra("ChapterName", articleList.get(position).getChapterName());
clickIntent.putExtra("superChapterName", articleList.get(position).getSuperChapterName());
clickIntent.putExtra("Author", articleList.get(position).getAuthor());
startActivity(clickIntent);
}
});
//点击更多显示菜单
articleAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
ShowPopupMenu.showPopupMenu(view, getContext(), new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.all_removeItem:
articleList.remove(position);
articleAdapter.notifyDataSetChanged();
break;
case R.id.all_waitRead:
WaitRead waitRead = new WaitRead();
waitRead.setAuthor(articleList.get(position).getAuthor());
waitRead.setChapterName(articleList.get(position).getChapterName());
waitRead.setNiceDate(articleList.get(position).getNiceDate());
waitRead.setSuperChapterName(articleList.get(position).getSuperChapterName());
waitRead.setTitle(articleList.get(position).getTitle());
waitRead.setUrl(articleList.get(position).getLink());
waitRead.save();
Toast.makeText(getContext(), "已加入稍后阅读", Toast.LENGTH_SHORT).show();
default:
break;
}
return false;
}
});
}
});
}
//列表上拉加载更多
private void itemPull_refresh() {
articleAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
@Override
public void onLoadMoreRequested() {
++page_size;
getArticleData(page_size);
}
}, newBowenRecycleList);
}
//列表下拉刷新
private void itemDrop_down() {
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
articleList.clear();
page_size=0;
getArticleData(page_size);
refreshLayout.finishRefresh(1500);
}
});
}
//添加头部布局
private void itemHeadView() {
View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_index_banner, null);
banner = view.findViewById(R.id.banner);
articleAdapter.addHeaderView(view);
}
/**
* 获取网络数据
*/
//获取最新博文列表数据
private List<Article.DataBean.DatasBean> getArticleData(int pageSize) {
if (isOne) {
loading.showProgressDialog(getContext());
isOne = false;
}
Retrofit retrofit = RetrofitUtil.sendRequest(Api.allUrl);
Api_Interface api_interface = retrofit.create(Api_Interface.class);
retrofit2.Call<Article> call = api_interface.getArticleList(pageSize);
call.enqueue(new retrofit2.Callback<Article>() {
@Override
public void onResponse(retrofit2.Call<Article> call, retrofit2.Response<Article> response) {
if (response.isSuccessful()) {
Article data = response.body();
for (Article.DataBean.DatasBean articleData : data.getData().getDatas()) {
Article.DataBean.DatasBean article = new Article.DataBean.DatasBean();
article.setAuthor(articleData.getAuthor());
article.setChapterName(articleData.getChapterName());
article.setSuperChapterName(articleData.getSuperChapterName());
String title = articleData.getTitle()
.replace("&", "")
.replace("“", "")
.replace("”", "")
.replace("·", "")
.replace("—", "");
article.setTitle(title);
article.setNiceDate(articleData.getNiceDate());
article.setLink(articleData.getLink());
articleList.add(article);
}
}
Objects.requireNonNull(getActivity()).runOnUiThread(new Runnable() {
@Override
public void run() {
loading.closeProgressDialog();
articleAdapter.setNewData(articleList);
}
});
}
@Override
public void onFailure(retrofit2.Call<Article> call, Throwable t) {
Log.i("状态", "失败");
}
});
return articleList;
}
//获取轮播图数据
private void getBannerData() {
Retrofit retrofit = RetrofitUtil.sendRequest(Api.allUrl);
Api_Interface api_interface = retrofit.create(Api_Interface.class);
retrofit2.Call<Carousel> call = api_interface.getCarouselList();
imagePath = new ArrayList<>();//图片集合
titles = new ArrayList<>();//标题集合
carouselList = new ArrayList<>();
call.enqueue(new retrofit2.Callback<Carousel>() {
@Override
public void onResponse(retrofit2.Call<Carousel> call, retrofit2.Response<Carousel> response) {
if (response.isSuccessful()) {
Carousel carousel = response.body();
assert carousel != null;
for (Carousel.DataBean carouselData : carousel.getData()) {
Carousel.DataBean carouseBean = new Carousel.DataBean();
carouseBean.setImagePath(carouselData.getImagePath());
carouseBean.setDesc(carouselData.getDesc());
carouseBean.setTitle(carouselData.getTitle());
carouseBean.setUrl(carouselData.getUrl());
carouselList.add(carouseBean);
imagePath.add(carouselData.getImagePath());
titles.add(carouselData.getTitle());
}
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
BannerImg();
}
});
}
@Override
public void onFailure(retrofit2.Call<Carousel> call, Throwable t) {
Log.i("状态", "失败");
}
});
}
/**
* 其他
*/
//fragment销毁回调方法
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
//轮播图设置
private void BannerImg() {
//设置圆形指示器和标题
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
//设置图片加载器
banner.setImageLoader(new GlideImageLoader()); //必要
//设置图片集合
banner.setImages(imagePath);
//设置滑动特效
banner.setBannerAnimation(Transformer.CubeOut);
//设置标题
banner.setBannerTitles(titles);
//轮播时间
banner.setDelayTime(3000);
//设置点击事件
banner.setOnBannerListener(new OnBannerListener() {
@Override
public void OnBannerClick(int position) {
Intent intent = new Intent(getContext(), PageDetailActivity.class);
intent.putExtra("Url", carouselList.get(position).getUrl());
intent.putExtra("Title", carouselList.get(position).getTitle());
startActivity(intent);
}
});
//取消指示器
// Carousel.setBannerStyle(BannerConfig.NOT_INDICATOR);
//banner设置方法全部调用完毕时最后调用
banner.start();
}
}