生活随笔
收集整理的這篇文章主要介紹了
使用ListView实现汽泡短信聊天
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
小魏原創,歡迎轉載~
轉載請注明出處:http://blog.csdn.net/xiaowei_cqu/article/details/7045543
如前文http://blog.csdn.net/xiaowei_cqu/article/details/7045497
我們進行了SimpleAdapter適配器初次嘗試,那么離實現我們最終想要的效果也不遠啦,只要仿照chata的布局,再編寫第二位聊天人(“路人甲”)的布局chatb——只要讓他靠右顯示就行~。
但是這樣我們每次都要很麻煩的定義一遍SimpleAdapter,為了“偷懶”,我們直接來編寫自己的Adapter,這樣每次定義就方便多了。
先附上最終的代碼:
[java]?view plaincopy
public?void?onCreate(Bundle?savedInstanceState)?{?? ????????????super.onCreate(savedInstanceState);?? ????????????setContentView(R.layout.main);?? ????????????chatlist?=?(ListView)?findViewById(R.id.chatlist);?? ????????????list?=?new?ArrayList<ChatEntity>();?? ????????????ChatEntity?chat1=new?ChatEntity("小魏","嗨~",R.layout.chata);?? ????????????list.add(chat1);?? ????????????ChatEntity?chat2=new?ChatEntity("路人甲","你好!",R.layout.chatb);?? ????????????list.add(chat2);?? ????????????ChatEntity?chat3=new?ChatEntity("小魏","我是小魏~",R.layout.chata);?? ????????????list.add(chat3);?? ?????????????? ????????????chatlist.setAdapter(new?ChatAdapter(TryChatPop2Activity.this,list));?? }??
如上代碼,在setAdapter時使用了自己的ChatAdapter,以下是類文件代碼:
[java]?view plaincopy
public?class?ChatAdapter?implements?ListAdapter{?? ????private?ArrayList<ChatEntity>?list;?? ????private?Context?ctx;?? ?? ????public?ChatAdapter(Context?context?,ArrayList<ChatEntity>?list)?{?? ????????ctx?=?context;?? ????????this.list?=?list;?? ????}?? ?????? ????public?boolean?areAllItemsEnabled()?{?? ????????return?false;?? ????}?? ????public?boolean?isEnabled(int?arg0)?{?? ????????return?false;?? ????}?? ????public?int?getCount()?{?? ????????return?list.size();?? ????}?? ????public?Object?getItem(int?position)?{?? ????????return?list.get(position);?? ????}?? ????public?long?getItemId(int?position)?{?? ????????return?position;?? ????}?? ????public?int?getItemViewType(int?position)?{?? ????????return?position;?? ????}?? ????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{?? ????????ChatEntity?entity?=?list.get(position);?? ????????int?itemLayout?=?entity.getLayoutID();?? ?????????? ????????LinearLayout?layout?=?new?LinearLayout(ctx);?? ????????LayoutInflater?vi?=?(LayoutInflater)?ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);?? ????????vi.inflate(itemLayout,?layout,true);?? ?????????? ????????TextView?txvName?=?(TextView)?layout.findViewById(R.id.txvName);?? ????????txvName.setText(entity.getName());?? ?????????? ????????TextView?txvText?=?(TextView)?layout.findViewById(R.id.txvInfo);?? ????????txvText.setText(entity.getInfo());?? ????????return?layout;?? ????}?? ????public?int?getViewTypeCount()?{?? ????????return?list.size();?? ????}?? ????public?boolean?hasStableIds()?{?? ????????return?false;?? ????}?? ????public?boolean?isEmpty()?{?? ????????return?false;?? ????}?? ????public?void?registerDataSetObserver(DataSetObserver?observer)?{?? ????}?? ????public?void?unregisterDataSetObserver(DataSetObserver?observer)?{?? ????}?? ?? }??
ChatAdapterd的類實現了ListAdapter的接口,并通過ChatEntity中的內容設置了定義布局中聊天對象名字txvName及聊天內容txvInfo的內容,當然你肯定能明白ChatEntity就是存放聊天信息等內容的實體類。
這里我們可以這樣寫,就是因為ListAdapter的接口是綁定Data和ListView的適配器,實際上我們常用的ArryaAdapter、SimpleAdapter、CursorAdapter就是他的子類。
關系如下:
這樣再看代碼,甚至再回頭看SimpleAdapter就感覺好理解多了,其他內容不細說了,具體參照源碼:http://download.csdn.net/detail/xiaowei_cqu/3886321
再上一遍效果圖:
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的使用ListView实现汽泡短信聊天的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。