android实现简单模拟聊天界面

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/I123456789T/article/details/89352347

1、先看效果

2、布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/circle_edit"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/et_content"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="请输入内容"/>

        <Button
            android:id="@+id/btn_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发送"/>

    </LinearLayout>

</LinearLayout>

3、activity:

package com.example.weiwenyi.androidtest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.weiwenyi.androidtest.adapter.MsgAdapter;
import com.example.weiwenyi.androidtest.bean.Msg;
import com.example.weiwenyi.androidtest.service.PassengerService;

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

/**
 * Created by weiwenyi on 2018/3/26.
 */

public class TestActivity extends AppCompatActivity {

    private RecyclerView circleRecycle;
    private EditText inputEdit;
    private Button btn_send;
    private List<Msg> msgList = new ArrayList<>();
    private MsgAdapter msgAdapter;
    private LocalReceiver localReceiver;
    private LocalBroadcastManager localBroadcastManager;

    BroadcastMain receiver;
    //内部类,实现BroadcastReceiver
    public class BroadcastMain extends BroadcastReceiver{
        //必须要重载的方法,用来监听是否有广播发送
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("brocast","------> 接收到消息 ");
//            Toast.makeText(Create9FileActivity.this, intent.getStringExtra("msg"), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_9file);

        Intent intent = new Intent();
        intent.setClass(this, PassengerService.class);
        startService(intent);

        circleRecycle = (RecyclerView) findViewById(R.id.circle_edit);
        inputEdit = (EditText) findViewById(R.id.et_content);
        btn_send = (Button) findViewById(R.id.btn_send);

        // 初始化消息数据
        initMsgs();

        receiver = new BroadcastMain();
        //新添代码,在代码中注册广播接收程序
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.goods.updata");
        registerReceiver(receiver, filter);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        circleRecycle.setLayoutManager(layoutManager);
        msgAdapter = new MsgAdapter(msgList);
        circleRecycle.setAdapter(msgAdapter);

        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = inputEdit.getText().toString();
                if (!"".equals(content)){
                    Msg msg = new Msg(content,Msg.TYPE_SEND);
                    msgList.add(msg);
                    msgAdapter.notifyItemInserted(msgList.size() - 1);// 当有新消息时,刷新适配器
                    circleRecycle.scrollToPosition(msgList.size() - 1);// 将circleRecycle 定位到最后一行
                    inputEdit.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_SEND);
        msgList.add(msg2);
        Msg msg3 = new Msg("This is Tom. Nice talking to you.",Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }

    private class LocalReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("brocast","-----> 接收");
            Toast.makeText(Create9FileActivity.this,"收到本地广播",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Intent intent = new Intent();
        intent.setClass(this, PassengerService.class);
        stopService(intent);
    }
}

4、实体类Msg:

package com.example.weiwenyi.androidtest.bean;

/**
 * Created by weiwenyi on 2018/3/26.
 */

public class Msg {
    public static final int TYPE_RECEIVED = 0;
    public static final int TYPE_SEND = 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 void setContent(String content) {
        this.content = content;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
}

5、adapter

package com.example.weiwenyi.androidtest.adapter;

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 com.example.weiwenyi.androidtest.R;
import com.example.weiwenyi.androidtest.bean.Msg;

import java.util.List;

/**
 * Created by weiwenyi on 2018/3/26.
 */

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
    private List<Msg> msgList;

    public MsgAdapter(List<Msg> msgList) {
        this.msgList = msgList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Msg msg = msgList.get(position);
        if (msg.getType() == Msg.TYPE_RECEIVED){
            // 如果收到消息,则显示在左边的消息布局,将右边的隐藏
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLauout.setVisibility(View.GONE);
            holder.leftMsg.setText(msg.getContent());

        }else if (msg.getType() == Msg.TYPE_SEND){
            // 如果发送消息,则显示在右边的消息布局,将左边的隐藏
            holder.rightLauout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightMsg.setText(msg.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return msgList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout leftLayout,rightLauout;
        TextView leftMsg,rightMsg;
        public ViewHolder(View itemView) {
            super(itemView);
            leftLayout = itemView.findViewById(R.id.left_layout);
            rightLauout = itemView.findViewById(R.id.right_layout);
            leftMsg = itemView.findViewById(R.id.left_msg);
            rightMsg = itemView.findViewById(R.id.right_msg);
        }
    }
}

6、item 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <LinearLayout
        android:id="@+id/left_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#e1e1e1"
        android:layout_gravity="left">

        <TextView
            android:id="@+id/left_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:textColor="#fff"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/right_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="#918e8e">

        <TextView
            android:id="@+id/right_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:textColor="#fff"/>

    </LinearLayout>

</LinearLayout>

7、运行看看,这是本地的,,简单的界面效果,

猜你喜欢

转载自blog.csdn.net/I123456789T/article/details/89352347