我正在我的应用程序中实现分页概念我从数据库中获取数据15当我有超过15个数据它工作正常时,进度条工作正常
问题是:当只有4或5条记录时,进度条会继续进行,这不应该像只有当我滚动才能获得更多数据时才会出现进度条
我该如何解决?
final RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(mContext);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(null);
parishNewsAdapter = new ParishNewsAdapter(recyclerView, listParishNews, mActivity, mContext);
recyclerView.setAdapter(parishNewsAdapter);
parishNewsAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
finalList.add(null);
parishNewsAdapter.notifyItemInserted(finalList.size() - 1);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ArrayList<ParishNewsModel> mList = new ArrayList<>();
finalList.remove(finalList.size() - 1);
parishNewsAdapter.notifyItemRemoved(finalList.size());
index = index + 15;
int end = index + 1;
mList = tableHelper.getParishNews(end);
if (mList.size() == 0) {
ParishNewsAdapter.LoadingViewHolder.progressBar.setVisibility(View.GONE);
}
parishNewsAdapter.addObjectToList(mList);
parishNewsAdapter.notifyDataSetChanged();
parishNewsAdapter.setLoaded();
}
}, 1000);
}
});
This is where i just call the adapter where the progress should show when i swipe to see more data
here is my adapter
private ArrayList<ParishNewsModel> parishList;
ParishNewsModel parishNewsModel;
Context mContext;
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
private OnLoadMoreListener onLoadMoreListener;
private boolean isLoading;
private Activity activity;
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
int limit = 10;
public ParishNewsAdapter(RecyclerView recyclerView, ArrayList<ParishNewsModel> parishList, Activity activity, Context mContext) {
this.activity = activity;
this.parishList = parishList;
this.mContext = mContext;
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
isLoading = true;
}
}
});
}
public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
this.onLoadMoreListener = mOnLoadMoreListener;
}
@Override
public int getItemViewType(int position) {
// return parishList.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
if (parishList.get(position) == null) {
return VIEW_TYPE_LOADING;
} else {
return VIEW_TYPE_ITEM;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
View view = LayoutInflater.from(activity).inflate(R.layout.parish_news_reference_layout, parent, false);
return new MyViewHolder(view);
} else if (viewType == VIEW_TYPE_LOADING) {
View view = LayoutInflater.from(activity).inflate(R.layout.lazy_load_progress_bar, parent, false);
return new LoadingViewHolder(view);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof MyViewHolder) {
ParishNewsModel newsModel = parishList.get(position);
MyViewHolder myViewHolder = (MyViewHolder) holder;
myViewHolder.title.setText(newsModel.getTitleOfTheNews());
myViewHolder.short_description.setText(Html.fromHtml(newsModel.getDescription()));
myViewHolder.date_of_text.setText(Html.fromHtml(newsModel.getDateOfNews()));
String str = newsModel.getParishNewsImage();
Glide.with(mContext).load(str).thumbnail(Glide.with(mContext).load(R.drawable.jesus)).into(myViewHolder.circleImageView);
} else if (holder instanceof LoadingViewHolder) {
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.progressBar.setVisibility(View.VISIBLE);
loadingViewHolder.progressBar.setIndeterminate(true);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
parishNewsModel = parishList.get(position);
AppUtils.model = parishNewsModel;
if (AppUtils.model != null) {
Intent intent = new Intent(mContext, ScrollingActivity.class);
mContext.startActivity(intent);
}
}
});
}
static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, short_description, date_of_text;
ImageView circleImageView;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title_parish_news);
short_description = (TextView) itemView.findViewById(R.id.short_description);
date_of_text = (TextView) itemView.findViewById(R.id.date_published);
circleImageView = (ImageView) itemView.findViewById(R.id.news_image);
}
}
public static class LoadingViewHolder extends RecyclerView.ViewHolder {
public static ProgressBar progressBar;
public LoadingViewHolder(View itemView) {
super(itemView);
progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar1);
}
}
@Override
public int getItemCount() {
return parishList == null ? 0 : parishList.size();
}
public void addObjectToList(ArrayList<ParishNewsModel> list) {
parishList.addAll(list);
}
public void setLoaded() {
isLoading = false;
}
我如何进一步行动请指导我
答案 0 :(得分:1)
只需从onBindViewHolder上的适配器中删除进度条并放入其中 postDelayed。这是因为当你向下滚动onBindViewHolder时会再次调用 并再次设置进度条仅在其中可见。