android 仿微信聊天介面

來源:互聯網
上載者:User

android 仿聊天介面

一些IM聊天軟體我們發現,他的展現形式,是左右分開的形式,而我們的listview很多時候是顯示同一個布局,其實BaseAdapter中有2個重要的方法在大多數情況下我們並未使用到,一個是public int getViewTypeCount(),它是顯示listview中有多少種布局,預設是顯示是1,像那樣聊天介面,是有2種布局方式,;另外一個getItemViewType(),它是在那些item條目中顯示那種布局,下面就簡單的類比下的聊天介面做法:

MainActivity.java

package com.example.weixin;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity {private ListView listview;private List persons;private int TYPE_COUNT = 2;private int LEFT = 0;private int RIGHT = 1;private LayoutInflater inflater;private MyAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listview = (ListView) findViewById(R.id.listview);inflater = LayoutInflater.from(this);initData();adapter = new MyAdapter();listview.setAdapter(adapter);}private void initData() {persons = new ArrayList();for(int i=0;i<40;i++){Person p = new Person();p.setAge(i);p.setName("張三");if(i%2==0){p.setType(0);}else{p.setType(1);}persons.add(p);}}class MyAdapter extends BaseAdapter{@Overridepublic int getCount() {return persons.size();}@Overridepublic Object getItem(int position) {return persons.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic int getItemViewType(int position) {if(persons.get(position).getType()==0){return LEFT;}return RIGHT;}@Overridepublic int getViewTypeCount() {return TYPE_COUNT;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;Person p = persons.get(position);if(getItemViewType(position)==LEFT){if(convertView==null){holder = new ViewHolder();convertView =inflater.inflate(R.layout.item_left, null); holder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);holder.tv_age = (TextView) convertView.findViewById(R.id.tv_age);convertView.setTag(holder);}else{holder = (ViewHolder) convertView.getTag();}holder.tv_username.setText(p.getName());holder.tv_age.setText(String.valueOf(p.getAge()));}else{if(convertView==null){holder = new ViewHolder();convertView =inflater.inflate(R.layout.item_right, null);holder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);holder.tv_age = (TextView) convertView.findViewById(R.id.tv_age);convertView.setTag(holder);}else{holder = (ViewHolder) convertView.getTag();}holder.tv_username.setText(p.getName());holder.tv_age.setText(String.valueOf(p.getAge()));}return convertView;}class ViewHolder{TextView tv_username;TextView tv_age;}}}

Person.java

package com.example.weixin;public class Person {    private String name;    private int age;    private int type;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getType() {return type;}public void setType(int type) {this.type = type;}}

item_left.xml

        

item_right.xml

        

介面效果:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.