• Android学习——利用RecyclerView编写聊天界面


    1、待会儿会用到RecyclerView,首先在app/build.gradle(注意有两个build.gradle,选择app下的那个)当中添加依赖库,如下:

    1 dependencies {
    2     compile fileTree(dir: 'libs', include: ['*.jar'])
    3     compile 'com.android.support:appcompat-v7:24.2.1'
    4     compile 'com.android.support:recyclerview-v7:24.2.1'
    5     testCompile 'junit:junit:4.12'
    6     androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    7         exclude group: 'com.android.support', module: 'support-annotations'
    8     })
    9 }

    添加完之后记得点击Sync Now进行同步。

    2、开始编写主界面,修改activity_main.xml中的代码,如下:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:id="@+id/activity_main"
     4     android:orientation="vertical"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     android:background="#d8e0e8"
     8     >
     9     <android.support.v7.widget.RecyclerView
    10         android:id="@+id/msg_recycler_view"
    11         android:layout_width="match_parent"
    12         android:layout_height="0dp"
    13         android:layout_weight="1"
    14         />
    15     <LinearLayout
    16         android:layout_width="match_parent"
    17         android:layout_height="wrap_content">
    18         <EditText
    19             android:id="@+id/input_text"
    20             android:layout_width="0dp"
    21             android:layout_height="wrap_content"
    22             android:layout_weight="1"
    23             android:hint="Type something here"
    24             android:maxLines="2"
    25             />
    26         <Button
    27             android:id="@+id/send"
    28             android:layout_width="wrap_content"
    29             android:layout_height="wrap_content"
    30             android:text="send"
    31             />
    32     </LinearLayout>
    33 </LinearLayout>

    RecyclerView用于显示聊天的消息内容(因为不是内置在系统SDK中的,所以需要把完整的包路径写出来);

    放置一个EditView用于输入消息,一个Button用于发送消息。

    3、定义消息的实体类,新建Msg,代码如下:

     1 public class Msg {
     2     public static final int TYPE_RECEIVED=0;
     3     public static final int TYPE_SENT=1;
     4     private String content;
     5     private int type;
     6     public Msg(String content,int type){
     7         this.content=content;
     8         this.type=type;
     9     }
    10     public String getContent(){
    11         return content;
    12     }
    13 
    14     public int getType(){
    15         return type;
    16     }
    17 }

    Msg只有两个字段,content表示消息的内容,type表示消息的类型(二值可选,一个是TYPE_RECRIVED,一个是TYPE_SENT)。

    4、接着编写RecyclerView子项的布局,新建msg_item.xml,代码如下:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical"
     4     android:layout_width="match_parent"
     5     android:layout_height="wrap_content"
     6     android:padding="10dp"
     7     >
     8 
     9     <LinearLayout
    10         android:id="@+id/left_layout"
    11         android:layout_width="283dp"
    12         android:layout_height="106dp"
    13         android:layout_gravity="left"
    14         android:background="@drawable/zuo"
    15         android:weightSum="1">
    16 
    17         <TextView
    18             android:id="@+id/left_msg"
    19             android:layout_width="match_parent"
    20             android:layout_height="wrap_content"
    21             android:layout_gravity="center"
    22             android:layout_margin="10dp"
    23             />
    24     </LinearLayout>
    25 
    26     <LinearLayout
    27         android:id="@+id/right_layout"
    28         android:layout_width="229dp"
    29         android:layout_height="109dp"
    30         android:layout_gravity="right"
    31         android:background="@drawable/you"
    32         >
    33         <TextView
    34             android:id="@+id/right_msg"
    35             android:layout_width="wrap_content"
    36             android:layout_height="wrap_content"
    37             android:layout_gravity="center"
    38             android:layout_margin="10dp"
    39             />
    40     </LinearLayout>
    41 
    42 </LinearLayout>

    收到的消息局左对齐,发出的消息居右对齐,并用相应的图片作为背景。

    5、创建RecyclerView的适配器类,新建MsgAdapter,代码如下:

     1 public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
     2     private List<Msg> mMsgList;
     3     static class ViewHolder extends RecyclerView.ViewHolder{
     4         LinearLayout leftLayout;
     5         LinearLayout rightLayout;
     6         TextView leftMsg;
     7         TextView rightMsg;
     8         public ViewHolder(View view){
     9             super(view);
    10             leftLayout=(LinearLayout)view.findViewById(R.id.left_layout);
    11             rightLayout=(LinearLayout)view.findViewById(R.id.right_layout);
    12             leftMsg=(TextView)view.findViewById(R.id.left_msg);
    13             rightMsg=(TextView)view.findViewById(R.id.right_msg);
    14         }
    15     }
    16     public MsgAdapter(List<Msg> msgList){
    17         mMsgList=msgList;
    18     }
    19     @Override
    20     public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){               //onCreateViewHolder()用于创建ViewHolder实例
    21         View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
    22         return new ViewHolder(view);                                                   //把加载出来的布局传到构造函数中,再返回
    23     }
    24     @Override
    25     public void onBindViewHolder(ViewHolder Holder,int position){                     //onBindViewHolder()用于对RecyclerView子项的数据进行赋值,会在每个子项被滚动到屏幕内的时候执行
    26         Msg msg=mMsgList.get(position);
    27         if(msg.getType()==Msg.TYPE_RECEIVED){                                         //增加对消息类的判断,如果这条消息是收到的,显示左边布局,是发出的,显示右边布局
    28             Holder.leftLayout.setVisibility(View.VISIBLE);
    29             Holder.rightLayout.setVisibility(View.GONE);
    30             Holder.leftMsg.setText(msg.getContent());
    31         }else if(msg.getType()==Msg.TYPE_SENT) {
    32             Holder.rightLayout.setVisibility(View.VISIBLE);
    33             Holder.leftLayout.setVisibility(View.GONE);
    34             Holder.rightMsg.setText(msg.getContent());
    35         }
    36     }
    37     @Override
    38     public int getItemCount(){
    39         return mMsgList.size();
    40     }
    41 }

    6、最后修改MainActivity中的代码,来为RecyclerView初始化一些数据,并给发送按钮加入事件响应,代码如下:

     1 public class MainActivity extends AppCompatActivity {
     2     private List<Msg> msgList=new ArrayList<>();
     3     private EditText inputText;
     4     private Button send;
     5     private RecyclerView msgRecyclerView;
     6     private MsgAdapter adapter;
     7 
     8     @Override
     9     protected void onCreate(Bundle savedInstanceState) {
    10         super.onCreate(savedInstanceState);
    11         setContentView(R.layout.activity_main);
    12         initMsgs();                                                         //初始化消息数据
    13         inputText=(EditText)findViewById(R.id.input_text);
    14         send=(Button)findViewById(R.id.send);
    15         msgRecyclerView=(RecyclerView)findViewById(R.id.msg_recycler_view);
    16 
    17         LinearLayoutManager layoutManager=new LinearLayoutManager(this);    //LinearLayoutLayout即线性布局,创建对象后把它设置到RecyclerView当中
    18         msgRecyclerView.setLayoutManager(layoutManager);
    19 
    20         adapter=new MsgAdapter(msgList);                                    //创建MsgAdapter的实例并将数据传入到MsgAdapter的构造函数中
    21         msgRecyclerView.setAdapter(adapter);
    22 
    23         send.setOnClickListener(new View.OnClickListener(){                 //发送按钮点击事件
    24             @Override
    25             public void onClick(View v){
    26                 String content=inputText.getText().toString();              //获取EditText中的内容
    27                 if(!"".equals(content)){                                    //内容不为空则创建一个新的Msg对象,并把它添加到msgList列表中
    28                     Msg msg=new Msg(content,Msg.TYPE_SENT);
    29                     msgList.add(msg);
    30                     adapter.notifyItemInserted(msgList.size()-1);           //调用适配器的notifyItemInserted()用于通知列表有新的数据插入,这样新增的一条消息才能在RecyclerView中显示
    31 msgRecyclerView.scrollToPosition(msgList.size()-1); //调用scrollToPosition()方法将显示的数据定位到最后一行,以保证可以看到最后发出的一条消息 32 inputText.setText(""); //调用EditText的setText()方法将输入的内容清空 33 } 34 } 35 }); 36 } 37 38 private void initMsgs(){ 39 Msg msg1=new Msg("Hello guy.",Msg.TYPE_RECEIVED); 40 msgList.add(msg1); 41 Msg msg2=new Msg("Hello.Who is that?",Msg.TYPE_SENT); 42 msgList.add(msg2); 43 Msg msg3=new Msg("This is Tom!",Msg.TYPE_RECEIVED); 44 msgList.add(msg3); 45 } 46 }

    运行程序,效果如下:

  • 相关阅读:
    lsmod命令详解
    init命令详解
    runlevel 命令详解
    nohup命令详解
    nice和renice命令详解
    pstree命令详解
    ps命令详解
    crontab命令详解
    pkill命令详解
    killall命令详解
  • 原文地址:https://www.cnblogs.com/cxq1126/p/7193228.html
Copyright © 2020-2023  润新知