Android Chat User Interface demo(Making a deep impression for myself)

LayoutCode:
partOne:
name : activity_main.xml



    
    
        
        

功能: 提供一个聊天界面的布局,其中layout_width="0dp"表明让出控件大小的控制权,而后的layout_weight接受了控制权,依照安卓界面的大小与控件的数量其weight的值,按照比例分配指名的weight权重。wrap_content占据默认的界面空间,
maxLines表示输入框最多可以看见两行的输入。android.support.v7.widget.RecyclerView是一种控件的布局方式,其内部的风格可以自定义。
partTwo:
name: activity_recycler_layout.xml



    
        
    
    
        
    


功能:提供一组用户输入、接收其他客户输入的数据显示的功能,@+id/value表示创建id值为value的控件,以供java程序调用,layout_gravity字面山的意思就是布局的位置。layout_margin表明了布局和其他控件或布局的间距,这与html一致。TextView意思是可视文本。

Android Java Code:
partOne:
name : MainActivity.java

package com.example.chatui;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List msgList = new ArrayList<>();
    private EditText inputText;
    private Button send;
    private RecyclerView msgRecyclerView;
    private RecyclerLayoutActivity 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.recylerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter = new RecyclerLayoutActivity(msgList);
        msgRecyclerView.setAdapter(adapter);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                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_RECEIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("Hello. Who is that?", Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3 = new Msg("This is Tome. Nice talking to you.", Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }
}

功能:各个主界面控件的初始化、控件监听及数据的调用存入;由于软件经常性的需求不只是文本,所以在适配器的使用时,我们的数组需要继承,自定义一部分内容,具体内容如下:
partTwo:
name : RecyclerLayoutActivity.java

package com.example.chatui;


import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

public class RecyclerLayoutActivity extends RecyclerView.Adapter {
    private List mMsgList;

public RecyclerLayoutActivity(List msgList) {
    mMsgList = msgList;
}

static class ViewHolder extends RecyclerView.ViewHolder {
    LinearLayout leftLayout;
    LinearLayout rightLayout;
    TextView leftMsg;
    TextView rightMsg;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        leftLayout = (LinearLayout) itemView.findViewById(R.id.left_side);
        rightLayout = (LinearLayout) itemView.findViewById(R.id.right_side);
        leftMsg = (TextView) itemView.findViewById(R.id.text_left);
        rightMsg = (TextView) itemView.findViewById(R.id.text_right);
    }
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_recycler_layout, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List payloads) {
    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();
}


}
 
  

功能: 自定义一个类,继承RecyclerView.Adapter适配器类,这个类是泛型的类,其数据类型可以外部指定。其中大部分的逻辑运行过程在父类中运行,几个主要要修改的抽象类只需要列出即可,我们需要修改的几个方法就是
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)、
public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List payloads)、 public int getItemCount(), 这些方法的是对于父类的重写,其中主要与视图相关的就是public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List payloads)方法,在里面可以自定义多种视图方式,其余holder相关的就是缓存、性能优化方面的内容,最后一个返回size的方法为给父类方法的信息。

由于java是高级的面向对象语言,为了避免过多的参数在程序中传递引起程序的混乱,及bug,故而需要将基本的信息进行封装,封装为一个类后,通过setter\getter或者构造函数的形式可以相对简洁易懂的方式处理程序,具体的封装类入选下:

partThree:
name: Msg.java

package com.example.chatui;

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;
    }
}

功能: 存储数据的对象类;
最后运行的结果:
Android Chat User Interface demo(Making a deep impression for myself)_第1张图片素材:
在这里插入图片描述
在这里插入图片描述

你可能感兴趣的:(安卓)