【问题标题】:How to hide/show progress bar from method onLoadingStateChanged in the FirestorePagingAdapter?如何从 FirestorePagingAdapter 中的方法 onLoadingStateChanged 隐藏/显示进度条?
【发布时间】:2021-07-26 13:45:10
【问题描述】:

在我的应用中有一个带有 FirestorePagingAdapter 的回收站视图。在适配器中有一个方法 onLoadingStateChanged 负责加载状态。我想在下一页加载时显示进度条并在加载时隐藏它,这意味着当 case 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】:

    要显示/隐藏您的 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;
        }
    }
    

    【讨论】:

    • 您好 Alex mamo 非常感谢您,顺便说一句,我在 youtube 上观看了您的视频)。您的解决方案对我有帮助,但相对而言,我会解释:当适配器单独放在文件中时它不起作用,但是当我将适配器放在片段中的函数中时它对我有用。
    • 再次您好,不客气 :) 很高兴听到它对您有帮助,并感谢您观看我的教程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    相关资源
    最近更新 更多