问题描述
在我的应用中,有一个 FirestorePagingAdapter 的回收视图.在适配器中,有一种方法 onLoadingStateChanged 负责加载状态.我想在加载下一页时显示进度条,并在加载时隐藏它,这意味着装入LOADING_INITIAL 时-显示进度条.下面的代码将进一步说明:
In my app there is a recyclerview with FirestorePagingAdapter. In the adapter there is method onLoadingStateChanged who is responsible for the loading state. I want to show progress bar while the next page is loading and hide it when it loaded, that's means when case LOADING_INITIAL - show progress bar.The code below will more explain:
public class AdAdapter extends FirestorePagingAdapter<Ad, AdAdapter.AdHolder> {
private Context mContext;
private ArrayList<String> imageUrls;
private ProgressBar homeRecyclerViewProgressBar;
public AdAdapter(Context context, @NonNull FirestorePagingOptions<Ad> options) {
super(options);
this.mContext = context;
}
@NonNull
@Override
public AdAdapter.AdHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.ad_item, viewGroup, false);
AdHolder vh = new AdHolder(v);
mContext = viewGroup.getContext();
return vh;
}
@Override
protected void onBindViewHolder(@NonNull AdHolder holder, int position, @NonNull Ad model) {
holder.textViewTitle.setText(model.getTitle());
holder.textViewPrice.setText(String.valueOf(model.getPrice()));
imageUrls = model.getImagesUrls();
Glide.with(mContext)
.load(imageUrls.get(0))
.into(holder.imageView);
}
@Override
protected void onLoadingStateChanged(@NonNull LoadingState state) {
super.onLoadingStateChanged(state);
switch (state) {
case LOADING_INITIAL:
homeRecyclerViewProgressBar.setVisibility(View.VISIBLE);
// The initial load has begun
// ...
Log.d("someLog", "The initial load has begun");
return;
case LOADING_MORE:
// The adapter has started to load an additional page
// ...
Log.d("someLog", "The additional load has begun");
return;
case LOADED:
homeRecyclerViewProgressBar.setVisibility(View.GONE);
// The previous load (either initial or additional) completed
// ...
Log.d("someLog", "Has loaded");
return;
case ERROR:
// The previous load (either initial or additional) failed. Call
// the retry() method in order to retry the load operation.
// ...
Log.d("someLog", "Failed to load");
}
}
class AdHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
TextView textViewPrice;
ImageView imageView;
public AdHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.adTitleMain);
textViewPrice = itemView.findViewById(R.id.adPriceMain);
imageView = itemView.findViewById(R.id.imageView);
homeRecyclerViewProgressBar = itemView.findViewById(R.id.home_recycler_view_progressbar);
}
}
}
问题在这里:
...
switch (state) {
case LOADING_INITIAL:
homeRecyclerViewProgressBar.setVisibility(View.VISIBLE);
// The initial load has begun
........
推荐答案
要显示/隐藏您的 homeRecyclerViewProgressBar
,请在您的 onLoadingStateChanged()
中使用以下代码行方法:
To show/hide your homeRecyclerViewProgressBar
, please use the following lines of code in your onLoadingStateChanged()
method:
@Override
protected void onLoadingStateChanged(@NonNull LoadingState state) {
switch (state) {
case LOADING_INITIAL:
homeRecyclerViewProgressBar.setVisibility(View.VISIBLE);
break;
case LOADING_MORE:
case LOADED:
homeRecyclerViewProgressBar.setVisibility(View.GONE);
break;
case FINISHED:
homeRecyclerViewProgressBar.setVisibility(View.GONE);
break;
case ERROR:
retry();
break;
}
}
这篇关于如何从FirestorePagingAdapter中的onLoadingStateChanged方法隐藏/显示进度栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!