点击下载源码
主要实现功能
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;
}
}