简体   繁体   English

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

[英]don't work onClick of view that is in recyclerView

I have a recyclerView for chat! 我有一个recyclerView聊天! i want record voice and play it in chat ... for this i have a layout for play and pause voice ...i need voice position in recyclerview to play it. 我想录制声音并在聊天中播放...为此,我有一个播放和暂停声音的布局...我需要在recyclerview中放置声音才能播放它。

I have tried two methods: 1- msgRecyclerView.addOnItemTouchListener but in this way i can't control play and pause voice with one button 2- onClick for play button and onClick for pause button in this way i don't have voice position in recyclerView 我尝试了两种方法:1- msgRecyclerView.addOnItemTouchListener,但是用这种方式我无法通过一个按钮控制播放和暂停声音2-这样播放按钮的onClick和暂停按钮的onClick我在recyclerView中没有声音位置

    pos=msgRecyclerView.getChildLayoutPosition(view);

stops application 停止申请

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();
    }

In this mode, the click function does not work.. 在这种模式下,单击功能不起作用。

Please help me to play and stop voice in chat 请帮助我播放和停止聊天中的声音

You can try onclick method in your adapter. 您可以在适配器中尝试onclick方法。 Like this; 像这样;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM