【项目FJU】使用SwipeRefreshLayout+Recy
2020-03-26 本文已影响0人
秀叶寒冬
效果截图
上拉加载更多gif预览
test.gif
需要用到的知识
- setColorSchemeResources:设置下拉刷新进度条的颜色
- setOnRefreshListener:设置下拉刷新监听
- android:overScrollMode:设置滑动到边缘时无效果模式
- ContentLoadingProgressBar:内容加载进度条,继承ProgressBar,该进度在显示时将以最短时间内显示,在消失时最短时间内消失,避免了UI闪烁。
- 设置上拉加载更多进度条颜色
mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(mContext,R.color.f_font_tabbar_text_selected), PorterDuff.Mode.MULTIPLY);
- progressBar的style
<!-- 默认的进度条风格. 这是一个中等的圆形进度条 -->
<attr name="progressBarStyle" format="reference" />
<!-- 水平进度条风格. 这是一个水平的进度条. -->
<attr name="progressBarStyleHorizontal" format="reference" />
<!-- 小型进度条风格. 这是一个小型的圆形进度条 -->
<attr name="progressBarStyleSmall" format="reference" />
<!-- 标题样式的小ProgressBar。 这是一个小的圆形进度条,将放置在标题栏中。 -->
<attr name="progressBarStyleSmallTitle" format="reference" />
<!-- 大ProgressBar样式。 这是一个很大的圆形进度条 -->
<attr name="progressBarStyleLarge" format="reference" />
<!-- 反ProgressBar样式。 这是一个中等的圆形进度栏。 -->
<attr name="progressBarStyleInverse" format="reference" />
<!--小型反向ProgressBar样式。 这是一个小的圆形进度栏。 -->
<attr name="progressBarStyleSmallInverse" format="reference" />
<!-- 大的反向ProgressBar样式。 这是一个很大的圆形进度栏。 -->
<attr name="progressBarStyleLargeInverse" format="reference" />
- android:elevation:设置控件阴影
- newState的三种状态
//空闲,RecyclerView空闲时返回的状态值,RecyclerView完全停止
public static final int SCROLL_STATE_IDLE = 0;
//拖拽,RecyclerView滑动时返回的状态值
public static final int SCROLL_STATE_DRAGGING = 1;
//停止,RecyclerView停止时返回的状态值,此时手指已经离开屏幕,但RecyclerView可能由于惯性依然在滑动
public static final int SCROLL_STATE_SETTLING = 2;
代码实现
- SwipeRefreshLayout+RecyclerView布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:overScrollMode="never"
/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
- RecyclerView加载Adapter
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
dataBOList = MakeListDataDAO.parseMakeListData(getContext());
mAdapter = new ArticleAdapter(getActivity(),dataBOList);
mRecyclerView.setAdapter(mAdapter);
- 下拉刷新
mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
dataBOList.clear();
dataBOList.addAll(MakeListDataDAO.refreshMakeListData(getContext()));
mAdapter.notifyDataSetChanged();
mRefreshLayout.setRefreshing(false);
}
});
mRefreshLayout.setColorSchemeResources(R.color.f_font_tabbar_text_selected);
- 上拉加载更多监听
/**
* Created by yds
* on 2020/3/24.
*/
public abstract class OnLoadMoreListener extends RecyclerView.OnScrollListener {
private int countItem;
private int lastItem;
private boolean isScrolled = false;
private static boolean isAllScreen = false;
private RecyclerView.LayoutManager mLayoutManager;
protected abstract void onLoading(int countItem, int lastItem);
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_DRAGGING || newState == RecyclerView.SCROLL_STATE_SETTLING) {
isScrolled = true;
isAllScreen = true;
} else {
isScrolled = false;
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
mLayoutManager = recyclerView.getLayoutManager();
countItem = mLayoutManager.getItemCount();
lastItem = ((LinearLayoutManager) mLayoutManager).findLastCompletelyVisibleItemPosition();
}
if (isScrolled && countItem != lastItem && lastItem == countItem - 1) {
onLoading(countItem, lastItem);
}
}
public static boolean isAllScreen(){
return isAllScreen;
}
- 上拉加载更多
mRecyclerView.addOnScrollListener(new OnLoadMoreListener() {
@Override
protected void onLoading(int countItem, int lastItem) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
dataBOList.addAll(MakeListDataDAO.refreshMakeListData(getContext()));
mAdapter.notifyDataSetChanged();
if (mRefreshLayout.isRefreshing()){
mRefreshLayout.setRefreshing(false);
}
}
},3000);
}
});
- Adapter
/**
* Created by yds
* on 2020/3/10.
*/
public class ArticleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private static final int TYPE_CONTENT = 0;
private static final int TYPE_FOOTER = 1;
private List<MakeListDataBO> mDataBOList;
public ArticleAdapter(Context mContext, List<MakeListDataBO> dataBOList) {
this.mContext = mContext;
this.mDataBOList = dataBOList;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_adapter_item_article, parent, false);
// return new ViewHolder(view);
if (viewType == TYPE_CONTENT){
View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_adapter_item_article, parent, false);
return new ViewHolder(view);
}else{
View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_adapter_item_footer, parent, false);
return new FootViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position)==TYPE_FOOTER){
FootViewHolder viewHolder = (FootViewHolder) holder;
viewHolder.mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(mContext,R.color.f_font_tabbar_text_selected), PorterDuff.Mode.MULTIPLY);
}else{
ViewHolder viewHolder = (ViewHolder) holder;
Glide.with(mContext).load(mDataBOList.get(position).getHeadUrl()).thumbnail(0.3f).into(viewHolder.mCircleImageView);
viewHolder.mUserName.setText(mDataBOList.get(position).getUserName());
viewHolder.mTime.setText(mDataBOList.get(position).getTime());
viewHolder.mAbstract.setText(mDataBOList.get(position).getAbstractStr());
List<String> mList = mDataBOList.get(position).getList();
viewHolder.mImageView.setImagesData(mList);
}
}
@Override
public int getItemCount() {
return mDataBOList.size() + 1;
}
@Override
public int getItemViewType(int position) {
if (position == mDataBOList.size()&& OnLoadMoreListener.isAllScreen()) {
return TYPE_FOOTER;
}
return TYPE_CONTENT;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private MultiImageView mImageView;
private CirCleImageView mCircleImageView;
private TextView mUserName;
private TextView mTime;
private TextView mAbstract;
// private RecyclerView mRecyclerView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.image_three);
mCircleImageView = itemView.findViewById(R.id.circle_img);
mUserName = itemView.findViewById(R.id.user_name);
mTime = itemView.findViewById(R.id.time);
mAbstract = itemView.findViewById(R.id.abstract_article_tv);
// mRecyclerView = itemView.findViewById(R.id.image_recyclerview);
}
}
public class FootViewHolder extends RecyclerView.ViewHolder {
private ContentLoadingProgressBar mProgressBar;
public FootViewHolder(@NonNull View itemView) {
super(itemView);
mProgressBar = itemView.findViewById(R.id.load_more_progress);
}
}
}