0

RecyclerView项目中使用。卡片视图使用 Imageview、ImageButton、TextView。当用户单击 ImageView 存储在 objectbox 数据库中的图像链接时。两个图像按钮显示喜欢和不喜欢。当用户单击喜欢或不喜欢的图像按钮时,存储喜欢和不喜欢的数据库。

如何通过不同的点击监听器在数据库中存储图像和喜欢和不同。

public class ThumbnailAdapter extends RecyclerView.Adapter<ThumbnailAdapter.MyViewHolder>{

    private Context mContext;
    private List<Thumbnail> albumList;
    private List<Giphy> giphyList;

    private BoxStore boxStore;
    private Box<Giphy> box;

    public class MyViewHolder extends RecyclerView.ViewHolder {
       // public TextView title, count;

        @BindView(R.id.imgThumbnail)
        public ImageView imageView;

        @BindView(R.id.imgButtonThumbUp)
        public ImageButton imageButtonUP;

        @BindView(R.id.imgButtonThumbDown)
        public ImageButton imageButtonDown;

        @BindView(R.id.tvThumbUpCount)
        public TextView tvUpCount;

        @BindView(R.id.tvThumbDownCount)
        public TextView tvCountDown;

        int countUP = 0;
        int countDown = 0;

        String url;
        int id = 0;
        int thumbUp,thumbDown;

        public MyViewHolder(View view) {
            super(view);
            mContext = view.getContext();
            //imageView = (ImageView) view.findViewById(R.id.imgThumbnail);

            ButterKnife.bind(this,view);




            int idCount = id++;

            giphyList = new ArrayList<>();


            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   // String Title = title.getText().toString();

                    Thumbnail list = albumList.get(getAdapterPosition());

                    url = list.getVideoUrl();
                    giphyList.add(new Giphy(0,url,thumbUp,thumbDown));
                    Intent intent = new Intent(mContext,ExoPlayer.class);
                    intent.putExtra("url",list.getVideoUrl());


                    mContext.startActivity(intent);


                }
            });

            imageButtonUP.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                   thumbUp = countUP++;

                    //box.put(countUP);

                    tvUpCount.setText(""+countUP);
                }
            });

            imageButtonDown.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    thumbDown = countDown++;

                   // box.put(countDown);

                    tvCountDown.setText(""+countDown);
                }
            });


            addData(new Giphy(0,url,thumbUp,thumbDown));


        }



    }

    public ThumbnailAdapter(Context mContext, List<Thumbnail> albumList) {
        this.mContext = mContext;
        this.albumList = albumList;
//        this.giphyList = giphyList;


        boxStore = ((ObjectBox)mContext.getApplicationContext()).getBoxStore();
        box = boxStore.boxFor(Giphy.class);

    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_list_item_album, parent, false);

        return new MyViewHolder(itemView);
    }

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


        Thumbnail album = albumList.get(position);



        Glide.with(mContext)
                .asGif()
                .load(album.getGif())
                .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))


                .into(holder.imageView);


    }

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

   public void addData(Giphy giphy){
        box.put(giphy);
    }
4

1 回答 1

1

在 onBindViewHolder 中,将位置设置为 ImageButton 的标记:

holder.imageButtonUp.setTag(position);
holder.imageButtonDown.setTag(position);

然后在 ImageButton 的 onClickListener 中,您可以获得位置:

int position = (Integer)view.getTag();
Thumbnail album = albumList.get(position);

然后做任何你想做的事。

但是由于您使用的是 Glide,并且 Glide 使用 ImageView 的标签,所以您应该通知 Glide 使用另一个标签。看到这个:

使用 Glide 时出现错误“您不能在 Glide 的目标视图上调用 setTag()”

于 2018-09-21T09:56:08.443 回答