S
S
SimpleName2019-01-19 16:31:50
Java
SimpleName, 2019-01-19 16:31:50

How to show ProgressBar while loading RecyclerView?

There is a RecyclerView with pictures that are loaded from the Internet. How should I connect the ProgressBar so that it spins until the RecyclerView appears? (The ProgressBar should not show the exact loading value, but just spin until the RecyclerView with pictures appears)

RecyclerView adapter
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {

    private TestFragment mContext;
    private ArrayList<GalleryGridObject> galleryArrayList;

    public TestAdapter(TestFragment mContext, ArrayList<GalleryGridObject> galleryArrayList){
        this.mContext=mContext;
        this.galleryArrayList=galleryArrayList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_coupons, parent, false);

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        GalleryGridObject gallery = galleryArrayList.get(position);

        Glide.with(mContext).load(gallery.getCompanyImage()).into(holder.imageGallery);
    }

    @Override
    public int getItemCount() {
        if(galleryArrayList==null) return 0;
        return galleryArrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView imageGallery;

        public ViewHolder(View view) {
            super(view);
            imageGallery = (ImageView)itemView.findViewById(R.id.coupons_picture);
        }
    }
}

fragment
public class TestFragment extends Fragment {

    private ArrayList<GalleryGridObject> galleryList = new ArrayList<GalleryGridObject>();
    private TestAdapter mAdapter;
    private RecyclerView recyclerView;
    ProgressBar progressBar;

    int spanCount = 2; // 2 columns

    public TestFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_coupons, container, false);

        recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
        progressBar = (ProgressBar) view.findViewById(R.id.progressBar);

        mAdapter = new TestAdapter(this, galleryList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mAdapter);

        recyclerView.setHasFixedSize(true);

        prepareGalleryData();

        return view;
    }



    private void prepareGalleryData()
    {
        GalleryGridObject gallery = new GalleryGridObject("https://firebasestorage.googleapis.com/v0/b/socialapp-217e6.appspot.com/o/KFC%2F16.jpg?alt=media&token=c2bb063b-3a72-4511-9a09-7679eaea1422");
        galleryList.add(gallery);

        gallery = new GalleryGridObject("https://firebasestorage.googleapis.com/v0/b/socialapp-217e6.appspot.com/o/KFC%2F16.jpg?alt=media&token=c2bb063b-3a72-4511-9a09-7679eaea1422");
        galleryList.add(gallery);

        gallery = new GalleryGridObject("https://firebasestorage.googleapis.com/v0/b/socialapp-217e6.appspot.com/o/KFC%2F16.jpg?alt=media&token=c2bb063b-3a72-4511-9a09-7679eaea1422");
        galleryList.add(gallery);

        gallery = new GalleryGridObject("https://firebasestorage.googleapis.com/v0/b/socialapp-217e6.appspot.com/o/KFC%2F16.jpg?alt=media&token=c2bb063b-3a72-4511-9a09-7679eaea1422");
        galleryList.add(gallery);

        gallery = new GalleryGridObject("https://firebasestorage.googleapis.com/v0/b/socialapp-217e6.appspot.com/o/KFC%2F16.jpg?alt=media&token=c2bb063b-3a72-4511-9a09-7679eaea1422");
        galleryList.add(gallery);

        gallery = new GalleryGridObject("https://firebasestorage.googleapis.com/v0/b/socialapp-217e6.appspot.com/o/KFC%2F16.jpg?alt=media&token=c2bb063b-3a72-4511-9a09-7679eaea1422");
        galleryList.add(gallery);

        gallery = new GalleryGridObject("https://firebasestorage.googleapis.com/v0/b/socialapp-217e6.appspot.com/o/KFC%2F16.jpg?alt=media&token=c2bb063b-3a72-4511-9a09-7679eaea1422");
        galleryList.add(gallery);

        gallery = new GalleryGridObject("https://firebasestorage.googleapis.com/v0/b/socialapp-217e6.appspot.com/o/KFC%2F16.jpg?alt=media&token=c2bb063b-3a72-4511-9a09-7679eaea1422");
        galleryList.add(gallery);

        mAdapter.notifyDataSetChanged();
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question