编写简单的聊天界面
来源:互联网 发布:windows上类似imovie 编辑:程序博客网 时间:2024/05/17 00:00
MainActivity
public class MainActivity extends AppCompatActivity { private List<Msg> msgList = new ArrayList<>(); private EditText inputText; private Button send; private RecyclerView msgRecyclerView; private MsgAdapter adapter; @Override 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.msg_recycler_view); //定义一个线性布局对象 LinearLayoutManager layoutManager = new LinearLayoutManager(this); //将线性布局对象设置在滚动控件中 msgRecyclerView.setLayoutManager(layoutManager); //将适配器与数据关联 adapter = new MsgAdapter(msgList); //将适配器设置在控件中 msgRecyclerView.setAdapter(adapter); /* * 监听Send按钮 * 获取TextView中的内容 * 如果TextView中的内容不为空 * 1.将内容与消息类型传入实体类的构造函数 * 2.将消息添加到masList集合中 * 3.通知列表有新的数据加入 * 4.将控件位置定在最后一个 * 5.将TextView内容置空 * */ 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_SENT); msgList.add(msg); adapter.notifyItemInserted(msgList.size() - 1); msgRecyclerView.scrollToPosition(msgList.size() - 1); inputText.setText(""); } } }); } private void initMsgs() { Msg msg1 = new Msg("Hello guy.", Msg.TYPE_SENT); msgList.add(msg1); Msg msg2 = new Msg("hello. who is that?" ,Msg.TYPE_RECEIVED); msgList.add(msg2); Msg msg3 = new Msg("This is Tom, Nice taking to you", Msg.TYPE_RECEIVED); msgList.add(msg3); }}
MsgAdapter
//泛型指定为MsgAdapter.ViewHolder,是MsgAdapter中的类public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> { private List<Msg> mMsgList; //外部类继承自RecyclerView.Adapter,内部类继承自RecyclerView.ViewHolder static class ViewHolder extends RecyclerView.ViewHolder { //定义控件对象 LinearLayout leftLayout; LinearLayout rightLayout; TextView leftMsg; TextView rightMsg; public ViewHolder(View itemView) { super(itemView); //获取控件实例 leftLayout = itemView.findViewById(R.id.left_layout); rightLayout = itemView.findViewById(R.id.right_layout); leftMsg = itemView.findViewById(R.id.left_msg); rightMsg = itemView.findViewById(R.id.right_msg); } } public MsgAdapter(List<Msg> mMsgList) { this.mMsgList = mMsgList; } //获取ViewHolder实例 @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item, parent, false); //view通常是RecyclerView子项最外层布局,传到ViewHolder中,可获取控件实例 return new ViewHolder(view); } //子项滚进屏幕时执行,用于设置数据 @Override public void onBindViewHolder(ViewHolder holder, int position) { Msg msg = mMsgList.get(position); //如果是收到信息,显示左边图片和文字,隐藏右边,反之。 if(msg.getType() == Msg.TYPE_RECEIVED) { holder.leftLayout.setVisibility(View.VISIBLE); holder.rightLayout.setVisibility(View.GONE); holder.leftMsg.setText(msg.getContent()); } else if (msg.getType() == Msg.TYPE_SENT) { holder.rightLayout.setVisibility(View.VISIBLE); holder.leftLayout.setVisibility(View.GONE); holder.rightMsg.setText(msg.getContent()); } } @Override public int getItemCount() { return mMsgList.size(); }}
实体类Msg
public class Msg { public static final int TYPE_RECEIVED = 0; public static final int TYPE_SENT = 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; }}
主布局activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#d8e0e8"> <android.support.v7.widget.RecyclerView android:id="@+id/msg_recycler_view" 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 something 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布局msg_item
<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/left"> <TextView android:id="@+id/left_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:textColor="#121212"/> </LinearLayout> <LinearLayout android:id="@+id/right_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:background="@drawable/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>
阅读全文
0 0
- 编写简单的聊天界面
- 编写一个简单的聊天界面
- android 怎么编写一个简单的聊天界面
- 编写精美的聊天界面
- 自定义Notification 聊天界面的编写
- 【Android】48、编写精美的聊天界面
- Pyqt4下聊天软件界面的编写
- 聊天窗口界面编写
- Android编写聊天界面
- 编写界面的最佳实践——聊天界面
- 编写界面的最佳实践——聊天界面
- 简单的聊天界面demo FaceChatDemo
- 简单实现带界面的一对一聊天
- 一个简单聊天界面的UI实现
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- MotionEvent的理解
- HTML img图片加载失败时用默认图片替换
- Linux 安装
- 名称不能以"<"字符(十六进制值0x3C)开头
- Animation的理解
- 编写简单的聊天界面
- 腾讯云服务器云+校园领取代金卷地址
- 使用编辑器编写一个java才程序
- 关于通知的一些小问题
- TCP的粘包问题
- c++数据结构快速排序
- Java抽象类的概念和使用
- 数据结构--one
- 基于C语言sscanf()函数的深入理解