实施无限滚动(分页)后,图像未在 recyclerview 中显示(加载)

Images are not showing up (loading) in recyclerview after implementing Endless scroll (Pagination)

我已经为 RecyclerView 实现了 EndlessScroll(其中只有图像)或者我们可以说 Pagination 但是在实现之后我不知道为什么图像没有加载从 Firebase

所以我尝试从我的适配器中包含一个占位符图像 class 但默认的占位符图像也没有显示

顺便说一句,在问这个 SO Q 之前,我问了一个关于如何使用 StaggeredGridLayoutManager 实现分页的问题,但是在实现了我的原始问题的答案之后,没有错误,但是图像正在显示这是问题 link关于我如何在回收站视图中实现分页的参考 -

这是代码

Home_Fragment.java // 对于那些引用我原来问题的人,我在上面放了一个 link,这与我原来的问题不同在最初的问题中,我包含了 Profile_Fragment.java,但因为该文件更长,所以我包含了这个,因为与原始

相比,它的代码更少
private boolean loading = true;
    private int pastVisibleItems, 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);
        requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        MaterialToolbar materialToolbar = view.findViewById(R.id.toolbar);
        materialToolbar.setOnMenuItemClickListener(toolbarItemClickListener);
        postRecyclerView = view.findViewById(R.id.recyclerViewHome);
//        shimmerFrameLayout = view.findViewById(R.id.shimmerEffect);
//        this is for one item per scroll
//        SnapHelper snapHelper = new PagerSnapHelper();
//        snapHelper.attachToRecyclerView(verticalRecyclerView);
        postRecyclerView.setAdapter(postsAdapter);

        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        postRecyclerView.setLayoutManager(
                staggeredGridLayoutManager // I have 3 rows
        );


        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 (firstVisibleItems != null && firstVisibleItems.length > 0) {
                    pastVisibleItems = firstVisibleItems[0];
                }

                if (loading) {
                    if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                        loading = false;
                        getData();
                    }
                }
            }
        });
//        setupFirebaseAuth();
//        shimmerFrameLayout.startShimmer();
        mUploads = new ArrayList<>();
        postsAdapter = new PostAdapter_Home(getContext(), mUploads);
        postRecyclerView.setAdapter(postsAdapter);
        postRecyclerView.scrollToPosition(Home_Fragment.saved_position);
        return view;
    }


    private void getData() {
        databaseReference.addValueEventListener(new ValueEventListener() {
            @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);


                    }

                }

                //notify the adapter
                postsAdapter.notifyDataSetChanged();
                loading = true;
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                loading = true;
            }
        });
    }

PostAdapter_Home.java // 适配器 Class

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 static class PostViewHolder extends RecyclerView.ViewHolder {

        private final ShapeableImageView imageView;

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

        }


        }
    }

我想你需要像 postsAdapter.setUploads(mUploads) 这样的东西在你调用 postsAdapter.notifyDataSetChanged() 之前在适配器中重置它们;因为我不清楚你的适配器是如何处理 mUploads 的,因为你只在构造函数中传递了一次。我认为将 mUploads 传递给适配器构造函数不会保留指向原始对象的指针。

理想情况下,您可以将 mUploads 放入适配器 class 中,并且仅通过 . setUploads()addUploads()

编辑

所以基本上我建议您将以下方法添加到您的适配器 class:

public void setUploads(List<Upload> uploads){
   mUploads=uploads;
}

然后在您 activity 调用 postsAdapter.setUploads(mUploads); 之前调用

//notify the adapter
postsAdapter.notifyDataSetChanged();

试试看,然后告诉我。

关于你使用完整图片,我告诉你,这会给你带来麻烦。我曾经做过同样的事情,不仅你的图片需要很长时间才能加载,而且你的 recyclerview 也不会顺利移动,除非你希望你的应用程序只能在最高端的手机上使用。我建议您在 firebase 中存储一些可接受的小版本照片,它们不一定是缩略图,但绝对小于 1 兆像素。最好尝试不同的分辨率,看看哪种分辨率在一系列不同的设备上效果最好。即使这样,您也可能希望在渲染照片时放入加载微调器或虚拟图像。