android实现简单模拟聊天界面

1、先看效果

android实现简单模拟聊天界面_第1张图片

2、布局代码:




    

    

        

        

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 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 {
    private List msgList;

    public MsgAdapter(List 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 布局:




    

        

    

    

        

    

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

你可能感兴趣的:(Android开发)