原CSDN博客已弃用,文章会逐渐迁移过来。

Manifest添加网络访问权限

<uses-permission android:name="android.permission.INTERNET"/>  

string.xml

<resources>  
    <string name="app_name">ImoocDemoProject</string>  
    <string name="xiaoming">小明</string>  
</resources>  

colors.xml

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <color name="colorPrimary">#3F51B5</color>  
    <color name="colorPrimaryDark">#303F9F</color>  
    <color name="colorAccent">#FF4081</color>  
    <color name="blue">#484fd9</color>  
    <color name="main_background">#f0f0f0</color>  
    <color name="white">#FFFFFF</color>  
</resources>  

styles.xml

<resources>  

    <!-- Base application theme. -->  
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
        <!-- Customize your theme here. -->  
        <item name="colorPrimary">@color/colorPrimary</item>  
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>  
        <item name="colorAccent">@color/colorAccent</item>  
    </style>  

    <style name="chat_text_date_style">  
        <item name="android:padding">2dp</item>  
        <item name="android:textColor">#CCCCCC</item>  
        <item name="android:textSize">14sp</item>  
        <item name="android:background">@color/main_background</item>  
    </style>  


    <style name="chat_content_date_style">  
        <item name="android:minHeight">36dp</item>  
        <item name="android:gravity">left|center</item>  
        <item name="android:textColor">#000000</item>  
        <item name="android:textSize">18sp</item>  
        <item name="android:lineSpacingExtra">2dp</item>  
        <item name="android:clickable">true</item>  
        <item name="android:focusable">true</item>  
        <item name="android:background">#bfbfbf</item>  
    </style>  

    <style name="chat_text_name_style">  
        <item name="android:textColor">#818181</item>  
        <item name="android:textSize">15sp</item>  
        <item name="android:gravity">center</item>  
    </style>  

</resources>  

item_main_list.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
              android:orientation="vertical"  
              android:layout_width="match_parent"  
              android:layout_height="match_parent">  
    <TextView  
        android:id="@+id/title"  
        android:layout_width="wrap_content"  
        android:layout_height="60dp"  
        android:text="@string/app_name"  
        android:minHeight="30dp"  
        android:layout_centerVertical="true"  
        android:gravity="center_vertical"  
        android:textColor="#494949"  
        android:layout_marginLeft="16dp"  
        android:layout_toRightOf="@+id/breakfast_imageview"  
        android:textSize="18sp"/>  
</LinearLayout>  

item_demo_list.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
              android:orientation="horizontal"  
              android:layout_width="match_parent"  
              android:layout_height="60dp">  
    <ImageView  
        android:id="@+id/icon_image_view"  
        android:layout_width="60dp"  
        android:layout_height="60dp"  
        android:src="@mipmap/ic_launcher"/>  
    <TextView  
        android:id="@+id/title_text_view"  
        android:layout_width="match_parent"  
        android:layout_height="60dp"  
        android:text="@string/app_name"  
        android:minHeight="30dp"  
        android:layout_centerVertical="true"  
        android:gravity="left|center"  
        android:textColor="#494949"  
        android:singleLine="true"  
        android:layout_marginLeft="16dp"  
        android:layout_toRightOf="@+id/breakfast_imageview"  
        android:textSize="18sp"/>  
</LinearLayout>  

header_list_demo.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
              android:orientation="horizontal"  
              android:layout_width="match_parent"  
              android:layout_height="60dp">  
    <ImageView  
        android:id="@+id/icon_image_view"  
        android:layout_width="match_parent"  
        android:layout_height="80dp"  
        android:background="@drawable/banner"/>  

</LinearLayout>  

