Firebase Recycleview重复图像

时间:2018-12-01 10:54:12

标签: android

我正在开发一个从Firebase检索图像的应用程序。我已成功从Firebase检索了所有图像,但是当我选择某个图像作为收藏夹时,它将在RecyclerView的新活动中将所有选择的图像复制两次或三次。请任何人帮助我。在此先感谢。

这是我的代码。

public class Fragment3 extends Fragment {

ArrayList<String> ImagesList = (ArrayList) (ListofData.FavoriteList = new ArrayList<>());
Set<String> hashset = new HashSet<String>(ImagesList);
RecyclerView recyclerView;
FragThreeAdapter adapter;

public Fragment3() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_fragment3, container, false);

    FirebaseHandler firebaseHandler = new FirebaseHandler(getActivity());
    firebaseHandler.getFavoriteUrlRef().addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot d : dataSnapshot.getChildren()) {
                String images = d.getValue().toString();
                ImagesList.add(images);
                adapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    ArrayList<String> Images = new ArrayList<>(hashset);
    recyclerView = view.findViewById(R.id.frag3_fav_recyclerView);
    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    adapter = new FragThreeAdapter(Images, getActivity());
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(adapter);
    return view;
}
}

我的点击监听器

favoriteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                Toast.makeText(activity, "saved to favorite", Toast.LENGTH_SHORT).show();
                reference = FirebaseDatabase.getInstance().getReference().child("Favorite");
                String key = reference.push().getKey();
                reference.child(key).setValue(imagePaths.get(position));

我的适配器类

public class FragThreeAdapter extends RecyclerView.Adapter<FragThreeAdapter.myHolder> {

private List<String> list;
private Context context;

public FragThreeAdapter(List<String> list, Context context) {
    this.list = list;
    this.context = context;
}

@NonNull
@Override
public myHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    LayoutInflater inflater= LayoutInflater.from(context);
    View view= inflater.inflate(R.layout.list_item,parent,false);

    return new myHolder(view);
}

@Override
public void onBindViewHolder(@NonNull myHolder holder, int position) {

    Glide.with(context)
            .load(list.get(position))
            .into(holder.imageView);
    holder.imageView.setOnClickListener(new OnImageClickListner(position));
}

@Override
public int getItemCount() {
    return list.size();
}

public class myHolder extends RecyclerView.ViewHolder{

    ImageView imageView;
    public myHolder(View itemView) {
        super(itemView);

        imageView= itemView.findViewById(R.id.imageView_id);
    }
}

class OnImageClickListner implements View.OnClickListener{
    int position;
    public OnImageClickListner(int position){
        this.position= position;
    }
    @Override
    public void onClick(View v) {

        Intent intent= new Intent(context,Frag3_Favrit_FullActvity.class);
        intent.putExtra("array_position",position);
        intent.putStringArrayListExtra("list", (ArrayList<String>) list);
        context.startActivity(intent);
    }
}

}

1 个答案:

答案 0 :(得分:0)

您可以尝试这样制作:

public void onBindViewHolder(@NonNull myHolder holder, int position) {
    if (list.get(position) != null) {
        Glide.with(context)
            .load(list.get(position))
            .into(holder.imageView);
    } esle {
         holder.imageView.setImageResource(R.mipmap.some_default_cover);
    }

    holder.imageView.setOnClickListener(new OnImageClickListner(position));
}