资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

androidListview模拟聊天界面

本文实例为大家分享了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 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"?>


  

  

    

    
<?xml version="1.0" encoding="utf-8"?>


  

  




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


网页题目:androidListview模拟聊天界面
分享地址:http://cdkjz.cn/article/gosoid.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220