chatting_item_msg_text_right.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:orientation="vertical"  
        android:padding="6dp" >  

        <LinearLayout  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:orientation="vertical"   
            android:gravity="center_horizontal">  

            <TextView  
            android:id="@+id/tv_send_time"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:padding="2dp"  
            android:textColor="#ffffff"  
            android:textSize="12sp"  
            android:background="#bfbfbf"/>  

        </LinearLayout>  

        <RelativeLayout  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginTop="5dp"  
            android:layout_marginRight="5dp" >  


               <ImageView   
               android:id="@+id/iv_user_head"  
               android:layout_width="40dp"  
               android:layout_height="40dp"  
               android:focusable="false"   
               android:layout_alignParentRight="true"   
               android:layout_alignParentTop="true"   
               android:background="@drawable/avatar"/>  

                <TextView   
                android:id="@+id/tv_chat_content"  
                android:layout_toLeftOf="@id/iv_user_head"  
                android:layout_marginRight="10dp"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:minWidth="50dp"  
                android:textColor="#FFFFFF"  
                android:background="@drawable/msg_bubble_blue"  
                android:textSize="15sp"  
                android:gravity="left|center"  
                android:minHeight="40dp"  
                android:lineSpacingExtra="2dp"  
                android:clickable="true"  
                android:focusable="true"/>   

                <TextView   
                android:id="@+id/tv_time"   
                android:layout_toLeftOf="@id/tv_chat_content"  
                android:layout_marginRight="10dp"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"        
                android:textSize="15sp"  
                android:textColor="#ff000000"  
                android:gravity="left|center"  
                android:minHeight="50dp"   
                android:lineSpacingExtra="2dp"  
                android:clickable="true"  
                android:focusable="true"/>       


                <TextView   
                android:id="@+id/tv_username"   
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_below="@id/iv_user_head"  
                android:layout_alignParentRight="true"  
                android:layout_toRightOf="@id/tv_chat_content"  
                android:textSize="15sp"  
                android:gravity="center"  
                android:textColor="#818181"/>  

        </RelativeLayout>  

</LinearLayout>  

chatting_item_msg_text_left.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:orientation="vertical"  
    android:padding="6dp" >  

    <LinearLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:gravity="center_horizontal"  
        android:orientation="vertical" >  

        <TextView  
            android:id="@+id/tv_send_time"  
            android:padding="2dp"  
            android:textColor="#CCCCCC"  
            android:text="18:20"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content" />  
    </LinearLayout>  

    <RelativeLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_marginLeft="5dp"  
        android:layout_marginTop="5dp" >  

        <ImageView  
            android:id="@+id/iv_user_head"  
            android:layout_width="40dp"  
            android:layout_height="40dp"  
            android:layout_alignParentLeft="true"  
            android:layout_alignParentTop="true"  
            android:background="@drawable/avatar"  
            android:clickable="true"  
             />  

        <TextView  
            android:id="@+id/tv_chat_content"  
            android:minHeight="36dp"  
            android:gravity="left|center"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="10dp"  
            android:minWidth="50dp"  
            android:textColor="#494949"  
            android:textSize="18sp"  
            android:text="@string/app_name"  
            android:lineSpacingExtra="2dp"  
            android:layout_toRightOf="@id/iv_user_head"  
            android:background="@drawable/msg_bubble_white" />  

        <TextView  
            android:id="@+id/tv_time"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginRight="10dp"  
            android:layout_toRightOf="@id/tv_chat_content"  
            android:clickable="true"  
            android:focusable="true"  
            android:gravity="left|center"  
            android:lineSpacingExtra="2dp"  
            android:minHeight="50dp"  
            android:textColor="#ff000000"  
            android:textSize="15sp" />  

        <TextView  
            android:id="@+id/tv_username"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignParentLeft="true"  
            android:layout_below="@id/iv_user_head"  
            android:text="@string/xiaoming"  
            android:textSize="14sp"  
            android:singleLine="true"  
            android:visibility="visible"  
            android:textColor="#818181"  
            android:gravity="center"  
            android:layout_toLeftOf="@id/tv_chat_content" />  
    </RelativeLayout>  

</LinearLayout>  

activity_main.xml

<?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="match_parent"  
android:paddingBottom="@dimen/activity_vertical_margin"  
android:paddingLeft="@dimen/activity_horizontal_margin"  
android:paddingRight="@dimen/activity_horizontal_margin"  
android:paddingTop="@dimen/activity_vertical_margin">  

<ListView  
    android:id="@+id/list_view_demo"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:background="@color/white"  
    >  
</ListView>  


activity_list_view_demo.xml

<?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="match_parent"  
    android:orientation="vertical"  
    android:background="@color/blue">  


</LinearLayout>  

ChatMessage.java

package com.imooc.demo.model;  

/** 
 * Function: 
 * Create date on 16/8/13. 
 * 
 * @author Conquer 
 * @version 1.0 
 */  
