【问题标题】:don't work onClick of view that is in recyclerView对 recyclerView 中的视图不起作用
【发布时间】:2019-03-30 15:25:20
【问题描述】:

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

在这种模式下,点击功能不起作用..

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

【问题讨论】:

    标签: java android android-recyclerview android-mediaplayer


    【解决方案1】:

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

    【讨论】:

    • 您应该在适配器构造函数中提供活动/片段上下文。像这样YourAdapter(Context context, int yourLayout, ArrayList&lt;Object&gt; yourList)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    相关资源
    最近更新 更多