android Listview模拟聊天界面

 更新时间:2017年03月10日 10:56:14   作者:zhu6201976  
这篇文章主要为大家详细介绍了android Listview模拟聊天界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了android Listview模拟聊天界面的具体代码,供大家参考,具体内容如下

代码:

package com.example.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import static com.example.test.R.id.tv_receive;
import static com.example.test.R.id.tv_send;

public class MainActivity extends AppCompatActivity {


  private ArrayList<Msg> msgs;
  private EditText et_input;
  private MyAdapter myAdapter;
  private ListView lv;

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

    lv = (ListView) findViewById(R.id.lv);
    et_input = (EditText) findViewById(R.id.et_input);

    findViewById(R.id.bt_send).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        String content = et_input.getText().toString();
        if (!content.isEmpty()) {
          msgs.add(new Msg(content, Msg.TYPE_SEND));
          myAdapter.notifyDataSetChanged();
          lv.setSelection(msgs.size() - 1);
          et_input.setText("");
        } else {
          Toast.makeText(MainActivity.this, "请输入内容!", Toast.LENGTH_SHORT).show();
        }
      }
    });

    msgs = new ArrayList<>();
    msgs.add(new Msg("hello", Msg.TYPE_RECEIVE));
    msgs.add(new Msg("who is that?", Msg.TYPE_SEND));
    msgs.add(new Msg("this is LiLei,nice to meet you!", Msg.TYPE_RECEIVE));

    myAdapter = new MyAdapter();
    lv.setAdapter(myAdapter);

  }

  private class MyAdapter extends BaseAdapter {

    @Override
    public int getCount() {
      return msgs.size();
    }

    @Override
    public Msg getItem(int position) {
      return msgs.get(position);
    }

    @Override
    public long getItemId(int position) {
      return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder;
      if (convertView == null) {
        holder = new ViewHolder();
        convertView = View.inflate(getApplicationContext(), R.layout.listview_item, null);
        holder.tv_receive = (TextView) convertView.findViewById(tv_receive);
        holder.tv_send = (TextView) convertView.findViewById(tv_send);
        convertView.setTag(holder);
      } else {
        holder = (ViewHolder) convertView.getTag();
      }
      Msg msg = getItem(position);
      if (msg.type == Msg.TYPE_RECEIVE) {
        holder.tv_receive.setVisibility(View.VISIBLE);
        holder.tv_send.setVisibility(View.GONE);
        holder.tv_receive.setText(msg.content);
      } else if (msg.type == Msg.TYPE_SEND) {
        holder.tv_send.setVisibility(View.VISIBLE);
        holder.tv_receive.setVisibility(View.GONE);
        holder.tv_send.setText(msg.content);
      }
      return convertView;
    }
  }

  private static class ViewHolder {
    TextView tv_receive;
    TextView tv_send;
  }
}

package com.example.test;

/**
 * Created by My on 2017/3/3.
 */
class Msg {
  static final int TYPE_RECEIVE = 1;
  static final int TYPE_SEND = 2;
  String content;
  int type;

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

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ccc"
  android:orientation="vertical"
  tools:context="com.example.test.MainActivity">

  <ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:divider="@null" />

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

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

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

<?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="wrap_content"
  android:orientation="vertical"
  android:padding="10dp">

  <TextView
    android:id="@+id/tv_receive"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:background="@drawable/message_left"
    android:gravity="center"
    android:text="who?"
    android:textSize="20sp" />

  <TextView
    android:id="@+id/tv_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:background="@drawable/message_right"
    android:gravity="center"
    android:text="i am your father"
    android:textSize="20sp" />


</LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android 上下滚动TextSwitcher实例详解

    Android 上下滚动TextSwitcher实例详解

    这篇文章主要介绍了Android 上下滚动TextSwitcher实例详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • 在Visual Studio上构建C++的GUI框架wxWidgets的开发环境

    在Visual Studio上构建C++的GUI框架wxWidgets的开发环境

    这篇文章主要介绍了Visual Studio上构件C++的GUI框架wxWidgets开发环境的方法,wxWidgets是一个跨多个系统平台的图形化界面开发框架,并且可用语言不限于C++,需要的朋友可以参考下
    2016-04-04
  • Android持久化保存cookie的方法

    Android持久化保存cookie的方法

    这篇文章主要介绍了Android持久化保存cookie的方法,在解析网页信息的时候,需要登录后才能访问,所以使用httpclient模拟登录,然后把cookie保存下来,以供下一次访问使用,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)

    Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)

    本文主要介绍Fragment+FragmentTabHost组件实现常见主页面,这里整理了详细资料及简单示例代码,有兴趣的小伙伴可以参考下
    2016-09-09
  • Android自定义WheelView地区选择三级联动

    Android自定义WheelView地区选择三级联动

    这篇文章主要为大家详细介绍了Android自定义WheelView地区选择三级联动的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • Android Handler leak分析及解决办法详解

    Android Handler leak分析及解决办法详解

    这篇文章主要介绍了Android Handler leak分析及解决办法详解的相关资料,需要的朋友可以参考下
    2017-03-03
  • Android APK反编译图文教程

    Android APK反编译图文教程

    学会反编译比较关键,也是我们美化必须掌握技术,学会反编译也是实现制作ROM的起步,ROM高手必然是反编译高手这里有必要说一下,教程只是给你一个动手的那一个跷板,教程不是万能的,给了你基础与启发,最重要的是我们能够自主的进行创新与思考
    2016-04-04
  • Android实现带有指示器的自定义底部导航栏

    Android实现带有指示器的自定义底部导航栏

    这篇文章主要为大家详细介绍了Android实现带有指示器的自定义底部导航栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • Android中dataBinding使用的简单封装

    Android中dataBinding使用的简单封装

    前面一段时间学习了一下Android中的DataBinding,但是只是很简单地实现了一下,DataBinding中最强大的地方还没有认真地学习过,有很多地方还不理解,下面这篇文章主要给大家介绍了关于Android中dataBinding使用的简单封装,需要的朋友可以参考下
    2023-06-06
  • android中ProgressDialog与ProgressBar的使用详解

    android中ProgressDialog与ProgressBar的使用详解

    本篇文章是对android中ProgressDialog与ProgressBar的使用进行了详细的分析介绍,需要的朋友参考下
    2013-06-06

最新评论