public class ChatMessage {  

    public ChatMessage(int myID, int friendID, String name, String date, String content, String time, String avatarUrl, boolean isComeMessage) {  
        this.myID = myID;  
        this.friendID = friendID;  
        this.name = name;  
        this.date = date;  
        this.content = content;  
        this.time = time;  
        this.avatarUrl = avatarUrl;  
        this.isComeMessage = isComeMessage;  
    }  

    private int myID;  

    private int friendID;  

    private String name;  

    private String date;  

    private String content;  

    private String time;  

    private String avatarUrl;  

    private boolean isComeMessage = true;  


    public String getAvatarUrl() {  
        return avatarUrl;  
    }  

    public void setAvatarUrl(String avatarUrl) {  
        this.avatarUrl = avatarUrl;  
    }  

    public String getTime() {  
        return time;  
    }  

    public void setTime(String time) {  
        this.time = time;  
    }  

    public String getName() {  
        return name;  
    }  

    public void setName(String name) {  
        this.name = name;  
    }  

    public String getDate() {  
        return date;  
    }  

    public void setDate(String date) {  
        this.date = date;  
    }  

    public String getContent() {  
        return content;  
    }  

    public void setContent(String content) {  
        this.content = content;  
    }  

    public boolean getMsgType() {  
        return isComeMessage;  
    }  

    public void setMsgType(boolean isComMsg) {  
        isComeMessage = isComMsg;  
    }  

    public ChatMessage() {  
    }  

    public int getMyID() {  
        return myID;  
    }  

    public void setMyID(int myID) {  
        this.myID = myID;  
    }  

    public int getFriendID() {  
        return friendID;  
    }  

    public void setFriendID(int friendID) {  
        this.friendID = friendID;  
    }  

    public boolean isComeMessage() {  
        return isComeMessage;  
    }  

    public void setComeMessage(boolean isComMeg) {  
        this.isComeMessage = isComMeg;  
    }  
}  

ActivityItem.java

package com.imooc.demo.model;  

/** 
 * Function: 
 * Create date on 16/8/9. 
 * 
 * @author Conquer 
 * @version 1.0 
 */  
public class ActivityItem {  
    private String mName;  
    private Class mActivityClass;  

    public ActivityItem(String name, Class activityClass) {  
        mName = name;  
        mActivityClass = activityClass;  
    }  

    public String getName() {  
        return mName;  
    }  

    public void setName(String name) {  
        mName = name;  
    }  

    public Class getActivityClass() {  
        return mActivityClass;  
    }  

    public void setActivityClass(Class activityClass) {  
        mActivityClass = activityClass;  
    }  
}  

BaseListAdapter.java

package com.imooc.demo.base;  

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

import java.util.List;  

/** 
 * Function: 
 * Create date on 16/8/9. 
 * 
 * @author Conquer 
 * @version 1.0 
 */  
public abstract class BaseListAdapter<T> extends BaseAdapter {  
    private List<T> mList;  
    private final Context mContext;  

    public BaseListAdapter(Context context) {  
        this.mContext = context;  
    }  

    public BaseListAdapter(Context context, List<T> list) {  
        mContext = context;  
        mList = list;  
    }  

    public Context getContext() {  
        return mContext;  
    }  

    @Override  
    public int getCount() {  
        return mList != null ? mList.size() : 0;  
    }  

    @Override  
    public T getItem(int position) {  
        return mList != null ? mList.get(position) : null;  
    }  

    @Override  
    public long getItemId(int position) {  
        return position;  
    }  

    public void setData(List<T> list) {  
        mList = list;  
        notifyDataSetChanged();  
    }  

    public List<T> getList() {  
        return mList;  
    }  


    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        if (convertView == null) {  
            convertView = onNewItemView(LayoutInflater.from(mContext), parent, position);  
        }  

        onBindItemView(convertView, getItem(position), position, parent);  
        return convertView;  
    }  

    protected abstract View onNewItemView(LayoutInflater inflater, ViewGroup parent, int position);  

    protected abstract void onBindItemView(View viewItem, T data, int position, ViewGroup parent);  

    public void notifyDataSetChanged(List<T> mList) {  
        this.mList = mList;  
        notifyDataSetChanged();  
    }  
}  

