基础教程之Android 漂亮聊天APP实现

  • 内容
  • 评论
  • 相关

本例主要是为了讲解ListView用法,仅是个demo

MainActivity.java

package com.gaoxueping.chat3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    List<Msg> msgList = new ArrayList<Msg>();
    private EditText inputMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initDatas();
        final ListView listView = (ListView) findViewById(R.id.list_view);
        final MsgAdapter msgAdapter = new MsgAdapter(MainActivity.this, R.layout.msg_item, msgList);
        listView.setAdapter(msgAdapter);

        Button buttonSent = (Button) findViewById(R.id.sent);
        inputMsg = (EditText) findViewById(R.id.input);
        buttonSent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = inputMsg.getText().toString();
                if(!"".equals(content)){
                    Msg msg = new Msg(content, Msg.TYPE_SENT);
                    msgList.add(msg);
                    msgAdapter.notifyDataSetChanged();
                    listView.setSelection(msgList.size());
                    inputMsg.setText("");
                }
            }
        });
    }

    public void initDatas(){
        String[] originalMsg = {"hello i am alan", "may i help you", "no thanks, dear"};
        for(int i = 0; i < originalMsg.length; i++){
            Msg msg = new Msg(originalMsg[i], i%2);
            msgList.add(msg);
        }
    }
}

MsgAdapter.java

package com.gaoxueping.chat3;

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by hellogxp on 2016/8/21.
 */
public class MsgAdapter extends ArrayAdapter<Msg> {
    public int resourceId;
    private View view;
    private ViewHolder viewHolder;
    public MsgAdapter(Context context, int resource, List<Msg> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Msg msg = getItem(position);
        viewHolder = new ViewHolder();
        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder.layoutLeft = view.findViewById(R.id.layout_left);
            viewHolder.layoutRight = view.findViewById(R.id.layout_right);
            viewHolder.msgLeft = (TextView) viewHolder.layoutLeft.findViewById(R.id.msg_left);
            viewHolder.msgRight = (TextView) viewHolder.layoutRight.findViewById(R.id.msg_right);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        if(msg.getType() == Msg.TYPE_RECEIVED){
            viewHolder.layoutRight.setVisibility(View.GONE);
            viewHolder.layoutLeft.setVisibility(View.VISIBLE);
            viewHolder.msgLeft.setText(msg.getContent());
        }else if(msg.getType() == Msg.TYPE_SENT){
            viewHolder.layoutRight.setVisibility(View.VISIBLE);
            viewHolder.layoutLeft.setVisibility(View.GONE);
            viewHolder.msgRight.setText(msg.getContent());

        }
        return view;
    }

    class ViewHolder{
        private View layoutLeft, layoutRight;
        private TextView msgLeft, msgRight;
    }
}

msg_item.xml

<?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">

    <LinearLayout
        android:layout_gravity="left"
        android:id="@+id/layout_left"
        android:gravity="center"
        android:background="@drawable/incoming"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:gravity="center"
            android:id="@+id/msg_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:layout_gravity="right"
        android:id="@+id/layout_right"
        android:gravity="center"
        android:background="@drawable/outgoing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:gravity="center"
            android:id="@+id/msg_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

完整实例下载

http://pan.baidu.com/s/1dE1wS3f

评论

0条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注