通常我們用慣的ListView每一項(xiàng)的布局都是相同的,只是控件所綁定的數(shù)據(jù)不同。但單單只是如此并不能滿足我們某些特殊需求,比如我們常見(jiàn)的QQ、微信的聊天列表,除了有左右之分外,內(nèi)容更是有很大區(qū)別,有文字、語(yǔ)音、圖片、視頻等等,他們真的是ListView可以實(shí)現(xiàn)的嗎?答案是肯定的,只要我們做一下類型區(qū)別即可。
創(chuàng)新互聯(lián)專注于鐘山企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,成都商城網(wǎng)站開(kāi)發(fā)。鐘山網(wǎng)站建設(shè)公司,為鐘山等地區(qū)提供建站服務(wù)。全流程按需制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
實(shí)現(xiàn)效果如下所示:
大家不要在意布局,這里為了方便就隨意了。大家可以看到,這里有兩種布局,一種頭像在左,一種頭像在右,雖然這是一種簡(jiǎn)單的情況,但我們只需要了解其中的原理,再?gòu)?fù)雜的情況都可以迎刃而解。
我們只要將每一種布局劃為一種類型進(jìn)行區(qū)分,根據(jù)我們所區(qū)分的類型在我們自定義的Adapter中加載不同布局即可,代碼如下所示:
if (bean.getType() == 1) { convertView = mInflater.inflate(R.layout.item_chat_left, null); holder.mIvAvatar = (ImageView) convertView.findViewById(R.id.iv_chatLeftItem_avatar); holder.mTvContent = (TextView) convertView.findViewById(R.id.tv_chatLeftItem_content); } else if (bean.getType() == 2) { convertView = mInflater.inflate(R.layout.item_chat_right, null); holder.mIvAvatar = (ImageView) convertView.findViewById(R.id.iv_chatRightItem_avatar); holder.mTvContent = (TextView) convertView.findViewById(R.id.tv_chatRightItem_content); }
是不是發(fā)現(xiàn)很簡(jiǎn)單,最后,我再將所有代碼一同貼出來(lái)。
item_chat_left.xml
<?xml version="1.0" encoding="utf-8"?>
item_chat_right.xml
<?xml version="1.0" encoding="utf-8"?>
activity_chat.xml
ChatItemBean.java
import android.graphics.Bitmap; public class ChatItemBean { private int type; private String content; private Bitmap avatar; public int getType() { return type; } public void setType(int type) { this.type = type; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Bitmap getAvatar() { return avatar; } public void setAvatar(Bitmap avatar) { this.avatar = avatar; } }
ChatLVAdapter.java
import java.util.List; 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; public class ChatLVAdapter extends BaseAdapter { private ListmDatas; private LayoutInflater mInflater; public ChatLVAdapter(Context context, List datas) { this.mInflater = LayoutInflater.from(context); this.mDatas = datas; } @Override public int getCount() { return mDatas.size(); } @Override public Object getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; ChatItemBean bean = mDatas.get(position); if (convertView == null) { holder = new ViewHolder(); if (bean.getType() == 1) { convertView = mInflater.inflate(R.layout.item_chat_left, null); holder.mIvAvatar = (ImageView) convertView.findViewById(R.id.iv_chatLeftItem_avatar); holder.mTvContent = (TextView) convertView.findViewById(R.id.tv_chatLeftItem_content); } else if (bean.getType() == 2) { convertView = mInflater.inflate(R.layout.item_chat_right, null); holder.mIvAvatar = (ImageView) convertView.findViewById(R.id.iv_chatRightItem_avatar); holder.mTvContent = (TextView) convertView.findViewById(R.id.tv_chatRightItem_content); } convertView.setTag(holder); } else { // 通過(guò)tag找到緩存的布局 holder = (ViewHolder) convertView.getTag(); } holder.mIvAvatar.setImageBitmap(bean.getAvatar()); holder.mTvContent.setText(bean.getContent()); return convertView; } public final class ViewHolder { public ImageView mIvAvatar; public TextView mTvContent; }
ChatActivity.java
import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.ListView; public class ChatActivity extends Activity { private ListView mLv; private ListmDatas ; private ChatLVAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); mLv = (ListView) findViewById(R.id.lv_content); initData(); mAdapter = new ChatLVAdapter(this, mDatas); mLv.setAdapter(mAdapter); } private void initData() { mDatas = new ArrayList (); ChatItemBean chat1 = new ChatItemBean(); chat1.setType(1); chat1.setContent("早??!"); chat1.setAvatar(BitmapFactory.decodeResource(getResources(), R.drawable.ic_item1)); mDatas.add(chat1); ChatItemBean chat2 = new ChatItemBean(); chat2.setType(2); chat2.setContent("早!一大早找我有啥事?"); chat2.setAvatar(BitmapFactory.decodeResource(getResources(), R.drawable.ic_item2)); mDatas.add(chat2); ChatItemBean chat3 = new ChatItemBean(); chat3.setType(1); chat3.setContent("沒(méi)事就不能找你談情說(shuō)愛(ài)嗎?也沒(méi)什么事,看你有沒(méi)有在擼代碼。"); chat3.setAvatar(BitmapFactory.decodeResource(getResources(), R.drawable.ic_item1)); mDatas.add(chat3); ChatItemBean chat4 = new ChatItemBean(); chat4.setType(2); chat4.setContent("算了吧,別找我!我害怕。"); chat4.setAvatar(BitmapFactory.decodeResource(getResources(), R.drawable.ic_item2)); mDatas.add(chat4); ChatItemBean chat5 = new ChatItemBean(); chat5.setType(2); chat5.setContent("都被代碼擼慘了,我哪敢一早起來(lái)找虐。"); chat5.setAvatar(BitmapFactory.decodeResource(getResources(), R.drawable.ic_item2)); mDatas.add(chat5); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。