不要在 recyclerView 中的视图点击上工作

don't work onClick of view that is in recyclerView

我有一个聊天的recyclerView!我想录制语音并在聊天中播放... 为此,我有一个播放和暂停语音的布局...我需要在 recyclerview 中播放语音位置。

我试过两种方法: 1- msgRecyclerView.addOnItemTouchListener 但是这样我无法通过一个按钮控制播放和暂停语音 2- onClick 播放按钮和 onClick 暂停按钮 这样我在recyclerView

中没有语音位置
    pos=msgRecyclerView.getChildLayoutPosition(view);

停止应用程序

msgRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getApplicationContext(), msgRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {


                    List<Messages> messages = new DB_Helper(getApplicationContext()).getListOfMessages("MsgPos = " + position + " and DriverID = " + 2 + " and OperatorID = " + id);
                    String locPath = messages.get(0).getLocalPath();
                    int msgType = messages.get(0).getMsgType();
                    if (msgType == 1) {
                        Intent intent = new Intent(getApplicationContext(), ImageActivity.class);
                        intent.putExtra("picture", messages.get(0).getImage());
                        startActivity(intent);


                    } else if (msgType == 2) {
                        //video
                        //String VideoUri = String.valueOf(d.getDuration());
                        Intent intent = new Intent(getApplicationContext(), VideoActivity.class);
                        intent.putExtra("Video", locPath);
                        startActivity(intent);

                    } else if (msgType == 3) {

                        pos=position;


                }

                @Override
                public void onLongItemClick(View view, int position) {
                    // do whatever
                }
            })
    );




public void playVoice(View view)
    {
        if(mediaPlayer!=null)
            mediaPlayer.stop();
        List<Messages> messages = new DB_Helper(getApplicationContext()).getListOfMessages("MsgPos = " + pos + " and DriverID = " + 2 + " and OperatorID = " + id);
        mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(messages.get(0).getLocalPath()));
        mediaPlayer.start();
    }



 public void pauseVoice(View view)
    {
     //   pos=msgRecyclerView.getChildLayoutPosition(view);
        List<Messages> messages = new DB_Helper(getApplicationContext()).getListOfMessages("MsgPos = " + pos + " and DriverID = " + 2 + " and OperatorID = " + id);
        mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(messages.get(0).getLocalPath()));
        mediaPlayer.stop();
    }

此模式下点击功能无效..

请帮我播放和停止聊天中的语音

您可以尝试在您的适配器中使用 onclick 方法。像这样;

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    holder.myView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //your code
        }
    });
}