BindableAdapter.java

package com.imooc.demo.base;  

import android.content.Context;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  

import java.util.List;  

/** 
 * Function: 
 * Create date on 16/8/9. 
 * 
 * @author Conquer 
 * @version 1.0 
 */  
public abstract class BindableAdapter<T> extends BaseListAdapter<T> {  

    public BindableAdapter(Context context) {  
        super(context);  
    }  

    public BindableAdapter(Context context, List<T> list) {  
        super(context, list);  
    }  

    @Override  
    public final View getDropDownView(int position, View view, ViewGroup container) {  
        if (view == null) {  
            view = newDropDownView(LayoutInflater.from(getContext()), position, container);  
            if (view == null) {  
                throw new IllegalStateException("newDropDownView result must not be null.");  
            }  
        }  
        bindDropDownView(getItem(position), position, view, container);  
        return view;  
    }  

    /** 
     * Create a new instance of a drop-down view for the specified position. 
     */  
    public View newDropDownView(LayoutInflater inflater, int position, ViewGroup container) {  
        return onNewItemView(inflater, container, position);  
    }  

    /** 
     * Bind the data for the specified {@code position} to the drop-down view. 
     */  
    public void bindDropDownView(T item, int position, View view, ViewGroup container) {  
        onBindItemView(view, item, position, container);  
    }  
}  

MainActivity.java

package com.imooc.demo.app;  

import android.os.Bundle;  
import android.support.v7.app.AppCompatActivity;  
import android.widget.ListView;  

import com.imooc.demo.R;  
import com.imooc.demo.adapter.MainListAdapter;  
import com.imooc.demo.app.listview.ChatActivity;  
import com.imooc.demo.app.listview.RequestDataActivity;  
import com.imooc.demo.model.ActivityItem;  

import java.util.ArrayList;  

public class MainActivity extends AppCompatActivity {  

    private ListView mListView;  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        mListView = (ListView) findViewById(R.id.list_view_demo);  
        ArrayList<ActivityItem> activityItems = new ArrayList<>();  
        activityItems.add(new ActivityItem("1. 异步请求数据列表", RequestDataActivity.class));  
        activityItems.add(new ActivityItem("2. 模仿IM聊天布局", ChatActivity.class));  
        mListView.setAdapter(new MainListAdapter(MainActivity.this,activityItems ));  
    }  
}  

RequestDataActivity.java

package com.imooc.demo.app.listview;  

import android.content.Context;  
import android.os.AsyncTask;  
import android.os.Bundle;  
import android.support.v7.app.AppCompatActivity;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.ImageView;  
import android.widget.ListView;  
import android.widget.TextView;  

import com.imooc.demo.R;  
import com.imooc.demo.app.model.LessonInfo;  
import com.imooc.demo.app.model.LessonResult;  

import org.json.JSONArray;  
import org.json.JSONException;  
import org.json.JSONObject;  

import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.net.HttpURLConnection;  
import java.net.URL;  
import java.util.ArrayList;  
import java.util.List;  


public class RequestDataActivity  extends AppCompatActivity {  
    private ListView mListView;  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        mListView = (ListView) findViewById(R.id.list_view_demo);  
        LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        View view = layoutInflater.inflate(R.layout.header_list_demo, null);  
        mListView.addFooterView(view);  

