聊天ListView

来源:http://www.cnblogs.com/tangZH/archive/2017/11/12/7821432.html
-Advertisement-
Play Games

我們知道,在微信或者QQ聊天的時候,會出現至少兩種佈局,即收到的消息和自己發送的消息,這種效果可以用listView來實現。類似於下麵這樣的界面。 主要在Adapter的getView()裡面下筆。 通過 @Override public int getItemViewType(int positi ...


我們知道,在微信或者QQ聊天的時候,會出現至少兩種佈局,即收到的消息和自己發送的消息,這種效果可以用listView來實現。類似於下麵這樣的界面。

主要在Adapter的getView()裡面下筆。

package com.example.chatting.chatting.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.chatting.chatting.R;
import com.example.chatting.chatting.bean.Chat;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by Administrator on 2017/11/9.
 */
public class ChattingAdapter extends BaseAdapter{

    private List<Chat> list;
    private LayoutInflater inflater;
    private Context mContext;
//    private Drawable mDefaultHeadImage;

    public ChattingAdapter(List<Chat> list, Context context){
        this.list = list;
        inflater = LayoutInflater.from(context);
        mContext = context;
//        mDefaultHeadImage = mContext.getResources().getDrawable(R.drawable.image_head);
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

//一定得重寫該方法,否則只會出現一種佈局 @Override
public int getViewTypeCount() { // TODO Auto-generated method stub return 2; } @Override public View getView(int position, View convertView, ViewGroup parent) { Chat chat = (Chat) getItem(position); int type = getItemViewType(position); ViewHolder viewHolder; if(convertView == null){ if(type == 0) { // 我說的消息 System.out.println("****type0"); viewHolder = new ViewHolder(); convertView = inflater.inflate(R.layout.list_chat_out, null); viewHolder.tvName = (TextView)convertView.findViewById(R.id.tv_name_out); viewHolder.tvMsg = (TextView)convertView.findViewById(R.id.tv_msg_out); viewHolder.headImage = (ImageView)convertView.findViewById(R.id.image_head_out); }else{ System.out.println("****type1"); viewHolder = new ViewHolder(); convertView = inflater.inflate(R.layout.list_chat_in, null); viewHolder.tvName = (TextView)convertView.findViewById(R.id.tv_name_in); viewHolder.tvMsg = (TextView)convertView.findViewById(R.id.tv_msg_in); viewHolder.headImage = (ImageView)convertView.findViewById(R.id.image_head_in); } convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder)convertView.getTag(); } viewHolder.tvName.setText(chat.getNickName()); viewHolder.tvMsg.setText(chat.getMessage()); return convertView; } private class ViewHolder{ public TextView tvName, tvMsg; public ImageView headImage; } }

  通過

  @Override

    public int getItemViewType(int position) {

        return list.get(position).getType();
    }

來決定實例化哪個佈局,

通過
@Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 2;
    }
來決定佈局的類型個數。

chat為封裝聊天內容的java類:
package com.example.chatting.chatting.bean;

import java.io.Serializable;

/**
 * Created by Administrator on 2017/11/9.
 */
public class Chat implements Serializable{
    private String message;
    private int type;
    private String NickName;
    private String imgURL;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getNickName() {
        return NickName;
    }

    public void setNickName(String nickName) {
        NickName = nickName;
    }

    public String getImgURL() {
        return imgURL;
    }

    public void setImgURL(String imgURL) {
        this.imgURL = imgURL;
    }
}

 

 接下來是兩個佈局的代碼:
1、
list_chat_out:發送消息的佈局


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="80dp" >
        
      <ImageView
          android:id="@+id/image_head_out"
          android:layout_width="45dp"
          android:layout_height="45dp"
          android:src="@mipmap/me_press"
          android:layout_alignParentRight="true"/>
       
      <TextView
        android:id="@+id/tv_name_out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/image_head_out"
        android:layout_alignTop="@id/image_head_out"
        android:layout_marginRight="5dp"
        android:textColor="@color/colorTheme"
        android:textSize="14sp"
        android:text="name"/>
      
      <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="40dp"
        android:layout_toLeftOf="@id/image_head_out"
        android:layout_below="@id/tv_name_out"
        android:layout_marginRight="10dp"
        android:background="@color/colorTheme"
        android:gravity="center_vertical">
        <TextView
            android:id="@+id/tv_msg_out"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:text="content"/>
       </LinearLayout>

</RelativeLayout>
2、list_chat_in:接收消息的佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="80dp" >
        
      <ImageView
        android:id="@+id/image_head_in"
        android:layout_width="45dp"
        android:layout_height="45dp"
          android:src="@mipmap/me_press"
          />
       
      <TextView
          android:id="@+id/tv_name_in"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image_head_in"
        android:layout_alignTop="@id/image_head_in"
        android:layout_marginLeft="5dp"
        android:textColor="@color/colorTheme"
        android:textSize="14sp"
        android:text="name"/>
      
      <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="40dp"
        android:layout_toRightOf="@id/image_head_in"
        android:layout_below="@id/tv_name_in"
        android:layout_marginLeft="10dp"
        android:background="@color/colorGray"
        android:gravity="center_vertical">
        <TextView
            android:id="@+id/tv_msg_in"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:layout_marginLeft="15dp"
            android:textColor="@color/colorBlack"
            android:textSize="18sp"
            android:text="content"/>
       </LinearLayout>

</RelativeLayout>

 

 

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Hello Android 代碼 備註 //創建對象 //設置對話框標題 //設置圖標 //設置內容 //設置點擊返回鍵對話框不消失 //顯示對話框 控制項id.setOnClickListener{ ​ ///事件內容 } 對話框對象AlertDialog 對話框引用android.support. ...
  • Ps:7-10月 完成公司兩個app項目上架。漏掉的總結 開始慢慢補上。 一、寫一個Activity的管理類 1、單例模式,以棧(先進後出)的形式存儲Activity對象 2、給AppManager管理類添加幾個常用方法。 (1)、添加Activity對象 (2)、結束當前Activity對象,即棧 ...
  • 轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/78113868 本文出自 "【趙彥軍的博客】" 一:概述 如果不瞭解插件開發基礎的同學可以先看, "Android Studio 插件開發詳解一:入門練手" "Android Stud ...
  • 更新iOS11後,發現有些地方需要做適配,整理後按照優先順序分為以下三類: 1.單純升級iOS11後造成的變化; 2.Xcode9 打包後造成的變化; 3.iPhoneX的適配 一、單純升級iOS11後造成的變化 1. 升級後,發現某個擁有tableView的界面錯亂,組間距和contentInset ...
  • 比較基礎的一個方法。即繪製文本 使用如下: 效果; 可以看下方法: 這篇只討論第三個方法。 可以看到 4個參數,第二個、第三個參數 是float類型,實際上就是 繪製的文本的繪製參考坐標。註意這個坐標 不是文本的左上角的那個點,float y 代表 基線的Y位置。 驗證一下: 繪製一條藍色的基線,繪 ...
  • 什麼是RAC? 其實RAC就是一個簡化代碼的第三方庫ReactiveCocoa,直接用Cocoapods添加到項目里就可以, podfile 文件添加 pod 'ReactiveCocoa',然後終端 pod install ,頭文件引用: 使用RAC 1.方法 RAC最簡單的使用技巧就是對事件的監 ...
  • 自己離線打包Html5+ Runtime,通常是導入SDK的Hello實例,然後修改。在做消息通知功能時,使用push.createMessage不起作用。 首先參考Android平臺離線打包推送插件配置 需要特別註意的是AndroidManifest的配置,需要將包名替換成自己的包名 ...
  • Runtime是什麼?見名知意,其概念無非就是“因為 Objective-C 是一門動態語言,所以它需要一個運行時系統……這就是 Runtime 系統”云云。對博主這種菜鳥而言,Runtime 在實際開發中,其實就是一組C語言的函數。胡適說:“多研究些問題,少談些主義”,雲山霧罩的概念聽多了總是容易 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...