gpt4 book ai didi

android - 如何将分页请求发送到firebase以实现无限滚动的recyclerview

转载 作者:行者123 更新时间:2023-12-04 23:49:04 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中包含我想要的分页,例如在我的 firebase 中我有 500 个要在回收站 View 中显示的图像我想要的是在开始时在回收站 View 中加载前 10 或 15 个图像当用户到达最后一个项目(图片)的末尾时,它会加载接下来的 10 张图片(就像 Instagram 或 facebook 一样)

Note:- if anyone wants more reference of my code please tell me I willupdate my question


这是我的代码
Home_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);
// listState = savedInstanceState.getParcelable("ListState");

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


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();
Log.d("tag", "LOAD NEXT ITEM");
}
}
}
});
// 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() {
@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();
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 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);

}


}
}
更新 //根据评论部分的要求添加了 Upload.java 文件代码
上传.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;
}
}

最佳答案

首先,您应该将获取数据的逻辑与 UI 分开。然后你的滚动 Controller (RecyclerView)应该控制应该调用哪些图像来下载(所以当它加载时它应该要求第一批图像,当滚动发生时 - 要求另一批,等等)。下载和缓存图像并不是一件容易的事,这就是为什么我们有 GlideCoil在安卓世界。
作为旁注,请查看 https://developer.android.com/topic/libraries/architecture/paging/v3-paged-datahttps://developer.android.com/topic/libraries/architecture/paging .
请检查您要添加到 mUploads 列表中的内容因为每次添加项目时,都会预先清除列表mUploads.clear();这可能不是你想要的。
一些注意事项:

  • 不要让它静态public static List<Upload> mUploads;
  • 不需要存储mcontext , 上下文已经存在
  • 从不必要的东西中去除问题(例如,放下微光,修复问题,修复后添加微光)
  • 关于android - 如何将分页请求发送到firebase以实现无限滚动的recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69703763/

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