        new AppAsyncTask().execute();  
    }  

    public class AppListAdapter extends BaseAdapter {  

        private Context mContext;  
        private List<LessonInfo> mInfos;  

        public AppListAdapter(Context context, List<LessonInfo> infos) {  
            mContext = context;  
            mInfos = infos;  
        }  

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

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

        @Override  
        public long getItemId(int position) {  
            return position;  
        }  

        @Override  
        public View getView(final int position, View convertView, ViewGroup parent) {  
            ViewHolder viewHolder;  
            LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
            if(convertView == null){  
                viewHolder = new ViewHolder();  
                convertView = layoutInflater.inflate(R.layout.item_demo_list, null);  
                // 获取控件  
                viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.title_text_view);  
                viewHolder.avatarImageView = (ImageView) convertView.findViewById(R.id.icon_image_view);  
                convertView.setTag(viewHolder);  
            } else {  
                viewHolder = (ViewHolder) convertView.getTag();  
            }  
            // 和数据之间进行绑定  
            viewHolder.nameTextView.setText(mInfos.get(position).getName());  
            viewHolder.avatarImageView.setVisibility(View.GONE);  

            return convertView;  
        }  

        class ViewHolder {  
            ImageView avatarImageView;  
            TextView nameTextView;  
        }  
    }  



    public class AppAsyncTask extends AsyncTask<Void,Integer, String> {  

        @Override  
        protected String doInBackground(Void... params) {  

            return request("http://www.imooc.com/api/teacher?type=2&page=1");  
        }  

        @Override  
        protected void onPostExecute(String result) {  
            super.onPostExecute(result);  
            LessonResult lessonResult = new LessonResult();  
            try {  
                JSONObject jsonObject = new JSONObject(result);  
                int status = jsonObject.getInt("status");  
                String message = jsonObject.getString("msg");  
                lessonResult.setStatus(status);  
                lessonResult.setMessage(message);  

                List<LessonInfo> lessonInfos = new ArrayList<>();  
                JSONArray dataArray = jsonObject.getJSONArray("data");  
                for (int i = 0; i < dataArray.length(); i++) {  
                    LessonInfo lessonInfo = new LessonInfo();  
                    JSONObject tempJsonObject = (JSONObject) dataArray.get(i);  
                    lessonInfo.setID(tempJsonObject.getInt("id"));  
                    lessonInfo.setName(tempJsonObject.getString("name"));  
                    lessonInfos.add(lessonInfo);  
                }  
                lessonResult.setLessonInfos(lessonInfos);  

            } catch (JSONException e) {  
                e.printStackTrace();  
            }  
            mListView.setAdapter(new AppListAdapter(RequestDataActivity.this, lessonResult.getLessonInfos()));  
        }  


        private String request(String urlString) {  
            try {  
                URL url = new URL(urlString);  
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
                connection.setConnectTimeout(30000);  
                connection.setRequestMethod("GET");  // GET POST  
                connection.connect();  
                int responseCode = connection.getResponseCode();  
                String responseMessage = connection.getResponseMessage();  
                String result = null;  
                if(responseCode == HttpURLConnection.HTTP_OK){  
                    InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());  
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
                    StringBuilder stringBuilder = new StringBuilder();  
                    String line;  
                    while ((line = bufferedReader.readLine()) != null) {  
                        stringBuilder.append(line);  
                    }  
                    result = stringBuilder.toString();  
                } else {  
                  // TODO:  
                }  
                return result;  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            return null;  
        }  
    }  

}  

ChatActivity.java

package com.imooc.demo.app.listview;  

import android.content.Context;  
import android.os.Bundle;  
import android.support.v7.app.AppCompatActivity;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.ImageView;  
import android.widget.ListView;  
import android.widget.TextView;  

import com.imooc.demo.R;  
import com.imooc.demo.model.ChatMessage;  

import java.util.ArrayList;  
import java.util.List;  

public class ChatActivity extends AppCompatActivity {  

    private ListView mListView;  
    List<ChatMessage> mChatMessages = new ArrayList<>();  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        mListView = (ListView) findViewById(R.id.list_view_demo);  
        ChatMessage chatMessage = new ChatMessage(1,2,"刘小明","8:20","你好吗","","",true);  
        ChatMessage chatMessage2 = new ChatMessage(2,1,"小军","8:21","我很好","","",false);  
        ChatMessage chatMessage3 = new ChatMessage(1,2,"刘小明","8:22","今天天气怎么样","","",true);  
        ChatMessage chatMessage4 = new ChatMessage(2,1,"小军","8:23","热成狗了","","",false);  

        mChatMessages.add(chatMessage);  
        mChatMessages.add(chatMessage2);  
        mChatMessages.add(chatMessage3);  
        mChatMessages.add(chatMessage4);  

