模拟QQ聊天记录界面 RecyclerView

首先,在build.gradle中添加函数依赖库。
编辑主界面。
<《》LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”
android:background=”#d8e0e8”
>
<《》android.support.v7.widget.RecyclerView
android:id=”@+id/recyclerview”
android:layout_width=”match_parent”
android:layout_height=”0dp”
android:layout_weight=”1”/>
<《》LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”>
<《》EditText
android:id=”@+id/input_text”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1”
android:hint=”Type spmething here”
android:maxLines=”2”/>
<《》Button
android:id=”@+id/send”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”send”/>
<《》/LinearLayout>
<《》/LinearLayout>

定义RecyclerView,其中的weight是占的区块。
再定义一个LinearLayout,添加文本行和发送按钮

创建一个类,进行对消息进行体现的。
public class Msg {
public static final int TYPE_RECEIVED = 0;//接收的信号
public static final int TYPE_SEND = 1;//发送的信号
private String content;//发送还是接收的内容
private int type;//发送还是接受
public Msg(String content,int type){
this.content = content;
this.type = type;
}
public String getContent(){//获取内容
return content;
}
public int getType(){//获取是发送还是接收
return type;
}

创建一个msg_item.xml布局:
<《》LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:padding=”10dp”
>
<《》LinearLayout
android:id=”@+id/left_layout”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”left”
android:background=”@drawable/dialog” >
<《》TextView
android:id=”@+id/left_msg”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:layout_margin=”10dp” />
<《》/LinearLayout>
<《》LinearLayout
android:id=”@+id/right_layout”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”right”
android:background=”@drawable/dialog_right”>
<《》TextView
android:id=”@+id/right_msg”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:layout_margin=”10dp”/>
<《》/LinearLayout>
<《》/LinearLayout>

添加两个线性排列,一个为左边,一个为右边,都含有文本框

创建一个适配器:
public class MsgAdapter extends RecyclerView.Adapter<《》MsgAdapter.ViewHolder> {
private List<《》Msg> mMsgList;
static class ViewHolder extends RecyclerView.ViewHolder {
LinearLayout leftLayout;//左边的布局
LinearLayout rightLayout;//右边的布局
TextView leftMsg;
TextView rightMsg;
public ViewHolder(View view){
super(view);
leftLayout = (LinearLayout)view.findViewById(R.id.left_layout);
rightLayout =(LinearLayout)view.findViewById(R.id.right_layout);
leftMsg = (TextView)view.findViewById(R.id.left_msg);
rightMsg = (TextView)view.findViewById(R.id.right_msg);
}
}
public MsgAdapter(List<《》Msg> msgList){
mMsgList = msgList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {//创建其实例,并将view发送到ViewHolder类中,进行获取id号
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.msg_item,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
Msg msg = mMsgList.get(i);
if(msg.getType()==Msg.TYPE_RECEIVED){
//显示全局,来分别选取需要的局面
//如果是收到的消息,则显示左边的消息布局,将右边的消息布局隐藏
viewHolder.leftLayout.setVisibility(View.VISIBLE);//显示左边的布局,有内容
viewHolder.rightLayout.setVisibility(View.GONE);//将右边的布局隐藏,无内容
viewHolder.leftMsg.setText(msg.getContent());
}
else
if (msg.getType()==Msg.TYPE_SEND){
//如果是收到的消息,则显示右边的消息布局,将左边的消息布局隐藏
viewHolder.rightLayout.setVisibility(View.VISIBLE);//隐藏左边的布局,有内容
viewHolder.leftLayout.setVisibility(View.GONE);//将左边的布局显示,无内容
viewHolder.rightMsg.setText(msg.getContent());
}
}
@Override
public int getItemCount() {
return mMsgList.size();
}}
适配器多次提起用来对子项的显示。

主类中:
public class MainActivity extends AppCompatActivity {
public List<《》Msg> msgList = new ArrayList<>();
private EditText inputText;
private Button send;
private RecyclerView msgRecyclerView;
private MsgAdapter msgAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMsgs();//初始化
inputText = (EditText)findViewById(R.id.input_text);
send = (Button)findViewById(R.id.send);
msgRecyclerView = (RecyclerView)findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
msgRecyclerView.setLayoutManager(layoutManager);
msgAdapter = new MsgAdapter(msgList);
msgRecyclerView.setAdapter(msgAdapter);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String content = inputText.getText().toString();
if(!”“.equals(content)){
Msg msg = new Msg(content,Msg.TYPE_SEND);
msgList.add(msg);
msgAdapter.notifyItemInserted(msgList.size()-1);//当有新消息时,刷新RecyclerView中的显示,将添加刚刚输入的内容。
msgRecyclerView.scrollToPosition(msgList.size()-1);//将RecyclerView定位在最后一个,此函数进行定位
inputText.setText(“”);//清空
}
}
});
}
public void initMsgs(){
Msg msg1 = new Msg(“Hello guy “,Msg.TYPE_RECEIVED);
msgList.add(msg1);
Msg msg2 = new Msg(“hello xxxxxxxxxxxxxxx”,Msg.TYPE_SEND);
msgList.add(msg2);
Msg msg3 = new Msg(“xiixixixixixiix!”,Msg.TYPE_RECEIVED);
msgList.add(msg3);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值