1

I have implemented Pagination in my RecyclerView but after implementing pagination the images are not showing up

Ok I have implemented this pagination answer

Ok because the answer above is for LinearLayoutManager and I'm using StaggeredGridLayoutManager I have done some modifications in the addOnScrollListener

I have only included the necessary code of Home_Fragment.java so it doesn't get confusing and long but if you want more references to the code please tell me I will update the question

Home_Fragment.java

    private int previousTotal = 0;
    private final int visibleThreshold = 5;
    private RecyclerView postRecyclerView;
    private boolean loading = true;
    private int firstVisibleItem, visibleItemCount, totalItemCount;

 @SuppressLint("SourceLockedOrientationActivity")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

 StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        postRecyclerView.setLayoutManager(
                staggeredGridLayoutManager
        );


        postRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {

                visibleItemCount = staggeredGridLayoutManager.getChildCount();
                totalItemCount = staggeredGridLayoutManager.getItemCount();
                int[] firstVisibleItems = null;
                firstVisibleItems = staggeredGridLayoutManager.findFirstVisibleItemPositions(firstVisibleItems);
                if (loading) {
                    if (totalItemCount > previousTotal) {
                        loading = false;
                        previousTotal = totalItemCount;
                    }
                }

                if (!loading && (totalItemCount - visibleItemCount)
                        <= (firstVisibleItem + visibleThreshold)) {
                    getData();

                    loading = true;
                }
            }
        });

return view;
    }

private void getData() {
        databaseReference.addValueEventListener(new ValueEventListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.exists()) {
                    shimmerFrameLayout.stopShimmer();
                    shimmerFrameLayout.setVisibility(View.GONE);
                    postRecyclerView.setVisibility(View.VISIBLE);
                    mUploads.clear();
                    for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                        Upload upload = dataSnapshot.getValue(Upload.class);
                        assert upload != null;
                        upload.setmKey(dataSnapshot.getKey());
                        mUploads.add(upload);


                    }

                }
                postsAdapter.setUploads(mUploads);

                //notify the adapter
                postsAdapter.notifyDataSetChanged();

            }


            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

PostAdapter_Home.java

public class PostAdapter_Home extends RecyclerView.Adapter<PostAdapter_Home.PostViewHolder> {
    public static List<Upload> mUploads;
    public Context mcontext;

    public PostAdapter_Home(Context context, List<Upload> uploads) {
        mUploads = uploads;
        mcontext = context;
    }


    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(mcontext).inflate(R.layout.ex_home, parent, false);
        return new PostViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        Shimmer shimmer = new Shimmer.ColorHighlightBuilder()
                .setBaseColor(Color.parseColor("#F3F3F3"))
                .setBaseAlpha(1)
                .setHighlightColor(Color.parseColor("#E7E7E7"))
                .setHighlightAlpha(1)
                .setDropoff(50)
                .build();
        ShimmerDrawable shimmerDrawable = new ShimmerDrawable();
        shimmerDrawable.setShimmer(shimmer);
        Upload uploadCurrent = mUploads.get(position);
        Glide.with(mcontext)
                .load(uploadCurrent.getmImageUrl())
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .placeholder(shimmerDrawable)
                .centerCrop()
                .fitCenter()
                .into(holder.imageView);

//        holder.imageView.setOnClickListener(view -> changeScaleType(holder, position));

    }


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

    public void setUploads(List<Upload> uploads){
        mUploads=uploads;
    }
    public static class PostViewHolder extends RecyclerView.ViewHolder {

        private final ShapeableImageView imageView;

        public PostViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imagePostHome);

        }


    }
}

Update // Added Upload.java file as requested

Upload.java

package com.example.myappnotfinal.AdaptersAndMore;

import com.google.firebase.database.Exclude;

public class Upload {
    private String mImageUrl;
    private String mKey;
    private String mUserName;
    private String mComment;

    public Upload() {

    }

    public Upload(String imageUrl) {
        mImageUrl = imageUrl;
    }

    public String getmUserName() {
        return mUserName;
    }

    public void setmUserName(String mUserName) {
        this.mUserName = mUserName;
    }

    public String getmComment() {
        return mComment;
    }

    public void setmComment(String mComment) {
        this.mComment = mComment;
    }

    public String getmImageUrl() {
        return mImageUrl;
    }

    public void setmImageUrl(String mImageUrl) {
        this.mImageUrl = mImageUrl;
    }

    @Exclude
    public String getmKey() {
        return mKey;
    }

    @Exclude
    public void setmKey(String Key) {
        this.mKey = Key;
    }
}
Vasant Raval
  • 257
  • 1
  • 12
  • 31
  • Your Home Fragment Code wise RecyclerView did not initialize. At first, initialize RecyclerView. [Image Loder](https://stackoverflow.com/questions/32706246/recyclerview-adapter-and-glide-same-image-every-4-5-rows) [RecyclerView initialize](https://stackoverflow.com/questions/38174829/recyclerview-in-fragment-initialize) – Rezaul Khan Nov 04 '21 at 11:18
  • i have initialized the recycler view i just didn't induce that code – Vasant Raval Nov 04 '21 at 12:32
  • your image loader code is ok. and can you give me an image URL. and also you can try Picasso: [Picasso](https://square.github.io/picasso/) – Rezaul Khan Nov 04 '21 at 13:15
  • images are perfectly showing when i didn't include the pagination – Vasant Raval Nov 04 '21 at 13:31
  • ok please check the updated question – Vasant Raval Nov 04 '21 at 13:34
  • why do you add scroll listener in onCreateView? you should do it separately IMO. – rahimli Nov 08 '21 at 13:13
  • I have seen many answers that implemented the same way so I did so – Vasant Raval Nov 08 '21 at 16:13
  • I would think about using a modern architecture approach and usage of Android Jetpack Architecture Components. The pagination approach you used is from 2014 ;) Using MVVM architecture will make your code lot of more flexible. See: https://developer.android.com/topic/libraries/architecture/paging/v3-overview – Romain Scherfflein Nov 08 '21 at 16:46
  • but it is for kotlin – Vasant Raval Nov 09 '21 at 11:24
  • Sure, but it's worth it switching over to Kotlin. Sooner or later Java stuff becomes more and more irrelevant in the Android world and you will stuck on such old and complex solutions. – Romain Scherfflein Nov 11 '21 at 17:02
  • thanks for the suggestion actually I'm thinking to migrate my project to Kotlin but I'm pretty new and I don't know how the Kotlin community support is on SO – Vasant Raval Nov 11 '21 at 17:06
  • 1
    It's at least as good as for java IMHO and growing, cause more and more devs are switching. Once you got a bit familiar with it you'll love it i promise and you will think java is a language from the last century – Romain Scherfflein Nov 19 '21 at 15:39
  • Thank you so much , actually I'm making a social media kind of app , and I don't know java completely I'm just learning the stuff that I required for this particular app but atleast I'm familiar with java from quite a long time , but with Kotlin I'm not familiar that much I have stated learning the basics of Kotlin , should I switch from java to Kotlin now ,is it good for my app and for me – Vasant Raval Nov 20 '21 at 03:54
  • I'm really confused about the switching , should I do it right now, or sometime latter – Vasant Raval Nov 20 '21 at 03:54
  • hey there i have switched from java to kotlin and trying to add the paging dependency but there are some issues, can u help me – Vasant Raval Nov 24 '21 at 11:29

0 Answers0