        mListView.setAdapter(new ChatMessageAdapter(this, mChatMessages));  
    }  

    public static class ChatMessageAdapter extends BaseAdapter {  

        public interface IMessageViewType {  
            int COM_MESSAGE = 0;  
            int TO_MESSAGE = 1;  
        }  

        private List<ChatMessage> mChatMessages;  
        private LayoutInflater mInflater;  

        public ChatMessageAdapter(Context context, List<ChatMessage> coll) {  
            this.mChatMessages = coll;  
            mInflater = LayoutInflater.from(context);  
        }  

        public int getCount() {  
            return mChatMessages.size();  
        }  

        public Object getItem(int position) {  
            return mChatMessages.get(position);  
        }  

        public long getItemId(int position) {  
            return position;  
        }  

        public int getItemViewType(int position) {  
            ChatMessage entity = mChatMessages.get(position);  
            if (entity.getMsgType()) {  
                return IMessageViewType.COM_MESSAGE;  
            } else {  
                return IMessageViewType.TO_MESSAGE;  
            }  

        }  

        public int getViewTypeCount() {  
            return 2;  
        }  

        public View getView(int position, View convertView, ViewGroup parent) {  

            final ChatMessage entity = mChatMessages.get(position);  
            boolean isComMsg = entity.getMsgType();  

            ViewHolder viewHolder;  
            if (convertView == null) {  
                if (isComMsg) {  
                    convertView = mInflater.inflate(R.layout.chatting_item_msg_text_left, null);  
                } else {  
                    convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right, null);  
                }  
                viewHolder = new ViewHolder();  
                viewHolder.mSendTime = (TextView) convertView.findViewById(R.id.tv_send_time);  
                viewHolder.mUserName = (TextView) convertView.findViewById(R.id.tv_username);  
                viewHolder.mContent = (TextView) convertView.findViewById(R.id.tv_chat_content);  
                viewHolder.mTime = (TextView) convertView.findViewById(R.id.tv_time);  
                viewHolder.mUserAvatar = (ImageView) convertView.findViewById(R.id.iv_user_head);  
                viewHolder.mIsComMessage = isComMsg;  
                convertView.setTag(viewHolder);  
            } else {  
                viewHolder = (ViewHolder) convertView.getTag();  
            }  

            viewHolder.mSendTime.setText(entity.getDate());  
            viewHolder.mContent.setText(entity.getContent());  
            viewHolder.mContent.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);  
            viewHolder.mTime.setText("");  
            viewHolder.mUserName.setText(entity.getName());  
            if (isComMsg) {  
                viewHolder.mUserAvatar.setImageResource(R.drawable.avatar);  
            } else {  
                viewHolder.mUserAvatar.setImageResource(R.mipmap.ic_launcher);  
//                ImageLoader.getInstance().displayImage(entity.getAvatarUrl(), viewHolder.mUserAvatar);  
            }  

            return convertView;  
        }  

        class ViewHolder {  
            public TextView mSendTime;  
            public TextView mUserName;  
            public TextView mContent;  
            public TextView mTime;  
            public ImageView mUserAvatar;  
            public boolean mIsComMessage = true;  
        }  
    }  
}  

MainListAdapter.java

package com.imooc.demo.adapter;  

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

import com.imooc.demo.R;  
import com.imooc.demo.base.BindableAdapter;  
import com.imooc.demo.model.ActivityItem;  

import java.util.List;  

/** 
 * Function: 
 * Create date on 16/8/9. 
 * 
 * @author Conquer 
 * @version 1.0 
 */  
public class MainListAdapter extends BindableAdapter<ActivityItem> {  

    private Context mContext;  
    public MainListAdapter(Context context, List<ActivityItem> list) {  
        super(context, list);  
        mContext = context;  

    }  

    @Override  
    protected View onNewItemView(LayoutInflater inflater, ViewGroup parent, int position) {  
        View view = inflater.inflate(R.layout.item_main_list, null);  
        ViewHolder holder = new ViewHolder();  
        holder.mTitle = (TextView) view.findViewById(R.id.title);  
        view.setTag(holder);  
        return view;  
    }  

    @Override  
    protected void onBindItemView(View viewItem, final ActivityItem data, int position, ViewGroup parent) {  
        ViewHolder holder = (ViewHolder) viewItem.getTag();  
        holder.mTitle.setText(data.getName());  
        viewItem.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                 mContext.startActivity(new Intent(mContext, data.getActivityClass()));  
            }  
        });  
    }  

    private class ViewHolder {  
        private TextView mTitle;  
    }  
}  

如果xml文件的地方,图片处报红,请下载本文配套代码,从中提取该图片

运行效果:

项目Demo下载:

https://download.csdn.net/download/weimeig/10409593