Di aplikasi saya ada recyclerview dengan FirestorePagingAdapter. Di adaptor ada metode onLoadingStateChanged yang bertanggung jawab atas status pemuatan. Saya ingin menampilkan bilah kemajuan saat halaman berikutnya memuat dan menyembunyikannya saat dimuat, itu berarti ketika case LOADING_INITIAL - tampilkan bilah kemajuan. Kode di bawah ini akan lebih menjelaskan:

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);


        }
    }
}

Masalahnya ada di sini:

...
switch (state) {
            case LOADING_INITIAL:

                homeRecyclerViewProgressBar.setVisibility(View.VISIBLE);

                // The initial load has begun
                ........
1
anasalim28 10 Maret 2019, 04:38

1 menjawab

Jawaban Terbaik

Untuk menampilkan/menyembunyikan homeRecyclerViewProgressBar Anda, harap gunakan baris kode berikut dalam metode onLoadingStateChanged() Anda:

@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;
    }
}
1
Alex Mamo 26 Maret 2019, 14:37