Infinite Progress bar using JSON and Recycler view Android?

一个人想着一个人 提交于 2021-01-29 07:06:33

问题


I want implement load more progress bar after 10 image or position in my recycler-view.

Here is some screenshot which give you more ideas

I am fetching data from JSON. Here is example of my code

[{

"name": "Thick Wallpaper",

"url":{

    "small": "https://i.pinimg.com/originals/b1/99/57/b19957de05529750cf4d8270441d5f95.jpg",

    "medium": "https://i.pinimg.com/originals/b1/99/57/b19957de05529750cf4d8270441d5f95.jpg",

    "large": "https://i.pinimg.com/originals/b1/99/57/b19957de05529750cf4d8270441d5f95.jpg"

},

"status":"New",

"uploadby":"Admin",

"timestamp": "July 7, 2020"
},{

"name": "Thick Wallpaper",

"url":{

    "small": "https://images.pexels.com/photos/1156684/pexels-photo-1156684.jpeg",

    "medium": "https://images.pexels.com/photos/1156684/pexels-photo-1156684.jpeg",

    "large": "https://images.pexels.com/photos/1156684/pexels-photo-1156684.jpeg"

},

"status":"Old",

"uploadby":"Admin",

"timestamp": "July 1, 2020"

...

...

...
}]

In Java this is how i fetch images in java. Here in each row 2 images is showing in the grid form

RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);

    fetchImages(endpoint);

    private void fetchImages(String endpoint) {


    JsonArrayRequest req = new JsonArrayRequest(endpoint,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());


                    images.clear();
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject object = response.getJSONObject(i);
                            Image image = new Image();
                            image.setName(object.getString("name"));

                            JSONObject url = object.getJSONObject("url");
                            image.setSmall(url.getString("small"));
                            image.setMedium(url.getString("medium"));
                            image.setLarge(url.getString("large"));
                            image.setStatus(object.getString("status"));
                            image.setUploadby(object.getString("uploadby"));
                            image.setTimestamp(object.getString("timestamp"));

                            images.add(image);

                        } catch (JSONException e) {
                            Log.e(TAG, "Json parsing error: " + e.getMessage());
                        }
                    }

                    mAdapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Error: " + error.getMessage());
        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req);
}

Here is my Gallery Adapter

public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.MyViewHolder>  {
private List<Image> images;
private Context mContext;

public static class MyViewHolder extends RecyclerView.ViewHolder {
    public ImageView thumbnail;
    TextView status;

    public MyViewHolder(View view) {
        super(view);
        thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
        status = view.findViewById(R.id.status_gallery);
    }
}


public GalleryAdapter(Context context, List<Image> images) {
    mContext = context;
    this.images = images;
}

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

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Image image = images.get(position);

    Glide.with(mContext).load(image.getMedium())
            .thumbnail(0.5f)
            .into(holder.thumbnail);

}

@Override
public int getItemCount() {
    return images.size();
}
}

来源:https://stackoverflow.com/questions/63335533/infinite-progress-bar-using-json-and-recycler-view-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!