Android:RecyclerView 显示图像 2 次
Posted
技术标签:
【中文标题】Android:RecyclerView 显示图像 2 次【英文标题】:Android: RecyclerView Show images 2 times 【发布时间】:2021-12-21 01:28:07 【问题描述】:我的应用程序从 firebase-Realtime 获取图像,我正在使用 recyclerview 显示它们,但它显示图像 2 次,这意味着如果我在 firebase 上有 5 个图像,它将显示相同的 5 个图像,但复制它们是 10 个图像.
我的应用从 firebase-Realtime 获取图像,
我的适配器:
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.private_code_recycler_layout, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
@Override
public void onBindViewHolder(ViewHolder holder, final int position)
Picasso.get()
.load(mImages.get(position))
.placeholder(R.drawable.ic_baseline_cloud_download_24)
.memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE)
.resize(480,480)
.into(holder.image);
holder.parentLayout.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Intent intent = new Intent(mContext, FullScreenImageViewActivity.class);
intent.putExtra("ImageURL", mImages.get(position));
mContext.startActivity(intent);
);
活动:
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
binding = ActivityWorkDetailsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
workUid_details = getIntent().getExtras().getString("UID_Details");
String title = getIntent().getExtras().getString("name");
String description = getIntent().getExtras().getString("description");
String location = getIntent().getExtras().getString("location");
String path = getIntent().getExtras().getString("path");
databaseReference = FirebaseDatabase.getInstance().getReference().child("Work").child(path);
valueEventListener = new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot snapshot)
binding.workDetailsTitle.setText(title);
binding.workDetailsDescription.setText(description);
binding.workDetailsLocation.setText(location);
binding.getUIDDetails.setText(workUid_details);
for (DataSnapshot dataSnapshot : snapshot.child("images").getChildren())
String value = String.valueOf(dataSnapshot.child("image").getValue());
imagesFromURL.add(value);
initRecyclerView();
@Override
public void onCancelled(@NonNull DatabaseError error)
;
private void initRecyclerView()
binding.workDetailsImage.setNestedScrollingEnabled(false);
binding.workDetailsImage.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
binding.workDetailsImage.setHasFixedSize(true);
WorkDetailsAdapter adapter = new WorkDetailsAdapter(this, imagesFromURL);
binding.workDetailsImage.setAdapter(adapter);
binding.progressBar.setVisibility(View.VISIBLE);
【问题讨论】:
我认为您现在也应该添加“java”标签吗?现在推荐的语言是 Kotlin,所以很多人也使用它。只是为了避免混淆(迁移到 Kotlin 不是强制性的,Java 就可以了。Kotlin 只是更容易编写) 【参考方案1】:在添加 imageUrl 之前添加 imagesFromURL.clear();
。
imagesFromURL.clear();//add this line
for (DataSnapshot dataSnapshot : snapshot.child("images").getChildren())
String value = String.valueOf(dataSnapshot.child("image").getValue());
imagesFromURL.add(value);
【讨论】:
成功了,谢谢,你能解释一下是什么原因吗? @Mini 从firebase获取的数据就是全部了,所以每次都需要初始化或者清空列表。以上是关于Android:RecyclerView 显示图像 2 次的主要内容,如果未能解决你的问题,请参考以下文章
Android:在 Recyclerview 中更改图像旋转
Android:在 RecyclerView 中获取用户输入和显示