《第一行代码Android》学习总结第三章 编写聊天界面实践

1、用通过Nine-Patch制作好的气泡图片作为图片资源。

2、在app/build.gradle中添加依赖库,使用RecyclerView。

compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'

3、编写activity_main.xml文件,放置用于显示聊天内容界面,EditText用于输入消息,Button用于输入消息。


    
    
        
        

4、创建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;
    }
}

5、编写RecyclerView子项布局msg_item.xml,让收到的消息左对齐,发出的消息右对齐。


    
        
    
    
        
    

6、新建适配器类MsgAdapter类

public class MsgAdapter extends RecyclerView.Adapter {
    private List mMsgList;
    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftMsg;
        TextView rightMsg;
        public ViewHolder(View view) {
            super(view);
            leftLayout = view.findViewById(R.id.left_layout);
            rightLayout = view.findViewById(R.id.right_layout);
            leftMsg = view.findViewById(R.id.left_mes);
            rightMsg = view.findViewById(R.id.right_mes);
        }
    }
    public MsgAdapter(List mMsgList) {
        this.mMsgList = mMsgList;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
        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();
    }
}

7、编写MainActivity.java中代码

public class MainActivity extends AppCompatActivity {
    private List 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);
        initMsg();
        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.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = inputText.getText().toString();
//如果输入消息不为空,创建新的Msg对象加入到msgList中
                if(!"".equals(content)){
                    Msg msg=new Msg(content,Msg.TYPE_SENT);
                    msgList.add(msg);
//当有新消息时,通知列表有新数据加入,刷新RecyclerView中的显示
                    adapter.notifyItemInserted(msgList.size()-1);
//将显示的数据定位到最后一行,保证一定能看到最后发出的一行数据
                    msgRecyclerView.scrollToPosition(msgList.size()-1);
                    inputText.setText("");
                }
            }
        });
    }
//创建消息数据
    private void initMsg() {
        Msg msg1 = new Msg("Hello Guys" ,Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("Who is that" ,Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3 = new Msg("This is Tom" ,Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }
}

 

你可能感兴趣的:(Android,第一行代码)