我有一个全屏的RecyclerView活动,用于加载图片。用户可以左右滑动以查看下一项(通过
问题是当我点击FAB时,它会下载下一项而不是当前的图像。因为
Adapter -
在活动(Activity)中 -
LinearSnapHelper
实现)。在图片上方,有一个悬浮操作栏,其操作是下载当前可见的图像。问题是当我点击FAB时,它会下载下一项而不是当前的图像。因为
onBindViewHolder
提前执行下一项和上一项。如果FAB是项目布局的一部分,则不会出现问题,但在这种情况下,Fab是Activity布局的一部分。Adapter -
public class FullWallAdapter extends RecyclerView.Adapter<FullscreenViewHolder> {
private ArrayList<String> mWallKeys;
private OnFullSetupViewListener mOnFullSetupViewListener;
public FullWallAdapter(ArrayList<String> wallKeys, OnFullSetupViewListener listener) {
this.mWallKeys = wallKeys;
this.mOnFullSetupViewListener = listener;
}
@Override
public FullscreenViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fullscreen_wallpaper_preview, parent, false);
return new FullscreenViewHolder(itemView);
}
@Override
public void onBindViewHolder(final FullscreenViewHolder holder, int position) {
ValueEventListener wallListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ImageModel image = dataSnapshot.getValue(ImageModel.class);
mOnFullSetupViewListener.onSetupView(holder, image, holder.getAdapterPosition(), dataSnapshot.getKey());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("WallAdapter", "Error occurred: " + databaseError.getMessage());
}
};
DatabaseReference ref = FireUtils.getWallpapersRef().child(mWallKeys.get(position));
ref.addValueEventListener(wallListener);
holder.mWallRef = ref;
holder.mWallListener = wallListener;
}
@Override
public int getItemCount() {
return mWallKeys.size();
}
@Override
public void onViewRecycled(FullscreenViewHolder holder) {
super.onViewRecycled(holder);
if (holder.mWallListener != null)
holder.mWallRef.removeEventListener(holder.mWallListener);
}
public interface OnFullSetupViewListener {
void onSetupView(FullscreenViewHolder holder, ImageModel image, int position, String wallKey);
}
}
在活动(Activity)中 -
FullWallAdapter adapter = new FullWallAdapter(wallKeys, new FullWallAdapter.OnFullSetupViewListener() {
@Override
public void onSetupView(final FullscreenViewHolder holder, final ImageModel image,
int position, final String wallKey) {
holder.setImageView(image);
downloadFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
incrementDownload(wallKey, image);
String params;
params = wallpaper.getPhotoJson();
fetchSave(params);
}
});
}
});
llm
是线性布局管理器。 - Adi Nugroho