我的弹出菜单在适配器中获取相同的数据

时间:2018-04-18 08:57:00

标签: android adapter popupmenu

当我点击每个视频中的弹出菜单时,我想得到不同的数据,如下图所示,但是当我在不同的视频上点击菜单时,我的代码获得相同的数据,在这种情况下,当我在不同的视频中点击菜单预览时将播放相同的视频,我的代码会出现什么问题?

这是截图:

enter image description here

Recycler Adapter

public class MyCollectionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
static List<MyCollection> data;
private Context mContext;
private MyCollection collection = null;
private ProgressDialog progressDialog;

public MyCollectionAdapter(Context context, List<MyCollection> data) {
    this.data = data;
    this.mContext = context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.item_collection_video, parent, false);
    return new VH(v);
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
    collection = data.get(position);
    final VH vh = (VH) holder;

    vh.tvArtist.setText(collection.alias);
    vh.tvJudul.setText(collection.judul);
    vh.tvActive.setText(collection.sub_end);

    RequestOptions requestOptions = new RequestOptions();
    requestOptions.placeholder(R.drawable.background);
    Glide.with(vh.ivVideo.getContext())
            .load(collection.thumb_pic)
            .apply(requestOptions)
            .into(vh.ivVideo);

    vh.menu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popUpMenu(((VH) holder).menu);
        }
    });
}

private void popUpMenu(View view) {
    PopupMenu popupMenu = new PopupMenu(view.getContext(), view);
    MenuInflater inflater = popupMenu.getMenuInflater();
    inflater.inflate(R.menu.collection_menu, popupMenu.getMenu());
    popupMenu.setOnMenuItemClickListener(new MyMenuItemClickListener());
    popupMenu.show();
}

@Override
public int getItemCount() {
    return (data == null) ? 0 : data.size();
}

public class VH extends RecyclerView.ViewHolder {
    TextView tvArtist;
    TextView tvJudul;
    TextView tvActive;
    ImageView ivVideo, menu;
    MediaController mediaController;
    VideoView vvPreview;
    CardView cardView;

    public VH(View itemView) {
        super(itemView);
        tvArtist = itemView.findViewById(R.id.tv_artist_collection);
        tvJudul = itemView.findViewById(R.id.tv_title_collection);
        tvActive = itemView.findViewById(R.id.tv_date);
        ivVideo = itemView.findViewById(R.id.img_collection);
        cardView = itemView.findViewById(R.id.card_view);
        menu = itemView.findViewById(R.id.collection_menu);
        vvPreview = itemView.findViewById(R.id.vv_preview_collection);
        mediaController = new MediaController(itemView.getContext());
        mediaController.setAnchorView(mediaController);

    }
}

private class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {
    public MyMenuItemClickListener() {
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.setVideoTone:
                progressDialog = new ProgressDialog(mContext);
                progressDialog.setMessage("Set to video tone ...");
                progressDialog.setCancelable(false);
                progressDialog.show();
                serAsVideoTone();
                return true;
            case R.id.preview:
                Intent intent = new 
                Intent(mContext,PreviewVideoCollection.class);

                intent.putExtra("urlPreview", collection.source_content); // source_content contain same data (it should contain different data)
                mContext.startActivity(intent);
                return true;
            default:
        }
        return false;
    }
}

private void serAsVideoTone() {
    Call<APIResponse<List<SetVideoTone>>> call = ServicesFactory.getService(mContext).getVideoTone(collection.caller_id, collection.content_id);
    call.enqueue(new Callback<APIResponse<List<SetVideoTone>>>() {
        @Override
        public void onResponse(Call<APIResponse<List<SetVideoTone>>> call, Response<APIResponse<List<SetVideoTone>>> response) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    OwnVideoManager.getCollectionUri(mContext);
                    progressDialog.dismiss();
                    ((Activity) mContext).finish();
                }
            }, 2000);
        }

        @Override
        public void onFailure(Call<APIResponse<List<SetVideoTone>>> call, Throwable t) {
            Toast.makeText(mContext, "Setup Video Tone Failed", Toast.LENGTH_SHORT).show();
        }
    });
  }
}

1 个答案:

答案 0 :(得分:0)

在点击监听器中移动行collection = data.get(position);,如下所示。

vh.menu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            collection = data.get(vh.getAdapterPosition());
            popUpMenu(((VH) holder).menu);
        }
    });