如何使用多个ViewHolder处理RecyclerView Adapter中的ClickListener来播放音频

时间:2017-10-23 13:00:03

标签: android android-recyclerview onclicklistener recycler-adapter

我想在RecyclerView内播放特定的音频,我使用多个ViewHolder类来处理TextMessage,图像和音频,但ClickListener在音频播放器中无法正常工作:当我点击在第二个位置音频比播放零或任何其他位置音频。

适配器类代码:

public class MessageChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ChatMessage> mChatList;
    public static final int SENDER = 0;
    public static final int RECIPIENT = 1;
    public static final int RIGHT_MSG_IMG = 2;
    public static final int LEFT_MSG_IMG = 3;
    public static final int RIGHT_AUDIO_MESSAGE=4;
    public static final int LEFT_AUDIO_MESSAGE=5;

    public static int sCorner = 40;
    public static int sMargin = 5;
    public static int sBorder = 10;
    public static String sRightColor = "#E2FFC7";
    public static String sLeftColor = "#fff";

    Context context;

    private ClickListenerChatFirebase mClickListenerChatFirebase;

    private String myChatID;
    private String online_offline_status;

    public MessageChatAdapter(Context mContext,List<ChatMessage> listOfFireChats, String on_offLine,
                              ClickListenerChatFirebase mClickListenerChatFirebase) {
        mChatList = listOfFireChats;
        this.online_offline_status = on_offLine;
        this.mClickListenerChatFirebase = mClickListenerChatFirebase;
        this.context=mContext;
    }

    public String getMyChatID() {
        return myChatID;
    }

    public void setMyChatID(String myChatID) {
        this.myChatID = myChatID;
    }

    @Override
    public int getItemViewType(int position) {
        int viewType = 1000;
        ChatMessage chatMessage = mChatList.get(position);
        if (chatMessage.getSender().equalsIgnoreCase(myChatID)) {
            if (chatMessage.getMessage() != null) {
                viewType = 0;
            } else if (chatMessage.getMessage() == null && chatMessage.getSenderImage() != null) {
                viewType = 2;
            }else if (chatMessage.getMessage() == null && chatMessage.getSenderImage() == null
                    &&chatMessage.getAudioMessageModel().getAudio_sendPath()!=null){
                viewType=4;
            }
        } else {
            if (chatMessage.getMessage() != null) {
                viewType = 1;
            } else if (chatMessage.getMessage() == null && chatMessage.getSenderImage() != null) {
                viewType = 3;
            }else if (chatMessage.getMessage()==null&&chatMessage.getSenderImage()==null
                    &&chatMessage.getAudioMessageModel().getAudio_sendPath()!=null){
                viewType=5;
            }
        }
        switch (viewType) {
            case SENDER:
                return SENDER;
            case RECIPIENT:
                return RECIPIENT;
            case LEFT_MSG_IMG:
                return LEFT_MSG_IMG;
            case RIGHT_MSG_IMG:
                return RIGHT_MSG_IMG;
            case RIGHT_AUDIO_MESSAGE:
                return RIGHT_AUDIO_MESSAGE;
            case LEFT_AUDIO_MESSAGE:
                return LEFT_AUDIO_MESSAGE;
            default:
                return SENDER;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

        switch (viewType) {
            case SENDER:
                View viewSender = inflater.inflate(R.layout.layout_sender_message, viewGroup, false);
                viewHolder = new ViewHolderSender(viewSender);
                break;
            case RECIPIENT:
                View viewRecipient = inflater.inflate(R.layout.layout_recipient_message, viewGroup, false);
                viewHolder = new ViewHolderRecipient(viewRecipient);
                break;

            case RIGHT_MSG_IMG:
                View right_message = inflater.inflate(R.layout.item_message_right_img, viewGroup, false);
                viewHolder = new ViewHolderSenderImage(right_message);
                break;

            case LEFT_MSG_IMG:
                View leftMessage = inflater.inflate(R.layout.item_message_left_img, viewGroup, false);
                viewHolder = new ViewHolderReceipientImage(leftMessage);
                break;
            case RIGHT_AUDIO_MESSAGE:
                View rightAudioMessage=inflater.inflate(R.layout.audio_message_right,viewGroup,false);
                viewHolder = new ViewHolderSenderAudio(rightAudioMessage);
                break;
            case LEFT_AUDIO_MESSAGE:
                View leftAudioMessage=inflater.inflate(R.layout.audio_message_left,viewGroup,false);
                viewHolder = new ViewHolderRecipientAudio(leftAudioMessage);
                break;
            default:
                View viewSenderDefault = inflater.inflate(R.layout.layout_sender_message, viewGroup, false);
                viewHolder = new ViewHolderSender(viewSenderDefault);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        switch (getItemViewType(position)) {
            case SENDER:
                ViewHolderSender viewHolderSender = (ViewHolderSender) viewHolder;
                configureSenderView(viewHolderSender, position);
                break;
            case RECIPIENT:
                ViewHolderRecipient viewHolderRecipient = (ViewHolderRecipient) viewHolder;
                configureRecipientView(viewHolderRecipient, position);
                break;
            case LEFT_MSG_IMG:
                ViewHolderReceipientImage viewHolderReceipientImage = (ViewHolderReceipientImage) viewHolder;
                configureReceipientImagaeView(viewHolderReceipientImage, position);
                break;
            case RIGHT_MSG_IMG:
                ViewHolderSenderImage viewHolderSenderImage = (ViewHolderSenderImage) viewHolder;
                configureSenderImagaeView(viewHolderSenderImage, position);
                break;
            case RIGHT_AUDIO_MESSAGE:
                ViewHolderSenderAudio viewHolderSenderAudio=(ViewHolderSenderAudio)viewHolder;
                configureSenderAudioView(viewHolderSenderAudio,position);
                break;
            case LEFT_AUDIO_MESSAGE:
                ViewHolderRecipientAudio viewHolderRecipientAudio=(ViewHolderRecipientAudio)viewHolder;
                configureRecipientAudioView(viewHolderRecipientAudio,position);
            default:
                break;
        }
    }

    private void configureSenderView(ViewHolderSender viewHolderSender, int position) {

        ChatMessage senderFireMessage = mChatList.get(position);
        viewHolderSender.getSenderMessageTextView().setText(senderFireMessage.getMessage());
        viewHolderSender.getMsgSenderTime().setText(converttime(senderFireMessage.getTimeStamp()));
    }

    private void configureRecipientView(ViewHolderRecipient viewHolderRecipient, int position) {
        ChatMessage recipientFireMessage = mChatList.get(position);
        viewHolderRecipient.getRecipientMessageTextView().setText(recipientFireMessage.getMessage());
        viewHolderRecipient.getTime_text_recip().setText(converttime(recipientFireMessage.getTimeStamp()));
    }

    private void configureSenderImagaeView(ViewHolderSenderImage viewHolderSenderImage, int position) {
        ChatMessage senderImage = mChatList.get(position);
        viewHolderSenderImage.setIvChatPhoto(senderImage.getSenderImage());
        String a=(String)converttime(senderImage.getTimeStamp());
        viewHolderSenderImage.getTimeForImageSend().setText(a);
    }

    private void configureReceipientImagaeView(ViewHolderReceipientImage viewHolderReceipientImage, int position) {
        ChatMessage recipientImage = mChatList.get(position);
        viewHolderReceipientImage.setIvChatPhoto(recipientImage.getRecipient_file_url());
        String b= (String) converttime(recipientImage.getTimeStamp());
        //String a=recipientImage.getTimeStamp();
        viewHolderReceipientImage.getTimestampRecipient().setText(b);
    }

    private void configureSenderAudioView(final ViewHolderSenderAudio viewHolderSenderAudio,final int position){
                ChatMessage audioSender = mChatList.get(position);
                viewHolderSenderAudio.setAudio(audioSender.getAudioMessageModel().getAudio_sendPath());
                viewHolderSenderAudio.getAudioSendTime().setText(converttime(audioSender.getTimeStamp()));
    }

    private void configureRecipientAudioView(final ViewHolderRecipientAudio viewHolderRecipientAudio,final int position){
        ChatMessage audioRecipient = mChatList.get(position);
        viewHolderRecipientAudio.setAudio(audioRecipient.getAudioMessageModel().getDownload_url_file());
        viewHolderRecipientAudio.getAudioMsgTime().setText(converttime(audioRecipient.getTimeStamp()));
    }

    @Override
    public int getItemCount() {
        return mChatList.size();
    }

    public void refillAdapter(ChatMessage newFireChatMessage) {

        /*add new message chat to list*/
        mChatList.add(newFireChatMessage);

        /*refresh view*/
        notifyItemChanged(getItemCount() - 1);
    }

    public void cleanUp() {
        mChatList.clear();
    }

    /*==============ViewHolder===========*/
    /*ViewHolder for Sender*/
    public class ViewHolderSender extends RecyclerView.ViewHolder {

        private TextView mSenderMessageTextView;
        private TextView msgSenderTime;

        public ViewHolderSender(View itemView) {
            super(itemView);
            mSenderMessageTextView = (TextView) itemView.findViewById(R.id.text_view_sender_message);
            msgSenderTime = (TextView) itemView.findViewById(R.id.time_text);

        }

        public TextView getSenderMessageTextView() {
            return mSenderMessageTextView;
        }

        public TextView getMsgSenderTime() {
            return msgSenderTime;
        }


    }

    /*ViewHolder for Recipient*/
    public class ViewHolderRecipient extends RecyclerView.ViewHolder {

        private TextView mRecipientMessageTextView;
        private TextView time_text_recip;

        public ViewHolderRecipient(View itemView) {
            super(itemView);
            mRecipientMessageTextView = (TextView) itemView.findViewById(R.id.text_view_recipient_message);
            time_text_recip = (TextView) itemView.findViewById(R.id.time_text_recip);
        }

        public TextView getRecipientMessageTextView() {
            return mRecipientMessageTextView;
        }

        public TextView getTime_text_recip() {
            return time_text_recip;
        }


    }

    public class ViewHolderReceipientImage extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView ivChatPhoto;
        private TextView timestampRecipient;

        public ViewHolderReceipientImage(View itemView) {
            super(itemView);
            ivChatPhoto = (ImageView) itemView.findViewById(R.id.img_chat);
            timestampRecipient = (TextView) itemView.findViewById(R.id.timestampRecipient);
        }

        public void setIvChatPhoto(String url) {
            if (ivChatPhoto == null) return;
            Glide.with(ivChatPhoto.getContext()).load(url)
                    .bitmapTransform(new RoundedCornersTransformation( context,sCorner, sMargin))
                    .placeholder(R.drawable.place)
                    .into(ivChatPhoto);
            ivChatPhoto.setOnClickListener(this);
        }

        public TextView getTimestampRecipient() {
            return timestampRecipient;
        }

        @Override
        public void onClick(View view) {
            int position = getAdapterPosition();
            ChatMessage model = mChatList.get(position);
            mClickListenerChatFirebase.clickImageChat(view, position, null, model.getSenderImage(), model.getRecipient_file_url());
        }
    }

    public class ViewHolderSenderImage extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView ivChatPhoto;
        private TextView timeForImageSend;

        public ViewHolderSenderImage(View itemView) {
            super(itemView);
            ivChatPhoto = (ImageView) itemView.findViewById(R.id.img_chat);
            timeForImageSend = (TextView) itemView.findViewById(R.id.timestampSender);
        }

        public void setIvChatPhoto(String url) {
            if (ivChatPhoto == null) return;
            Glide.with(ivChatPhoto.getContext()).load(url)
                    .placeholder(R.drawable.place)
                    .fitCenter()
                    .bitmapTransform(new RoundedCornersTransformation( context,sCorner, sMargin,sRightColor,sBorder))
                    .into(ivChatPhoto);
            ivChatPhoto.setOnClickListener(this);
        }

        public TextView getTimeForImageSend() {
            return timeForImageSend;
        }


        @Override
        public void onClick(View view) {
            int position = getAdapterPosition();
            ChatMessage model = mChatList.get(position);
            mClickListenerChatFirebase.clickImageChat(view, position, null, model.getSenderImage(), model.getRecipient_file_url());
        }
    }

    public class ViewHolderSenderAudio extends RecyclerView.ViewHolder implements View.OnClickListener{

        private View mPlayMedia;
        private View mPauseMedia;
        private SeekBar mMediaSeekBar;
        private TextView mRunTime;
        private TextView mTotalTime;
        private TextView audioSendTime;

        public ViewHolderSenderAudio(View itemView) {
            super(itemView);

            mPlayMedia = itemView.findViewById(R.id.play);
            mPauseMedia = itemView.findViewById(R.id.pause);
            mMediaSeekBar = (SeekBar)itemView. findViewById(R.id.media_seekbar);
            mRunTime = (TextView) itemView.findViewById(R.id.run_time);
            mTotalTime = (TextView)itemView. findViewById(R.id.total_time);
            audioSendTime=(TextView)itemView.findViewById(R.id.recipient_audio_time);

            mPlayMedia.setOnClickListener(this);
            mPauseMedia.setOnClickListener(this);

        }

        private TextView getAudioSendTime(){
            return audioSendTime;
        }

        void setAudio(String uri){

            Uri inUri=Uri.parse(uri);


            if (inUri!=null){
            AudioWife.getInstance()
                    .init(context, inUri);


           /* AudioWife.getInstance().addOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {

                    //Toast.makeText(context, "Completed", Toast.LENGTH_SHORT).show();
                }
            });

            AudioWife.getInstance().addOnPlayClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //Toast.makeText(context, "Play", Toast.LENGTH_SHORT).show();
                    // get-set-go. Lets dance.
                }
            });

            AudioWife.getInstance().addOnPauseClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //Toast.makeText(context, "Pause", Toast.LENGTH_SHORT).show();
                    // Your on audio pause stuff.
                }
            });*/

            }
        }

        @Override
        public void onClick(View view) {
            int position = getAdapterPosition();
            mClickListenerChatFirebase.playAudioClick(view, position,mPlayMedia,mPauseMedia,
                    mMediaSeekBar,mTotalTime,mRunTime);
        }
    }

    public class ViewHolderRecipientAudio extends RecyclerView.ViewHolder {

        private View mPlayMedia;
        private View mPauseMedia;
        private SeekBar mMediaSeekBar;
        private TextView mRunTime;
        private TextView mTotalTime;
        private TextView audioMsgTime;

        public ViewHolderRecipientAudio(View itemView) {
            super(itemView);

            mPlayMedia = itemView.findViewById(R.id.play);
            mPauseMedia = itemView.findViewById(R.id.pause);
            mMediaSeekBar = (SeekBar)itemView. findViewById(R.id.media_seekbar);
            mRunTime = (TextView) itemView.findViewById(R.id.run_time);
            mTotalTime = (TextView)itemView. findViewById(R.id.total_time);
            audioMsgTime=(TextView)itemView.findViewById(R.id.recipient_audio_time);
        }

        public void setAudio(String uri){

            Uri inUri=Uri.parse(uri);

            if (inUri!=null){
                AudioWife.getInstance()
                        .init(context, inUri)
                        .setPlayView(mPlayMedia)
                        .setPauseView(mPauseMedia)
                        .setSeekBar(mMediaSeekBar)
                        .setRuntimeView(mRunTime)
                        .setTotalTimeView(mTotalTime);
            AudioWife.getInstance().addOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {

                    //Toast.makeText(context, "Completed", Toast.LENGTH_SHORT).show();
                }
            });

            AudioWife.getInstance().addOnPlayClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //Toast.makeText(context, "Play", Toast.LENGTH_SHORT).show();
                    // get-set-go. Lets dance.
                }
            });

            AudioWife.getInstance().addOnPauseClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //Toast.makeText(context, "Pause", Toast.LENGTH_SHORT).show();
                    // Your on audio pause stuff.
                }
            });

            }
        }

        private TextView getAudioMsgTime(){
            return audioMsgTime;
        }
    }

    public CharSequence converttime(String mileSecond) {

        long a = 0;
        long b = 0;
        long c = 0;

        try {
            a = Long.parseLong(mileSecond.trim());
            b = System.currentTimeMillis();
            c = DateUtils.SECOND_IN_MILLIS;

        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return DateUtils.getRelativeTimeSpanString(a, b, c);
    }

}

监听

public interface ClickListenerChatFirebase {

    void clickImageChat(View view, int position, String nameUser, String urlPhotoUser, String urlPhotoClick);

    void playAudioClick(View view, int position, View playAudioTv, View pauseAudioTv, SeekBar seekBar,
                        TextView mRunTime,TextView mTotalTime);


    void clickImageMapChat(View view, int position, String latitude, String longitude);

}

主要活动

@Override
    public void playAudioClick(View view, int position, View playAudioTv, View pauseAudioTv,
                               SeekBar mMediaSeekBar,TextView totalTime,TextView rumTime) {

        AudioWife.getInstance()
                .setPlayView(playAudioTv)
                .setPauseView(pauseAudioTv)
                .setSeekBar(mMediaSeekBar)
                .setRuntimeView(rumTime)
                .setTotalTimeView(totalTime);
    }

0 个答案:

没有答案