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

LayoutCode:
partOne:
name : activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/recylerview"
        android:layout_weight="1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff">
        <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="发送"/>
    </LinearLayout>
</LinearLayout>

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/right_side"
        android:layout_gravity="right"
        android:background="@drawable/puple"
        android:layout_margin="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text_right"
            android:textSize="18sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/left_side"
        android:background="@drawable/puppleleft"
        android:layout_gravity="left"
        android:layout_margin="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text_left"
            android:textSize="18sp"/>
    </LinearLayout>

</LinearLayout>

功能:提供一组用户输入、接收其他客户输入的数据显示的功能,@+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<Msg> 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<RecyclerLayoutActivity.ViewHolder> {
    private List<Msg> mMsgList;

public RecyclerLayoutActivity(List<Msg> 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<Object> 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<RecyclerLayoutActivity.ViewHolder>适配器类,这个类是泛型的类,其数据类型可以外部指定。其中大部分的逻辑运行过程在父类中运行,几个主要要修改的抽象类只需要列出即可,我们需要修改的几个方法就是
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;
    }
}

功能: 存储数据的对象类;
最后运行的结果:
在这里插入图片描述素材:
在这里插入图片描述
在这里插入图片描述

As an AI language model, I can provide some tips on how to make a public speech. 1. Research and Prepare: Start by researching your topic and gathering all the necessary information that you need to deliver a speech. Plan your speech by creating an outline of the main points you want to make. 2. Know your audience: Consider who your audience is and tailor your speech to their interests and knowledge. This will help you to engage them and keep their attention throughout your speech. 3. Be confident: Confidence is key when making a public speech. Practice your speech beforehand and know your material inside out. This will help you to deliver it with poise and clarity. 4. Use body language: Gestures and body language can help to emphasize your points and make your speech more engaging. Use appropriate hand gestures and facial expressions to convey your message. 5. Use visuals: Visual aids such as PowerPoint presentations or props can help to add interest to your speech and make complex ideas easier to understand. 6. Speak clearly and slowly: Make sure to speak clearly and slowly so that your audience can understand what you are saying. Take deep breaths and pause when necessary to maintain your composure. 7. Connect with your audience: Use anecdotes, humor or personal experiences to connect with your audience and build rapport. Remember that a public speech is an opportunity to share knowledge, inspire and motivate others. With thorough preparation and practice, you can give an impactful speech that leaves a lasting impression on your audience.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值