RecyclerView实现仿微信聊天界面

点击下载源码
主要实现功能
1.实现聊天界面文字、表情、图片、语音、视频
2.可以发送文字和语音功能
Activity中代码如下

public class ActivityChatList extends BaseActivity implements MyButton.AudioFinishRecorderCallBack, TextWatcher,
        View.OnFocusChangeListener {
    private RecyclerView lv_chart;
    private ChatListAdapter chatListAdapter;
    private List<ChatList> list = new ArrayList<>();
    private ChatList chatList;
    private ChatList.Builder builder = new ChatList.Builder();
    private MyButton id_recorder_button;
    private View animView;
    //判断语音和文字
    private boolean myMethod = true;
    private EditText et_text;
    private ImageView iv_image3;
    private TextView tv_fa_song;
    private View v_line;
    private FrameLayout fl_train;
    private boolean myTrain = true;
    private boolean myTrain1 = true;
    private FrameLayout fl_train1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_list);
        initView();
        initData();
        showRecycler();
    }

    @Override
    public void initView() {
        super.initView();
        lv_chart = (RecyclerView) findViewById(R.id.lv_chart);
        id_recorder_button = (MyButton) findViewById(R.id.id_recorder_button);
        id_recorder_button.setFinishRecorderCallBack(this);
        findViewById(R.id.iv_image1).setOnClickListener(this);
        findViewById(R.id.iv_image2).setOnClickListener(this);
        iv_image3 = (ImageView) findViewById(R.id.iv_image3);
        iv_image3.setOnClickListener(this);
        et_text = (EditText) findViewById(R.id.et_text);
        tv_fa_song = (TextView) findViewById(R.id.tv_fa_song);
        tv_fa_song.setOnClickListener(this);
        et_text.addTextChangedListener(this);
        v_line = findViewById(R.id.v_line);
        fl_train = (FrameLayout) findViewById(R.id.fl_train);
        et_text.setOnFocusChangeListener(this);
        disableShowInput();
        fl_train1 = (FrameLayout) findViewById(R.id.fl_train1);
    }

    @Override
    public void initData() {
        super.initData();
        for (int i = 0; i < 17; i++) {
            if (i == 0) {
                chatList = builder.Types("Receive").MultipleOptions("Text").Time("09:55:01").Content("广东省根深蒂固十点半").build();
            } else if (i == 1) {
                chatList = builder.Types("SendOut").MultipleOptions("Text").Time("7月08日 09:55:01").Content("时的gas的gas的高大上v阿萨德").build();
            } else if (i == 2) {
                chatList = builder.Types("Receive").MultipleOptions("Image").Time("7月08日 09:55:01").Images("http://www.ace.eiboran.net/Uploads/course/591173a220f39.jpg").build();
            } else if (i == 3) {
                chatList = builder.Types("SendOut").MultipleOptions("Image").Time("7月08日 09:55:01").Images("http://www.ace.eiboran.net/Uploads/course/591173a220f39.jpg").build();
            } else if (i == 4) {
                chatList = builder.Types("Receive").MultipleOptions("Text").Time("7月08日 09:55:01").Content("HAHASTEASDG").build();
            } else if (i == 5) {
                chatList = builder.Types("SendOut").MultipleOptions("Text").Time("7月08日 09:55:01").Content("你好").build();
            } else if (i == 6) {
                chatList = builder.Types("SendOut").MultipleOptions("Image").Time("09:55:01").Images("http://www.ace.eiboran.net/Uploads/course/591173a220f39.jpg").build();
            } else if (i == 7) {
                chatList = builder.Types("Receive").MultipleOptions("Text").Time("09:55:01").Content("GGDGAEG").build();
            } else if (i == 8) {
                chatList = builder.Types("SendOut").MultipleOptions("Image").Time("12:55:01").Images("http://www.ace.eiboran.net/Uploads/course/591173a220f39.jpg").build();
            } else if (i == 9) {
                chatList = builder.Types("Receive").MultipleOptions("Text").Time("19:55:01").Content("FASDFASDFASDFSDA").build();
            } else if (i == 10) {
                chatList = builder.Types("SendOut").MultipleOptions("Image").Time("19:55:01").Images("http://www.ace.eiboran.net/Uploads/course/591173a220f39.jpg").build();
            } else if (i == 11) {
                chatList = builder.Types("SendOut").MultipleOptions("Player").Time("19:55:01").Images("http://www.ace.eiboran.net/Uploads/course/591173a220f39.jpg").build();
            } else if (i == 12) {
                chatList = builder.Types("Receive").MultipleOptions("Player").Time("19:55:01").Images("http://www.ace.eiboran.net/Uploads/course/591173a220f39.jpg").build();
            } else if (i == 13) {
                chatList = builder.Types("SendOut").MultipleOptions("Player").Time("19:55:01").Images("http://www.ace.eiboran.net/Uploads/course/591173a220f39.jpg").build();
            } else if (i == 16) {
                chatList = builder.Types("SendOut").MultipleOptions("Player").Time("19:55:01").Images("http://www.ace.eiboran.net/Uploads/course/591173a220f39.jpg").build();
            } else {
                chatList = builder.Types("Receive").MultipleOptions("Player").Time("19:55:01").Images("http://www.ace.eiboran.net/Uploads/course/591173a220f39.jpg").build();
            }
            list.add(chatList);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //显示语音或文字输入框
            case R.id.iv_image1:
                if (myMethod){
                    id_recorder_button.setVisibility(View.INVISIBLE);
                    et_text.setVisibility(View.VISIBLE);
                    v_line.setVisibility(View.VISIBLE);
                    fl_train.setVisibility(View.GONE);
                    fl_train1.setVisibility(View.GONE);
                    show();
                    myTrain = true;
                    myTrain1 = true;
                    myMethod = false;
                } else {
                    show();
                    et_text.setVisibility(View.INVISIBLE);
                    v_line.setVisibility(View.INVISIBLE);
                    id_recorder_button.setVisibility(View.VISIBLE);
                    myMethod = true;
                }
                break;
            //显示表情输入框
            case R.id.iv_image2:
                if (myTrain){
                    hintKbTwo();
                    id_recorder_button.setVisibility(View.INVISIBLE);
                    et_text.setVisibility(View.VISIBLE);
                    v_line.setVisibility(View.VISIBLE);
                    fl_train1.setVisibility(View.GONE);
                    fl_train.setVisibility(View.VISIBLE);
                    myTrain1 = true;
                    myTrain = false;
                    myMethod = true;
                } else {
                    fl_train.setVisibility(View.GONE);
                    myTrain = true;
                }
                break;
            //显示录制视频和图片输入框
            case R.id.iv_image3:
                if (myTrain1){
                    hintKbTwo();
                    fl_train.setVisibility(View.GONE);
                    fl_train1.setVisibility(View.VISIBLE);
                    myTrain = true;
                    myMethod = true;
                    myTrain1 = false;
                } else {
                    fl_train1.setVisibility(View.GONE);
                    myTrain1 = true;
                }
                break;
            //发送消息按钮
            case R.id.tv_fa_song:
                if (et_text.getText().length() > 0) {
                    chatList = builder.Types("SendOut").MultipleOptions("Text").Time("09:55:01").Content(et_text.getText().toString()).build();
                    chatListAdapter.addData(chatList);
                    lv_chart.smoothScrollToPosition(list.size() - 1);
                    et_text.getText().clear();
                }
                break;
        }
    }

    /**
     * 隐藏显示键盘
     */
    private void show() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 点击事件隐藏键盘
     */
    public void disableShowInput() {
        if (android.os.Build.VERSION.SDK_INT <= 10) {
            et_text.setInputType(InputType.TYPE_NULL);
        } else {
            Class<EditText> cls = EditText.class;
            Method method;
            try {
                method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                method.setAccessible(true);
                method.invoke(et_text, false);
            } catch (Exception e) {
            }
        }
    }

    /**
     * 隐藏键盘
     */
    private void hintKbTwo() {
        et_text.clearFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive() && getCurrentFocus() != null) {
            if (getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    public void showRecycler() {
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        linearLayoutManager.setStackFromEnd(true); //关键
        lv_chart.setLayoutManager(linearLayoutManager);
        chatListAdapter = new ChatListAdapter(this, list, BR.Chat);
        lv_chart.setAdapter(chatListAdapter);
        lv_chart.smoothScrollToPosition(list.size() - 1);
        chatListAdapter.setItemClickListener(new ChatListAdapter.MyItemClickListener() {

            @Override
            public void onItemChatImage(View view, int position) {
                UIHelper.showMySetUpFragment(ActivityChatList.this);
            }

            @Override
            public void onItemChatPlayer(View view, int position) {
                Log.i("image", "222222");

            }

            @Override
            public void onItemChatHead(View view, int position) {
                Log.i("image", "111111");
            }

            @Override
            public void onItemChatVoice(View view, int position) {
                //声音播放动画
                if (animView != null) {
                    animView.setBackgroundResource(R.drawable.adj);
                    animView = null;
                }
                animView = view.findViewById(R.id.tv_item_send_txt);
                animView.setBackgroundResource(R.drawable.play_anim);
                AnimationDrawable animation = (AnimationDrawable) animView.getBackground();
                animation.start();
                // 播放录音
                MediaPlayerManager.playSound(list.get(position).getFilePath(), new MediaPlayer.OnCompletionListener() {
                    public void onCompletion(MediaPlayer mp) {
                        //播放完成后修改图片
                        animView.setBackgroundResource(R.drawable.adj);
                    }
                });
            }
        });
    }

    @Override
    public void onFinish(float seconds, String filePath) {
        chatList = builder.Types("SendOut").MultipleOptions("Voice").Time("19:55:01").Voice(seconds).FilePath(filePath).build();
        chatListAdapter.addData(chatList);
        lv_chart.smoothScrollToPosition(list.size() - 1);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    /**
     * 输入文字后的状态
     *
     * @param s
     */
    @Override
    public void afterTextChanged(Editable s) {
        if (et_text.getText().length() > 0) {
            iv_image3.setVisibility(View.GONE);
            tv_fa_song.setVisibility(View.VISIBLE);
        } else {
            iv_image3.setVisibility(View.VISIBLE);
            tv_fa_song.setVisibility(View.GONE);
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {

    }
}

activity中xml代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/lv_chart"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv_image1"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="7.5dp"
            android:background="@color/colorAccent" />

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <kjy.com.mqtt.widget.MyButton
                android:id="@+id/id_recorder_button"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="7.5dp"
                android:background="@drawable/btn_recorder_normal"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:text="@string/str_recorder_normal"
                android:textColor="#727272" />

            <EditText
                android:id="@+id/et_text"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="7.5dp"
                android:background="@null"
                android:textColor="#999999"
                android:textColorHint="#999999"
                android:textCursorDrawable="@null"
                android:visibility="gone" />

            <View
                android:id="@+id/v_line"
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:layout_below="@id/et_text"
                android:layout_marginLeft="15dp"
                android:background="@color/colorLine"
                android:visibility="gone" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="105dp"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/iv_image2"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="7.5dp"
                android:background="@color/colorAccent" />

            <ImageView
                android:id="@+id/iv_image3"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="7.5dp"
                android:layout_toRightOf="@id/iv_image2"
                android:background="@color/colorAccent" />

            <TextView
                android:id="@+id/tv_fa_song"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_marginLeft="60dp"
                android:layout_marginTop="7.5dp"
                android:background="@drawable/fa_song"
                android:gravity="center"
                android:text="发送"
                android:textColor="@color/white"
                android:textSize="14dp"
                android:visibility="gone" />
        </RelativeLayout>
    </LinearLayout>

    <FrameLayout
        android:id="@+id/fl_train"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:visibility="gone" />

    <FrameLayout
        android:id="@+id/fl_train1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:visibility="gone" />
</LinearLayout>

Adapter代码如下

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

    private MyItemClickListener mItemClickListener;
    private List<ChatList> list;
    private LayoutInflater inflater;
    private Context context;
    private int variableId;
    private ItemChatSendOutTextBinding itemChatSendOutTextBinding;
    private ItemChatSendOutImageBinding itemChatSendOutImageBinding;
    private ItemChatReceiveTextBinding itemChatReceiveTextBinding;
    private ItemChatReceiveImageBinding itemChatReceiveImageBinding;
    private ItemChatSendOutVoiceBinding itemChatSendOutVoiceBinding;
    private ItemChatReceiveVoiceBinding itemChatReceiveVoiceBinding;
    private ItemChatReceivePlayerBinding itemChatReceivePlayerBinding;
    private ItemChatSendPlayerBinding itemChatSendPlayerBinding;

    public ChatListAdapter(Context context, List<ChatList> list, int variableId) {
        this.inflater = LayoutInflater.from(context);
        this.list = list;
        this.context = context;
        this.variableId = variableId;
    }

    /**
     * 添加一条数据并刷新界面
     *
     * @param chatList
     */
    public void addData(ChatList chatList) {
        list.add(list.size(), chatList);
        notifyItemInserted(list.size());
    }

    /**
     * 根据不同的条目返回不同布局类型的方法
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        ChatList chatList = list.get(position);
        //发送
        if (chatList.getTypes() == ChatList.Type.SendOut) {
            if (chatList.getMultipleOptions() == ChatList.Type.Text) {
                //文字
                return Constant.SendOutText;
            } else if (chatList.getMultipleOptions() == ChatList.Type.Image) {
                //图片
                return Constant.SendOutImage;
            } else if (chatList.getMultipleOptions() == ChatList.Type.Voice) {
                //语音
                return Constant.SendOutVoice;
            } else if (chatList.getMultipleOptions() == ChatList.Type.Player) {
                //视频
                return Constant.SendOutPlayer;
            }
            //接收
        } else if (chatList.getTypes() == ChatList.Type.Receive) {
            if (chatList.getMultipleOptions() == ChatList.Type.Text) {
                //文字
                return Constant.ReceiveText;
            } else if (chatList.getMultipleOptions() == ChatList.Type.Image) {
                //图片
                return Constant.ReceiveImage;
            } else if (chatList.getMultipleOptions() == ChatList.Type.Voice) {
                //语音
                return Constant.ReceiveVoice;
            } else if (chatList.getMultipleOptions() == ChatList.Type.Player) {
                //视频
                return Constant.ReceivePlayer;
            }
        }
        return super.getItemViewType(position);
    }

    /**
     * 判断加载不同的item条目
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == Constant.SendOutText) {
            itemChatSendOutTextBinding = DataBindingUtil.inflate(inflater, R.layout.item_chat_send_out_text, parent, false);
            ChatListAdapter.ViewHolderSendOutText viewHolder = new ChatListAdapter.ViewHolderSendOutText(itemChatSendOutTextBinding.getRoot(), mItemClickListener);
            viewHolder.setBinding(itemChatSendOutTextBinding);
            return viewHolder;
        } else if (viewType == Constant.SendOutImage) {
            itemChatSendOutImageBinding = DataBindingUtil.inflate(inflater, R.layout.item_chat_send_out_image, parent, false);
            ChatListAdapter.ViewHolderSendOutImage viewHolder = new ChatListAdapter.ViewHolderSendOutImage(itemChatSendOutImageBinding.getRoot(), mItemClickListener);
            viewHolder.setBinding(itemChatSendOutImageBinding);
            return viewHolder;
        } else if (viewType == Constant.ReceiveText) {
            itemChatReceiveTextBinding = DataBindingUtil.inflate(inflater, R.layout.item_chat_receive_text, parent, false);
            ChatListAdapter.ViewHolderReceiveText viewHolder = new ChatListAdapter.ViewHolderReceiveText(itemChatReceiveTextBinding.getRoot(), mItemClickListener);
            viewHolder.setBinding(itemChatReceiveTextBinding);
            return viewHolder;
        } else if (viewType == Constant.ReceiveImage) {
            itemChatReceiveImageBinding = DataBindingUtil.inflate(inflater, R.layout.item_chat_receive_image, parent, false);
            ChatListAdapter.ViewHolderReceiveImage viewHolder = new ChatListAdapter.ViewHolderReceiveImage(itemChatReceiveImageBinding.getRoot(), mItemClickListener);
            viewHolder.setBinding(itemChatReceiveImageBinding);
            return viewHolder;
        } else if (viewType == Constant.SendOutVoice) {
            itemChatSendOutVoiceBinding = DataBindingUtil.inflate(inflater, R.layout.item_chat_send_out_voice, parent, false);
            ChatListAdapter.ViewHolderSendOutVoice viewHolder = new ChatListAdapter.ViewHolderSendOutVoice(itemChatSendOutVoiceBinding.getRoot(), mItemClickListener);
            viewHolder.setBinding(itemChatSendOutVoiceBinding);
            return viewHolder;
        } else if (viewType == Constant.ReceiveVoice) {
            itemChatReceiveVoiceBinding = DataBindingUtil.inflate(inflater, R.layout.item_chat_receive_voice, parent, false);
            ChatListAdapter.ViewHolderReceiveVoice viewHolder = new ChatListAdapter.ViewHolderReceiveVoice(itemChatReceiveVoiceBinding.getRoot(), mItemClickListener);
            viewHolder.setBinding(itemChatReceiveVoiceBinding);
            return viewHolder;
        } else if (viewType == Constant.ReceivePlayer) {
            itemChatReceivePlayerBinding = DataBindingUtil.inflate(inflater, R.layout.item_chat_receive_player, parent, false);
            ChatListAdapter.ViewHolderReceivePlayer viewHolder = new ChatListAdapter.ViewHolderReceivePlayer(itemChatReceivePlayerBinding.getRoot(), mItemClickListener);
            viewHolder.setBinding(itemChatReceivePlayerBinding);
            return viewHolder;
        } else if (viewType == Constant.SendOutPlayer) {
            itemChatSendPlayerBinding = DataBindingUtil.inflate(inflater, R.layout.item_chat_send_player, parent, false);
            ChatListAdapter.ViewHolderSendOutPlayer viewHolder = new ChatListAdapter.ViewHolderSendOutPlayer(itemChatSendPlayerBinding.getRoot(), mItemClickListener);
            viewHolder.setBinding(itemChatSendPlayerBinding);
            return viewHolder;
        }
        return null;
    }

    /**
     * 通过不同是item加载不同的数据
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolderSendOutText) {
            ((ViewHolderSendOutText) holder).getBinding().setVariable(variableId, list.get(position));
            ((ViewHolderSendOutText) holder).getBinding().executePendingBindings();
        } else if (holder instanceof ViewHolderSendOutImage) {
            Glide
                    .with(context)
                    .load(list.get(position).getImages())
                    //禁止缓存
                    .skipMemoryCache(true)
                    .error(R.mipmap.ic_launcher)
                    .into(((ViewHolderSendOutImage) holder).iv_send_image);
            ((ViewHolderSendOutImage) holder).getBinding().setVariable(variableId, list.get(position));
            ((ViewHolderSendOutImage) holder).getBinding().executePendingBindings();
        } else if (holder instanceof ViewHolderReceiveText) {
            ((ViewHolderReceiveText) holder).getBinding().setVariable(variableId, list.get(position));
            ((ViewHolderReceiveText) holder).getBinding().executePendingBindings();
        } else if (holder instanceof ViewHolderReceiveImage) {
            Glide
                    .with(context)
                    .load(list.get(position).getImages())
                    //禁止缓存
                    .skipMemoryCache(true)
                    .error(R.mipmap.ic_launcher)
                    .into(((ViewHolderReceiveImage) holder).iv_from_image);
            ((ViewHolderReceiveImage) holder).getBinding().setVariable(variableId, list.get(position));
            ((ViewHolderReceiveImage) holder).getBinding().executePendingBindings();
        } else if (holder instanceof ViewHolderSendOutVoice) {
            ((ViewHolderSendOutVoice) holder).getBinding().setVariable(variableId, list.get(position));
            ((ViewHolderSendOutVoice) holder).getBinding().executePendingBindings();
        } else if (holder instanceof ViewHolderReceiveVoice) {
            ((ViewHolderReceiveVoice) holder).getBinding().setVariable(variableId, list.get(position));
            ((ViewHolderReceiveVoice) holder).getBinding().executePendingBindings();
        } else if (holder instanceof ViewHolderReceivePlayer) {
            Glide
                    .with(context)
                    .load(list.get(position).getImages())
                    //禁止缓存
                    .skipMemoryCache(true)
                    .error(R.mipmap.ic_launcher)
                    .into(((ViewHolderReceivePlayer) holder).iv_from_image);
            ((ViewHolderReceivePlayer) holder).getBinding().setVariable(variableId, list.get(position));
            ((ViewHolderReceivePlayer) holder).getBinding().executePendingBindings();
        } else if (holder instanceof ViewHolderSendOutPlayer) {
            Glide
                    .with(context)
                    .load(list.get(position).getImages())
                    //禁止缓存
                    .skipMemoryCache(true)
                    .error(R.mipmap.ic_launcher)
                    .into(((ViewHolderSendOutPlayer) holder).iv_send_image);
            ((ViewHolderSendOutPlayer) holder).getBinding().setVariable(variableId, list.get(position));
            ((ViewHolderSendOutPlayer) holder).getBinding().executePendingBindings();
        }
    }

    /**
     * list总数
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * 发送文字
     */
    public static class ViewHolderSendOutText extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ChatListAdapter.MyItemClickListener myItemClickListener;
        private ViewDataBinding binding;

        public ViewHolderSendOutText(View itemView, ChatListAdapter.MyItemClickListener myItemClickListener) {
            super(itemView);
            this.myItemClickListener = myItemClickListener;
            itemView.findViewById(R.id.from_person).setOnClickListener(this);
        }

        /**
         * 设置点击事件
         *
         * @param v
         */
        @Override
        public void onClick(View v) {
            if (myItemClickListener != null) {
                switch (v.getId()) {
                    case R.id.from_person:
                        myItemClickListener.onItemChatHead(v, getPosition());
                        break;
                }
            }
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }
    }

    /**
     * 发送图片
     */
    public static class ViewHolderSendOutImage extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ChatListAdapter.MyItemClickListener myItemClickListener;
        private ViewDataBinding binding;
        private ImageView iv_send_image;

        public ViewHolderSendOutImage(View itemView, ChatListAdapter.MyItemClickListener myItemClickListener) {
            super(itemView);
            this.myItemClickListener = myItemClickListener;
            iv_send_image = (ImageView) itemView.findViewById(R.id.iv_send_image);
            itemView.findViewById(R.id.from_person).setOnClickListener(this);
            itemView.findViewById(R.id.iv_send_image).setOnClickListener(this);
        }

        /**
         * 设置点击事件
         *
         * @param v
         */
        @Override
        public void onClick(View v) {
            if (myItemClickListener != null) {
                switch (v.getId()) {
                    case R.id.from_person:
                        myItemClickListener.onItemChatHead(v, getPosition());
                        break;
                    case R.id.iv_send_image:
                        myItemClickListener.onItemChatImage(v, getPosition());
                        break;
                }
            }
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }
    }

    /**
     * 接收文字
     */
    public static class ViewHolderReceiveText extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ChatListAdapter.MyItemClickListener myItemClickListener;
        private ViewDataBinding binding;

        public ViewHolderReceiveText(View itemView, ChatListAdapter.MyItemClickListener myItemClickListener) {
            super(itemView);
            this.myItemClickListener = myItemClickListener;
            itemView.findViewById(R.id.from_person).setOnClickListener(this);
        }

        /**
         * 设置点击事件
         *
         * @param v
         */
        @Override
        public void onClick(View v) {
            if (myItemClickListener != null) {
                switch (v.getId()) {
                    case R.id.from_person:
                        myItemClickListener.onItemChatHead(v, getPosition());
                        break;
                }
            }
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }
    }

    /**
     * 接收图片
     */
    public static class ViewHolderReceiveImage extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ChatListAdapter.MyItemClickListener myItemClickListener;
        private ViewDataBinding binding;
        private ImageView iv_from_image;

        public ViewHolderReceiveImage(View itemView, ChatListAdapter.MyItemClickListener myItemClickListener) {
            super(itemView);
            this.myItemClickListener = myItemClickListener;
            iv_from_image = (ImageView) itemView.findViewById(R.id.iv_from_image);
            itemView.findViewById(R.id.from_person).setOnClickListener(this);
            itemView.findViewById(R.id.iv_from_image).setOnClickListener(this);
        }

        /**
         * 设置点击事件
         *
         * @param v
         */
        @Override
        public void onClick(View v) {
            if (myItemClickListener != null) {
                switch (v.getId()) {
                    case R.id.from_person:
                        myItemClickListener.onItemChatHead(v, getPosition());
                        break;
                    case R.id.iv_from_image:
                        myItemClickListener.onItemChatImage(v, getPosition());
                        break;
                }
            }
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }
    }

    /**
     * 接收语音
     */
    public static class ViewHolderReceiveVoice extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ChatListAdapter.MyItemClickListener myItemClickListener;
        private ViewDataBinding binding;

        public ViewHolderReceiveVoice(View itemView, ChatListAdapter.MyItemClickListener myItemClickListener) {
            super(itemView);
            this.myItemClickListener = myItemClickListener;
            itemView.findViewById(R.id.fl_voice).setOnClickListener(this);
            itemView.findViewById(R.id.from_person).setOnClickListener(this);
        }

        /**
         * 设置点击事件
         *
         * @param v
         */
        @Override
        public void onClick(View v) {
            if (myItemClickListener != null) {
                switch (v.getId()) {
                    case R.id.fl_voice:
                        myItemClickListener.onItemChatVoice(v, getPosition());
                        break;
                    case R.id.from_person:
                        myItemClickListener.onItemChatHead(v, getPosition());
                        break;
                }
            }
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }
    }

    /**
     * 发送语音
     */
    public static class ViewHolderSendOutVoice extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ChatListAdapter.MyItemClickListener myItemClickListener;
        private ViewDataBinding binding;

        public ViewHolderSendOutVoice(View itemView, ChatListAdapter.MyItemClickListener myItemClickListener) {
            super(itemView);
            this.myItemClickListener = myItemClickListener;
            itemView.findViewById(R.id.fl_voice).setOnClickListener(this);
            itemView.findViewById(R.id.from_person).setOnClickListener(this);
        }

        /**
         * 设置点击事件
         *
         * @param v
         */
        @Override
        public void onClick(View v) {
            if (myItemClickListener != null) {
                switch (v.getId()) {
                    case R.id.fl_voice:
                        myItemClickListener.onItemChatVoice(v, getPosition());
                        break;
                    case R.id.from_person:
                        myItemClickListener.onItemChatHead(v, getPosition());
                        break;
                }
            }
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }
    }

    /**
     * 接收视频
     */
    public static class ViewHolderReceivePlayer extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ChatListAdapter.MyItemClickListener myItemClickListener;
        private ViewDataBinding binding;
        private ImageView iv_from_image;

        public ViewHolderReceivePlayer(View itemView, ChatListAdapter.MyItemClickListener myItemClickListener) {
            super(itemView);
            this.myItemClickListener = myItemClickListener;
            iv_from_image = (ImageView) itemView.findViewById(R.id.iv_from_image);
            itemView.findViewById(R.id.iv_from_image).setOnClickListener(this);
            itemView.findViewById(R.id.from_person).setOnClickListener(this);
        }

        /**
         * 设置点击事件
         *
         * @param v
         */
        @Override
        public void onClick(View v) {
            if (myItemClickListener != null) {
                switch (v.getId()) {
                    case R.id.iv_from_image:
                        myItemClickListener.onItemChatPlayer(v, getPosition());
                        break;
                    case R.id.from_person:
                        myItemClickListener.onItemChatHead(v, getPosition());
                        break;
                }
            }
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }
    }

    /**
     * 发送视频
     */
    public static class ViewHolderSendOutPlayer extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ChatListAdapter.MyItemClickListener myItemClickListener;
        private ViewDataBinding binding;
        private ImageView iv_send_image;

        public ViewHolderSendOutPlayer(View itemView, ChatListAdapter.MyItemClickListener myItemClickListener) {
            super(itemView);
            this.myItemClickListener = myItemClickListener;
            iv_send_image = (ImageView) itemView.findViewById(R.id.iv_send_image);
            itemView.findViewById(R.id.iv_send_image).setOnClickListener(this);
            itemView.findViewById(R.id.from_person).setOnClickListener(this);
        }

        /**
         * 设置点击事件
         *
         * @param v
         */
        @Override
        public void onClick(View v) {
            if (myItemClickListener != null) {
                switch (v.getId()) {
                    case R.id.iv_send_image:
                        myItemClickListener.onItemChatPlayer(v, getPosition());
                        break;
                    case R.id.from_person:
                        myItemClickListener.onItemChatHead(v, getPosition());
                        break;
                }
            }
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }
    }

    /**
     * 定义接口实现点击事件的回调
     */
    public interface MyItemClickListener {
        void onItemChatHead(View view, int position);

        void onItemChatVoice(View view, int position);

        void onItemChatImage(View view, int position);

        void onItemChatPlayer(View view, int position);
    }

    public void setItemClickListener(ChatListAdapter.MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容