如何隐藏/显示FirestorePagingAdapter中onLoadingStateChanged方法的进度条?

问题描述 投票:1回答:1

在我的应用程序中,有一个使用FirestorePagingAdapter的recyclerview。在适配器中有onLoadingStateChanged方法,负责加载状态。我希望在下一页加载时显示进度条,并在加载时隐藏它,这意味着在LOADING_INITIAL情况下显示进度条。下面的代码将更多解释:

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
                ........
java android android-recyclerview google-cloud-firestore firebaseui
1个回答
0
投票

要显示/隐藏您的homeRecyclerViewProgressBar,请在onLoadingStateChanged()方法中使用以下代码行:

@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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.