91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android Listview模擬聊天界面

發布時間:2020-09-04 17:19:22 來源:腳本之家 閱讀:265 作者: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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

克什克腾旗| 泰和县| 开远市| 黔西| 化州市| 紫阳县| 睢宁县| 普陀区| 柳河县| 天峻县| 永善县| 乐陵市| 楚雄市| 上栗县| 开阳县| 长子县| 夏邑县| 米泉市| 建瓯市| 怀宁县| 井研县| 应用必备| 汝州市| 衡东县| 焉耆| 嵩明县| 南召县| 宁安市| 定安县| 日喀则市| 曲靖市| 梁河县| 灵璧县| 铜山县| 银川市| 尼木县| 洛宁县| 通河县| 东莞市| 太康县| 柞水县|