效果截图
https://github.com/ydslib/Jianshu/tree/develop
需要用到的知识
- 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
- 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布局
- 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 {
private Context mContext;
private static final int TYPE_CONTENT = 0;
private static final int TYPE_FOOTER = 1;
private List mDataBOList;
public ArticleAdapter(Context mContext, List 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 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);
}
}
}
源码地址:https://github.com/ydslib/Jianshu/tree/develop