1

我已经实现了一个EndlessScrollfor RecyclerView(Only Images in it) 或者我们可以说Pagination但是在实现之后我不知道为什么图像没有从Firebase

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

顺便说一句,在问这个 SO QI 之前问了一个关于如何使用 StaggeredGridLayoutManager 实现分页的问题,但是在实现了我的 Orignal 问题的答案之后,没有错误,但图像出现了 这是问题链接,以供参考,了解我如何在我的recycler view -如何使用 StaggeredGridLayoutManager 在 recyclerview 中实现无限滚动(分页)

这是代码

Home_Fragment.java // 对于那些引用我在上面放置链接的原始问题的人,这是一个与原始问题中的原始问题不同的片段,我已包含 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 // 适配器类

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

        }


        }
    }
4

1 回答 1

1

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

理想情况下,您会将 mUploads 放在适配器类中,并且仅通过 . setUploads()addUploads()

编辑

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

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

然后在你打电话postsAdapter.setUploads(mUploads);之前的活动电话中

//notify the adapter
postsAdapter.notifyDataSetChanged();

试试看,让我知道。

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

于 2021-10-25T00:29:09.057 回答