关闭

聊天布局(listview)

160人阅读 评论(0) 收藏 举报
分类:

实体类Msg

package com.example.entity;

public class Msg {
    public static final int other = 0;
    public static final int me = 1;
    String content;
    int type;

    public Msg(int type,String content) {
        this.type=type;
        this.content=content;
    }

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

}

activity_main.xml

<LinearLayout
    android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <ListView 
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_weight="9"
        android:layout_height="0dp"
        android:divider="#000"
        ></ListView>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_gravity="bottom">

        <EditText 
            android:layout_width="0dp"
            android:layout_weight="8"
            android:layout_height="match_parent"
            android:id="@+id/content"/>
        <Button 
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"
            android:id="@+id/send"
            android:layout_gravity="right"
            android:text="send"/>

    </LinearLayout>


</LinearLayout>




msg_listview.xml

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

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:id="@+id/leftlayout"
        android:layout_gravity="left">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/lefttext"
            android:layout_gravity="left"/>

    </LinearLayout>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:id="@+id/rightlayout"
        android:gravity="right">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/righttext"
            android:layout_gravity="right"/>

    </LinearLayout>


</LinearLayout>

MainActivity

    List<Msg> msg;
    MsgAdapter msgAdapter;
    ListView listview;
{
 listview =(ListView) this.findViewById(R.id.listview);
        msgAdapter = new MsgAdapter(this, R.layout.msg_item, msg);
        listview.setAdapter(msgAdapter);

        Button btn_send =(Button) this.findViewById(R.id.send);
        btn_send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                EditText edContent =(EditText) MainActivity.this.findViewById(R.id.content);
                String content =edContent.getText().toString();
                Msg msg0=new Msg(Msg.me,content);
                msg.add(msg0);
                msgAdapter.notifyDataSetChanged();//刷新页面
                listview.setSelection(msg.size());//定位到最后一行
                edContent.setText("");
            }
        });

}

private void initMsgListView(){
        msg =new ArrayList<Msg>();
        Msg msg1=new Msg(Msg.me, "hello");
        msg.add(msg1);
        Msg msg2=new Msg(Msg.other, "ok");
        msg.add(msg2);
    }

MsgAdapter

package com.example.adapter;

import java.util.List;

import com.example.adapter.FruitAdapter.ViewHolder;
import com.example.entity.Msg;
import com.example.testsometing.R;

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;

public class MsgAdapter extends ArrayAdapter<Msg>{
    int resourceId;
    View view;
    MsgViewHolder viewHolder;

    public class MsgViewHolder{
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftText;
        TextView rightText;
    }

    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) {

        if(convertView==null){
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder =new MsgViewHolder();
            viewHolder.leftLayout=(LinearLayout) view.findViewById(R.id.leftlayout);
            viewHolder.rightLayout=(LinearLayout) view.findViewById(R.id.rightlayout);
            viewHolder.leftText=(TextView) view.findViewById(R.id.lefttext);
            viewHolder.rightText=(TextView) view.findViewById(R.id.righttext);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (MsgViewHolder) view.getTag();
        }
        Msg msg =getItem(position);
        if(msg.getType()==Msg.me){
            viewHolder.leftLayout.setVisibility(View.GONE);
            viewHolder.rightLayout.setVisibility(View.VISIBLE);
            viewHolder.rightText.setText(msg.getContent());
        }else if(msg.getType()==Msg.other){
            viewHolder.rightLayout.setVisibility(View.GONE);
            viewHolder.leftLayout.setVisibility(View.VISIBLE);
            viewHolder.leftText.setText(msg.getContent());
        }

        return view;
    }

}

这里写图片描述

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:33472次
    • 积分:787
    • 等级:
    • 排名:千里之外
    • 原创:26篇
    • 转载:82篇
    • 译文:0篇
    • 评论:0条