android:ListView bbs Demo

我们制 作的 message_left.9.png 可以作为收到消息的背景图,那么毫无疑问你还需要再制作一张 message_right.9.png 作为发出消息的背景图。

图片都提供好了之后就可以开始编码了,首先还是编写主界面,修改 activity_main.xml

中的代码,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d8e0e8"
    android:orientation="vertical" >

    <ListView
        android1:id="@+id/msg_list_view"
        android1:layout_width="match_parent"
        android1:layout_height="0dp"
        android:layout_weight="1"
        android:divider="#0000"
        >
    </ListView>

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

        <EditText
android:id="@+id/input_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type somthing here"
android:maxLines="2" />

        <Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />

    </LinearLayout>

</LinearLayout>

  

这里在主界面中放置了一个 ListView 用于显示聊天的消息内容,又放置了一个 EditText 用于输入消息,还放置了一个 Button 用于发送消息。ListView 中用到了一个 android:divider 属性,它可以指定 ListView 分隔线的颜色,这里#0000 表示将分隔线设为透明色。其他用到 的所有属性都是我们之前学过的,相信你理解起来应该不费力。

然后我们来定义消息的实体类,新建 Msg,代码如下所示:

package com.example.uibestpractice;

//defined message class
public class Msg {

    //message content
    private String content;
    //message type
    private int type;

    //defined message type value
    public static final int TYPE_RECEIVE = 0;
    public static final int TYPE_SEND = 1;

    //defined constructor
    public Msg(String content , int type){

        this.content = content;
        this.type = type;

    }

    //function use to getcontent
    public String getContent(){

        return content;

    }

    //function use to gettype
    public int getType(){

        return type;
    }

}

Msg 类中只有两个字段,content 表示消息的内容,type 表示消息的类型。其中消息类型 有两个值可选,TYPE_RECEIVED 表示这是一条收到的消息,TYPE_SENT 表示这是一条发 出的消息。

接着来编写 ListView 子项的布局,新建 msg_item.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:padding="10dp" >

    <LinearLayout
        android:id="@+id/left_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:background="@drawable/message_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="@drawable/message_right" >

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

  

这里我们让收到的消息居左对齐,发出的消息居右对齐,并且分别使用 message_left.9.png

和 message_right.9.png 作为背景图。你可能会有些疑虑,怎么能让收到的消息和发出的消息 都放在同一个布局里呢?不用担心,还记得我们前面学过的可见属性吗,只要稍后在代码中 根据消息的类型来决定隐藏和显示哪种消息就可以了。

接下来需要创建 ListView 的适配器类,让它继承自 ArrayAdapter,并将泛型指定为 Msg

类。新建类 MsgAdapter,代码如下所示:

package com.example.uibestpractice;

import java.util.List;

import android.content.Context;
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> {

    private int resourceId;

    //context,listview sub item layoutid, listview data
    public MsgAdapter(Context context, int textViewResourceId,
            List<Msg> objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub

        resourceId = textViewResourceId;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        //return super.getView(position, convertView, parent);

        //get current item Msg
        Msg msg = getItem(position);

        //through LayoutInflater for current sub item  load layout;
        if(convertView == null){

            convertView = LayoutInflater.from(getContext()).inflate(resourceId, parent,false);

        }

        //though viewhoder model to optimization listview

        //get layout object
        LinearLayout leftLinearLayout = ViewHolder.get(convertView, R.id.left_layout);
        LinearLayout rightLinearLayout = ViewHolder.get(convertView, R.id.right_layout);

        //though msg object type set linearlayout is visible
        if(msg.getType() == Msg.TYPE_RECEIVE){

            //this is receive message , show left linearlayout and hide right linearlayout
            leftLinearLayout.setVisibility(View.VISIBLE);
            rightLinearLayout.setVisibility(View.GONE);

            //show receive message content
            TextView leftTextView = ViewHolder.get(convertView, R.id.left_msg);
            leftTextView.setText(msg.getContent());

        }
        else if(msg.getType() == msg.TYPE_SEND){

            //this is send message , show right linearlayout and hide right linearlayout
            leftLinearLayout.setVisibility(View.GONE);
            rightLinearLayout.setVisibility(View.VISIBLE);

            //show receive message content
            TextView rightTextView = ViewHolder.get(convertView, R.id.right_msg);
            rightTextView.setText(msg.getContent());

        }

        return convertView;

    }

}

