Chat ListView,

Source: Internet
Author: User

Chat ListView,

We know that there will be at least two la s when chatting with QQ, that is, the received message and the message sent by ourselves. This effect can be achieved using listView. Similar to the following interface.

It is mainly written in getView () of the Adapter.

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 login /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 ();}

// You must Override this method. Otherwise, only the layout @ Override public int getViewTypeCount () {// TODO Auto-generated method stub return 2;} appears ;} @ 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) {// message 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 ;}}

Pass

@ Override

Public int getItemViewType (int position ){

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

To determine which layout to instantiate,

Pass
@ Override
Public int getViewTypeCount () {// TODO Auto-generated method stub return 2 ;}
To determine the number of layout types.

Chat is a java class that encapsulates chat content:
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;    }}
 

 

The following is the code for two la s:
1,
List_chat_out: Message sending Layout
 
<?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:Message receiving Layout
<?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>

 

 

 
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.