最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android Listview模擬聊天界面

 更新時(shí)間:2017年03月10日 10:56:14   作者:zhu6201976  
這篇文章主要為大家詳細(xì)介紹了android Listview模擬聊天界面的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了android Listview模擬聊天界面的具體代碼,供大家參考,具體內(nèi)容如下

代碼:

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, "請(qǐng)輸入內(nèi)容!", 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="請(qǐng)輸入要發(fā)送的內(nèi)容" />

    <Button
      android:id="@+id/bt_send"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="發(fā)送" />
  </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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android 上下滾動(dòng)TextSwitcher實(shí)例詳解

    Android 上下滾動(dòng)TextSwitcher實(shí)例詳解

    這篇文章主要介紹了Android 上下滾動(dòng)TextSwitcher實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 在Visual Studio上構(gòu)建C++的GUI框架wxWidgets的開發(fā)環(huán)境

    在Visual Studio上構(gòu)建C++的GUI框架wxWidgets的開發(fā)環(huán)境

    這篇文章主要介紹了Visual Studio上構(gòu)件C++的GUI框架wxWidgets開發(fā)環(huán)境的方法,wxWidgets是一個(gè)跨多個(gè)系統(tǒng)平臺(tái)的圖形化界面開發(fā)框架,并且可用語(yǔ)言不限于C++,需要的朋友可以參考下
    2016-04-04
  • Android持久化保存cookie的方法

    Android持久化保存cookie的方法

    這篇文章主要介紹了Android持久化保存cookie的方法,在解析網(wǎng)頁(yè)信息的時(shí)候,需要登錄后才能訪問(wèn),所以使用httpclient模擬登錄,然后把cookie保存下來(lái),以供下一次訪問(wèn)使用,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android Fragment+FragmentTabHost組件實(shí)現(xiàn)常見(jiàn)主頁(yè)面(仿微信新浪)

    Android Fragment+FragmentTabHost組件實(shí)現(xiàn)常見(jiàn)主頁(yè)面(仿微信新浪)

    本文主要介紹Fragment+FragmentTabHost組件實(shí)現(xiàn)常見(jiàn)主頁(yè)面,這里整理了詳細(xì)資料及簡(jiǎn)單示例代碼,有興趣的小伙伴可以參考下
    2016-09-09
  • Android自定義WheelView地區(qū)選擇三級(jí)聯(lián)動(dòng)

    Android自定義WheelView地區(qū)選擇三級(jí)聯(lián)動(dòng)

    這篇文章主要為大家詳細(xì)介紹了Android自定義WheelView地區(qū)選擇三級(jí)聯(lián)動(dòng)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android Handler leak分析及解決辦法詳解

    Android Handler leak分析及解決辦法詳解

    這篇文章主要介紹了Android Handler leak分析及解決辦法詳解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Android APK反編譯圖文教程

    Android APK反編譯圖文教程

    學(xué)會(huì)反編譯比較關(guān)鍵,也是我們美化必須掌握技術(shù),學(xué)會(huì)反編譯也是實(shí)現(xiàn)制作ROM的起步,ROM高手必然是反編譯高手這里有必要說(shuō)一下,教程只是給你一個(gè)動(dòng)手的那一個(gè)蹺板,教程不是萬(wàn)能的,給了你基礎(chǔ)與啟發(fā),最重要的是我們能夠自主的進(jìn)行創(chuàng)新與思考
    2016-04-04
  • Android實(shí)現(xiàn)帶有指示器的自定義底部導(dǎo)航欄

    Android實(shí)現(xiàn)帶有指示器的自定義底部導(dǎo)航欄

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶有指示器的自定義底部導(dǎo)航欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Android中dataBinding使用的簡(jiǎn)單封裝

    Android中dataBinding使用的簡(jiǎn)單封裝

    前面一段時(shí)間學(xué)習(xí)了一下Android中的DataBinding,但是只是很簡(jiǎn)單地實(shí)現(xiàn)了一下,DataBinding中最強(qiáng)大的地方還沒(méi)有認(rèn)真地學(xué)習(xí)過(guò),有很多地方還不理解,下面這篇文章主要給大家介紹了關(guān)于Android中dataBinding使用的簡(jiǎn)單封裝,需要的朋友可以參考下
    2023-06-06
  • android中ProgressDialog與ProgressBar的使用詳解

    android中ProgressDialog與ProgressBar的使用詳解

    本篇文章是對(duì)android中ProgressDialog與ProgressBar的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06

最新評(píng)論

永年县| 喀喇| 饶平县| 漯河市| 天镇县| 晴隆县| 阳江市| 喀喇| 鄂托克旗| 谷城县| 白城市| 西丰县| 张掖市| 潼南县| 象山县| 铜梁县| 平泉县| 泰州市| 三河市| 沾益县| 资阳市| 台湾省| 长沙县| 永和县| 昭苏县| 桐庐县| 旺苍县| 宁陕县| 阳东县| 星子县| 汕头市| 贺兰县| 德钦县| 卫辉市| 太保市| 闽侯县| 突泉县| 原阳县| 汾西县| 类乌齐县| 十堰市|