package com.example.myuitest.chat;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.myuitest.R;
import com.example.myuitest.chat.frame.ContentModel;
import com.example.myuitest.chat.msgtype.SimpleImage;
import com.example.myuitest.chat.msgtype.SimpleImageViewBinder;
import com.example.myuitest.chat.msgtype.SimpleText;
import com.example.myuitest.chat.msgtype.SimpleTextViewBinder;
import com.example.myuitest.chat.msgtype.TimeBinder;
import com.example.myuitest.chat.msgtype.TimeSimple;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import me.drakeet.multitype.Linker;
import me.drakeet.multitype.MultiTypeAdapter;
import static me.drakeet.multitype.MultiTypeAsserts.assertAllRegistered;
import static me.drakeet.multitype.MultiTypeAsserts.assertHasTheSameAdapter;
public class ChatActivity extends Activity {
private static final String PATH1 = "http://img0.pconline.com.cn/pconline/1511/29/7257120_901_thumb.jpg";
private static final String PATH2 = "http://img2.imgtn.bdimg.com/it/u=1122649470,955539824&fm=26&gp=0.jpg";
private RecyclerView recyclerView;
private EditText input;
private Button send, receive;
private List<ContentModel> items;
private User other;
private User yourself;
private MultiTypeAdapter adapter;
private long lastTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
recyclerView = findViewById(R.id.recyclerview);
input = findViewById(R.id.input);
send = findViewById(R.id.send);
receive = findViewById(R.id.receive);
adapter = new MultiTypeAdapter();
//一对多,都有相同的父框架结构(头像、昵称、时间……等)
adapter.register(ContentModel.class).to(
new SimpleTextViewBinder(ContentModel.SEND_TYPE_OTHERS),//左边的布局(别人发的消息)
new SimpleTextViewBinder(ContentModel.SEND_TYPE_YOURSELF),//右边的布局(自己发的消息)
new SimpleImageViewBinder(ContentModel.SEND_TYPE_OTHERS),
new SimpleImageViewBinder(ContentModel.SEND_TYPE_YOURSELF)
).withLinker(new Linker<ContentModel>() {
@Override
public int index(int position, @NonNull ContentModel model) {
if (model.msgType == ContentModel.MSG_TYPE_SIMPLE_TEXT && model.sendType == ContentModel.SEND_TYPE_OTHERS)
return 0;//左边的布局(别人发的消息)
else if (model.msgType == ContentModel.MSG_TYPE_SIMPLE_TEXT && model.sendType == ContentModel.SEND_TYPE_YOURSELF)
return 1;//右边的布局(自己发的消息)
else if (model.msgType == ContentModel.MSG_TYPE_SIMPLE_IMAGE && model.sendType == ContentModel.SEND_TYPE_OTHERS)
return 2;
else if (model.msgType == ContentModel.MSG_TYPE_SIMPLE_IMAGE && model.sendType == ContentModel.SEND_TYPE_YOURSELF)
return 3;
return 0;
}
});
adapter.register(TimeSimple.class, new TimeBinder());
recyclerView.setLayoutManager(new LinearLayoutManager(this));//new GridLayoutManager(this,2)
recyclerView.setAdapter(adapter);
assertHasTheSameAdapter(recyclerView, adapter);//断言 recyclerView 使用的是正确的 adapter,可选择性使用
other = new User("other", PATH1);
yourself = new User("包青天", PATH2);
items = new ArrayList<>();
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long currentTime = System.currentTimeMillis();
if (lastTime == 0 || currentTime - lastTime > 5000){
//插入时间数据
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm");
String time = format.format(new Date(currentTime));
items.add(new TimeSimple(time));
lastTime = currentTime;
}
String text = input.getText().toString();
if (text.equals("1")){
items.add(new SimpleImage(yourself, ContentModel.SEND_TYPE_YOURSELF, PATH1));
}else{
items.add(new SimpleText(yourself, ContentModel.SEND_TYPE_YOURSELF, text));
}
adapter.setItems(items);
adapter.notifyDataSetChanged();
assertAllRegistered(adapter, items);//断言所有使用的类型都已注册,需要在加载或更新数据之后调用,可选择性使用
recyclerView.scrollToPosition(items.size() - 1);
}
});
receive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long currentTime = System.currentTimeMillis();
if (lastTime == 0 || currentTime - lastTime > 5000){
//插入时间数据
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm");
String time = format.format(new Date(currentTime));
items.add(new TimeSimple(time));
lastTime = currentTime;
}
String text = input.getText().toString();
if (text.equals("1")){
items.add(new SimpleImage(other, ContentModel.SEND_TYPE_OTHERS, PATH2));
}else{
items.add(new SimpleText(other, ContentModel.SEND_TYPE_OTHERS, text));
}
adapter.setItems(items);
adapter.notifyDataSetChanged();
assertAllRegistered(adapter, items);//断言所有使用的类型都已注册,需要在加载或更新数据之后调用,可选择性使用
recyclerView.scrollToPosition(items.size() - 1);
}
});
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
读书笔记:采用multitype框架构建聊天界面demo.zip (90个子文件)
读书笔记:采用multitype框架构建聊天界面demo
MyUITest
gradle.properties 728B
gradle
wrapper
gradle-wrapper.jar 53KB
gradle-wrapper.properties 233B
app
src
androidTest
java
com
example
myuitest
ExampleInstrumentedTest.java 724B
test
java
com
example
myuitest
ExampleUnitTest.java 381B
main
java
com
example
myuitest
normal
SimpleTextViewBinder.java 1KB
SimpleImage.java 206B
ContentModel.java 373B
SimpleText.java 205B
SimpleImageViewBinder.java 1KB
NormalActivity.java 1KB
chat
msgtype
SimpleTextViewBinder.java 1KB
TimeBinder.java 954B
SimpleImage.java 374B
TimeSimple.java 246B
SimpleText.java 350B
SimpleImageViewBinder.java 1KB
ChatActivity.java 6KB
User.java 193B
frame
ContentHolder.java 770B
ChatFrameBinder.java 3KB
ContentModel.java 655B
QueueActivity.java 3KB
MainActivity.java 1KB
titlelist
ChildViewBinder.java 2KB
TitleListActivity.java 4KB
ParentViewBinder.java 2KB
Parent.java 303B
Child.java 301B
CountryDetailBean.java 5KB
res
mipmap-xxhdpi
ic_launcher_round.png 10KB
ic_launcher.png 8KB
mipmap-hdpi
ic_launcher_round.png 5KB
ic_launcher.png 3KB
drawable-v24
ic_launcher_foreground.xml 2KB
mipmap-anydpi-v26
ic_launcher.xml 272B
ic_launcher_round.xml 272B
mipmap-mdpi
ic_launcher_round.png 3KB
ic_launcher.png 2KB
mipmap-xxxhdpi
ic_launcher_round.png 15KB
ic_launcher.png 9KB
mipmap-xhdpi
ic_launcher_round.png 7KB
ic_launcher.png 4KB
values
dimens.xml 211B
colors.xml 208B
strings.xml 76B
styles.xml 3KB
layout
item_simple_text_left.xml 461B
item_simple_image_left.xml 409B
item_big_image.xml 799B
item_simple_voice_left.xml 1KB
activity_chat.xml 1KB
item_title.xml 873B
activity_title_list.xml 563B
activity_main.xml 424B
item_frame_right.xml 1017B
item_simple_image_right.xml 456B
item_frame_left.xml 973B
item_time.xml 511B
activity_queue.xml 642B
item_content.xml 784B
item_simple_voice_right.xml 1KB
item_simple_text_right.xml 657B
drawable
ic_launcher_background.xml 5KB
icon.jpg 190KB
AndroidManifest.xml 1KB
proguard-rules.pro 751B
build.gradle 1KB
.gitignore 7B
gradlew.bat 2KB
.git
index 9KB
HEAD 23B
refs
heads
master 41B
tags
remotes
origin
master 41B
objects
pack
pack-a19197f7f5d578bcbf9215a22857dab7aba72943.idx 5KB
pack-a19197f7f5d578bcbf9215a22857dab7aba72943.pack 334KB
info
FETCH_HEAD 119B
logs
HEAD 130B
refs
heads
master 130B
remotes
origin
master 144B
hooks
config 256B
branches
build.gradle 558B
.idea
codeStyles
Project.xml 2KB
runConfigurations.xml 564B
vcs.xml 167B
misc.xml 262B
gradle.xml 884B
settings.gradle 15B
gradlew 5KB
.gitignore 203B
共 90 条
- 1
资源评论
baidu_16992441
- 粉丝: 311
- 资源: 1041
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功