以上代码你应该是非常熟悉了,和我们学习 ListView 那一节的代码基本是一样的,只不 过在 getView()方法中增加了对消息类型的判断。如果这条消息是收到的,则显示左边的消 息布局,如果这条消息是发出的,则显示右边的消息布局。

最后修改 MainActivity 中的代码,来为 ListView 初始化一些数据,并给发送按钮加入事 件响应,代码如下所示:

package com.example.uibestpractice;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

    private List<Msg> msgList = new ArrayList<Msg>();

    private MsgAdapter msgAdapter ;

    //defined message edittext control
    private EditText editText;

    //defined send button control
    private Button sendButton;

    //defined listview control;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //hide app title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        //init message data
        initMsgs();

        //init MsgAdapter
        msgAdapter = new MsgAdapter(MainActivity.this, R.layout.msg_item, msgList);

        //get layout control
        editText = (EditText)findViewById(R.id.input_text);
        sendButton = (Button)findViewById(R.id.send);
        listView = (ListView)findViewById(R.id.msg_list_view);

        //give listview control set adapter;
        listView.setAdapter(msgAdapter);

        //set sendbutton onclick event
        sendButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                //set listview control to show input message content

                String content = editText.getText().toString();

                //if content is not null
                if(content.length() > 0){

                    //init msg object
                    Msg msg = new Msg(content, Msg.TYPE_SEND);

                    //add msg object to msglist data
                    msgList.add(msg);

                    //if new message is send, refresh listview control
                    msgAdapter.notifyDataSetChanged();

                    //position to the listview last line
                    listView.setSelection(msgList.size());

                    //clear input text content
                    editText.setText("");

                }

            }
        });

    }

    //init message data
    private void initMsgs(){

        Msg msg1 = new Msg("Hello guy.", Msg.TYPE_RECEIVE);
        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_RECEIVE);
        msgList.add(msg3);

    }

}

在 initMsgs()方法中我们先初始化了几条数据用于在 ListView 中显示。然后在发送按钮

的点击事件里获取了 EditText 中的内容,如果内容不为空则创建出一个新的 Msg 对象,并把 它添加到 msgList 列表中去。之后又调用了适配器的 notifyDataSetChanged()方法,用于通知 列表的数据发生了变化,这样新增的一条消息才能够在 ListView 中显示。接着调用 ListView 的 setSelection()方法将显示的数据定位到最后一行,以保证一定可以看得到最后发出的一条 消息。最后调用 EditText 的 setText()方法将输入的内容清空。

这样所有的工作就都完成了,终于可以检验一下我们的成果了,运行程序之后你将会看 到非常美观的聊天界面,并且可以输入和发送消息,如图 3.43 所示。

时间: 2024-12-12 15:59:45

android:ListView bbs Demo的相关文章

Android ListView分页加载(服务端+android端)Demo

Android ListView分页加载功能 在实际开发中经常用到,是每个开发者必须掌握的内容,本Demo给出了服务端+Android端的两者的代码,并成功通过了测试. 服务端使用MyEclipse,Android端使用Eclipse. 实现效果图: 服务端一共100条数据,共分四页,每页有25条数据. 源代码: 服务端: 需要导入图中这几个jar包. 在运行Android端代码前,需要开启服务端: 下面先给出服务端的代码: 类EmpDataSource: package com.android

Android ListView分页载入(服务端+android端)Demo

