gpt4 book ai didi

android - 实现无尽滚动(分页)后,图像未在回收站 View 中显示(加载)

转载 作者:行者123 更新时间:2023-12-04 17:10:04 24 4
gpt4 key购买 nike

我已经实现了一个 EndlessScrollRecyclerView (只有图像)或者我们可以说 Pagination但是在实现之后我不知道为什么图像没有从 Firebase 加载
所以我试图从我的适配器类中包含一个占位符图像,但默认的占位符图像也没有显示出来
顺便说一句,在问这个问题之前,QI 问了一个关于如何使用 StaggeredGridLayoutManager 实现分页的问题,但在实现我的原始问题的答案后,没有错误,但图像显示这是有关我如何在我的中实现分页的问题链接,供引用回收者 View - How to implement endless scroll (pagination) in recyclerview with StaggeredGridLayoutManager
这是代码
首页_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);

}


}
}

最佳答案

我认为你需要像 postsAdapter.setUploads(mUploads) 这样的东西在您调用 postsAdapter.notifyDataSetChanged() 之前在适配器中重置它们;因为我不清楚您的适配器如何处理 mUploads,因为您只在构造函数中传递它一次。我不认为将 mUploads 传递给适配器构造函数会保留指向原始对象的指针。
理想情况下,您将 mUploads 放在适配器类中,并且仅通过像 . setUploads() , addUploads()等等。
编辑
所以基本上我建议您将以下方法添加到您的适配器类中:

public void setUploads(List<Upload> uploads){
mUploads=uploads;
}
然后在您的 Activity 电话中 postsAdapter.setUploads(mUploads);就在你打电话之前
//notify the adapter
postsAdapter.notifyDataSetChanged();
试试看,让我知道。
关于您使用完整图像,让我告诉您这会给您带来麻烦。我曾经做过同样的事情,不仅您的图像需要很长时间才能加载,而且您的 recyclerview 也不会顺利移动,除非您希望您的应用程序只能在最高端手机中使用。我建议您在 firebase 中存储一些可接受的小版本照片,它们不必是缩略图,但绝对小于 1 兆像素。最好尝试不同的分辨率,看看哪种在一系列不同的设备上效果最好。即便如此,您可能希望在渲染照片时放入加载微调器或虚拟图像。

关于android - 实现无尽滚动(分页)后,图像未在回收站 View 中显示(加载),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69671882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com