不在recyclerView中的onClick视图的视图上工作

时间:2019-03-30 15:25:21

标签: java android android-recyclerview android-mediaplayer

我有一个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();
    }

在此模式下,单击功能不起作用。

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

1 个答案:

答案 0 :(得分:0)

您可以在适配器中尝试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
        }
    });
}