Android ListView分页载入功能 在实际开发中经经常使用到,是每一个开发人员必须掌握的内容,本Demo给出了服务端+Android端的两者的代码,并成功通过了測试. 服务端使用MyEclipse,Android端使用Eclipse. 实现效果图: 服务端一共100条数据,共分四页,每页有25条数据. 源码: 服务端: 须要导入图中这几个jar包. 在执行Android端代码前,须要开启服务端: 以下先给出服务端的代码: 类EmpDataSource: package com.andr

android listview级联三菜单选择地区,本地数据库sqlite级联地区,item选中不变色

前言:因为找了N多网上的资源都没有好的解决方案,别人都是只给思路没给具体源码,真TMD纠结,干嘛求别人,自己动手才是真,最痛恨那些所谓大牛的作风,给了点点代码就让别人去想,你让我们这种小白情何于堪!!!!!!此例是基于listview来实现本地sqlite实现的! 二话不说,程序猿求的是有图有真相有源码!大家下载后有什么问题可以找到本人:QQ508181017 核心代码如下 1.数据库操作类 package com.icq.demo.db; import java.util.ArrayList;

Android ExpandableListView实例Demo

前几篇文章介绍了Listview,但在实际开发中也经常会用到多层的Listview来展示数据,比如qq中的好友展示,所以这张来了解一下ExpandableListview,基本思想与Listview大致是相同的,所以用起来会比较方便. 实现效果图: 程序代码: 布局文件: activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools

Android ListFragment实例Demo(自定义适配器)

上一篇文章介绍了ListFragment,其中的ListView并没有自定义适配器,实际上在实际开发中常会用到自定义适配器,是实现更复杂的列表数据展示.所以这篇文章增加了自定义适配器,来进行ListView数据的展示. 实现效果图: 左边是Activity中的一个按钮,点击按钮会出现右边的Fragment相应的数据列表. 代码展示: 布局文件: activity_main: <LinearLayout xmlns:android="http://schemas.android.com/ap

android Listview分批加载+自动加载(附源码下载)

直接上代码,代码有注释: public class TestForListviewActivity extends Activity implements OnScrollListener { private ListView mListview = null; private View mFooterView; private PaginationAdapter mAdapter; private Handler handler=new Handler(); private boolean i

Android ListView 和 Adapter 从本地/网络获取歌曲列表

本文内容 环境 项目结构 演示1:SimpleAdapter 演示2:BaseAdapter 演示3:customlazylist 演示4:customcompletelazylist 本文只给出演示概要,代码太多,贴出来意义不大,自己下载调试一下,点击此处下载. 本文通过四个示例,循序渐进地演示,将歌曲列表加载到 ListView 控件,歌曲列表,包括缩略图.歌手名.歌曲名等信息,或存放在本地,或以 JSON 形式存放在网络. 环境 Windows 2008 R2 64 位 Eclipse A

android listview 异步加载图片并防止错位

网上找了一张图, listview 异步加载图片之所以错位的根本原因是重用了 convertView 且有异步操作. 如果不重用 convertView 不会出现错位现象, 重用 convertView 但没有异步操作也不会有问题. 我简单分析一下: 当重用 convertView 时,最初一屏显示 7 条记录, getView 被调用 7 次,创建了 7 个 convertView. 当 Item1 划出屏幕, Item8 进入屏幕时,这时没有为 Item8 创建新的 view 实例, Ite

Android ListFragment实例Demo

该篇文章是一个ListFragment的一个实例,通过了解该实例,更能了解比较常用的ListFragment的用法,以及各Fragment之间的数据传递. 实现效果图: 该MainActivity中包括1个Button+2个Fragment(右边两个),点击Button,出现中间的list列表,点击列表中的任一项,相应item数值,会传递到右边的Fragment中并显示. 源代码: activity_main: <LinearLayout xmlns:android="http://sch