鱼C论坛

 找回密码
 立即注册
查看: 1977|回复: 0

[学习笔记] android手机编程3.7

[复制链接]
发表于 2017-10-1 10:12:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这个小案列非常简单,图片我就从网上随便找了一个,如果没有在布局文件里随便设置个背景颜色也行。 首先在build.gradle里安装依赖,在dependencies标签下添加一行
  1. compile 'com.android.support:recyclerview-v7:26.+'
复制代码

然后修改activity_main.xml文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#d8e0e8"
  6.     android:orientation="vertical">

  7.     <android.support.v7.widget.RecyclerView
  8.         android:id="@+id/msg_recycleview"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="0dp"
  11.         android:layout_weight="1"
  12.          />
  13.     <LinearLayout
  14.         android:layout_width="match_parent"
  15.         android:layout_height="wrap_content">
  16.         <EditText
  17.             android:id="@+id/input_text"
  18.             android:layout_width="0dp"
  19.             android:layout_height="wrap_content"
  20.             android:layout_weight="1"
  21.             android:hint="type something here"
  22.             />
  23.         <Button
  24.             android:id="@+id/send"
  25.             android:layout_width="wrap_content"
  26.             android:layout_height="wrap_content"
  27.             android:text="send"/>
  28.     </LinearLayout>
  29. </LinearLayout>
复制代码
然后再mainactivity.java旁边新建一个javabean的类
  1. package com.example.xinwei.chartactivity;

  2. /**
  3. * Created by xinwei on 2017/10/1.
  4. */

  5. public class Msg {
  6.     public static final int TYPE_RECEIVED = 0;
  7.     public static final int TYPE_SENT = 1;
  8.     private String content;
  9.     private int type;

  10.     public String getContent() {
  11.         return content;
  12.     }

  13.     public int getType() {
  14.         return type;
  15.     }

  16.     public Msg(String content, int type){
  17.         this.content=content;
  18.         this.type=type;
  19.     }
  20. }
复制代码

在布局layout文件夹下新建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" android:layout_width="match_parent"
  4.     android:layout_height="wrap_content"
  5.     android:padding="10dp">
  6.     <LinearLayout
  7.         android:id="@+id/left_layout"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:layout_gravity="left"
  11.         android:background="@drawable/message_left">
  12.     <TextView
  13.         android:id="@+id/left_msg"
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="wrap_content"
  16.         android:layout_gravity="center"
  17.         android:layout_margin="10dp"
  18.         android:textColor="#fff"/>
  19.     </LinearLayout>
  20.     <LinearLayout
  21.         android:id="@+id/right_layout"
  22.         android:layout_width="wrap_content"
  23.         android:layout_height="wrap_content"
  24.         android:layout_gravity="right"
  25.         android:background="@drawable/message_right">
  26.         <TextView
  27.             android:id="@+id/right_msg"
  28.             android:layout_width="wrap_content"
  29.             android:layout_height="wrap_content"
  30.             android:layout_gravity="center"
  31.             android:layout_margin="10dp"/>
  32.     </LinearLayout>
  33. </LinearLayout>
复制代码

然后创建适配器类,在mainactivity.java旁边新建个MsgAdapter.java文件
  1. package com.example.xinwei.chartactivity;

  2. import android.support.v7.widget.RecyclerView;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.LinearLayout;
  7. import android.widget.TextView;

  8. import java.util.List;

  9. /**
  10. * Created by xinwei on 2017/10/1.
  11. */

  12. public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
  13.     private List<Msg> mMsgList;
  14.     static class ViewHolder extends RecyclerView.ViewHolder{
  15.         LinearLayout leftLayout;
  16.         LinearLayout rightLayout;
  17.         TextView leftMsg;
  18.         TextView rightMsg;
  19.         public ViewHolder(View view) {
  20.             super(view);
  21.             leftLayout=(LinearLayout)view.findViewById(R.id.left_layout);
  22.             rightLayout=(LinearLayout)view.findViewById(R.id.right_layout);
  23.             leftMsg=(TextView)view.findViewById(R.id.left_msg);
  24.             rightMsg=(TextView)view.findViewById(R.id.right_msg);
  25.         }
  26.     }
  27.     public MsgAdapter(List<Msg> msgList){
  28.         mMsgList=msgList;
  29.     }
  30.     @Override
  31.     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  32.         View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
  33.         return new ViewHolder(view);
  34.     }

  35.     @Override
  36.     public void onBindViewHolder(ViewHolder holder, int position) {
  37.         Msg msg = mMsgList.get(position);
  38.         if(msg.getType()==Msg.TYPE_RECEIVED){
  39.             holder.leftLayout.setVisibility(View.VISIBLE);
  40.             holder.rightLayout.setVisibility(View.GONE);
  41.             holder.leftMsg.setText(msg.getContent());
  42.         }else if(msg.getType()==Msg.TYPE_SENT){
  43.             holder.leftLayout.setVisibility(View.GONE);
  44.             holder.rightLayout.setVisibility(View.VISIBLE);
  45.             holder.rightMsg.setText(msg.getContent());
  46.         }
  47.     }

  48.     @Override
  49.     public int getItemCount() {
  50.         return mMsgList.size();
  51.     }
  52. }
复制代码

然后修改mainactivity.java中的代码
  1. package com.example.xinwei.chartactivity;

  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.support.v7.widget.LinearLayoutManager;
  5. import android.support.v7.widget.RecyclerView;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;

  9. import java.util.ArrayList;
  10. import java.util.List;

  11. public class MainActivity extends AppCompatActivity {
  12.     private List<Msg> msgList = new ArrayList<>();
  13.     private EditText inputText;
  14.     private Button send;
  15.     private RecyclerView msgRecycleView;
  16.     private MsgAdapter adapter;
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.         initMsgs();
  22.         inputText=(EditText)findViewById(R.id.input_text);
  23.         send=(Button)findViewById(R.id.send);
  24.         msgRecycleView=(RecyclerView)findViewById(R.id.msg_recycleview);
  25.         LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  26.         msgRecycleView.setLayoutManager(layoutManager);
  27.         adapter=new MsgAdapter(msgList);
  28.         msgRecycleView.setAdapter(adapter);
  29.         send.setOnClickListener(new View.OnClickListener() {
  30.             @Override
  31.             public void onClick(View view) {
  32.                 String content = inputText.getText().toString();
  33.                 if(!"".equals(content)){
  34.                     Msg msg=new Msg(content,Msg.TYPE_SENT);
  35.                     msgList.add(msg);
  36.                     adapter.notifyItemInserted(msgList.size()-1);
  37.                     msgRecycleView.scrollToPosition(msgList.size()-1);
  38.                     inputText.setText("");
  39.                 }
  40.             }
  41.         });
  42.     }

  43.     private void initMsgs() {
  44.         Msg msg1 =new Msg("hello guy",Msg.TYPE_RECEIVED);
  45.         msgList.add(msg1);
  46.         Msg msg2 =new Msg("hello who is that",Msg.TYPE_SENT);
  47.         msgList.add(msg2);
  48.         Msg msg3 =new Msg("This is Tom.Nice talking to you",Msg.TYPE_RECEIVED);
  49.         msgList.add(msg3);
  50.     }
  51. }
复制代码

最后最好准备两张图片修改文件名为msg_left.png和msg_right.png放进drawable目录,效果图:

jdfw.gif

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
MSK + 5 + 5 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 01:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表