iPhone中短信聊天都是那種氣泡式的,當然,Android短信也是。那麼我們如何實現這種效果呢?我下面就簡單的實現了一個demo,其實就是一個listview,然後對adapter中getview的時候,根據自己的設定來獲取不同的layout。
具體看代碼:
- public class MainActivity extends Activity {
- private ListView talkView;
- private ArrayList<DetailEntity> list = null;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- talkView = (ListView)findViewById(R.id.list);
-
- list = new ArrayList<DetailEntity>();
- DetailEntity d1 = new DetailEntity("Me","2012-04-26","Hello!",R.layout.list_say_me_item);
- list.add(d1);
- DetailEntity d2 = new DetailEntity("You","2012-04-26","Hi!",R.layout.list_say_he_item);
- list.add(d2);
- DetailEntity d3 = new DetailEntity("Me","2012-04-26","Waht is your name?",R.layout.list_say_he_item);
- list.add(d3);
- DetailEntity d4 = new DetailEntity("You","2012-04-26","****!",R.layout.list_say_me_item);
- list.add(d4);
-
- talkView.setAdapter(new DetailAdapter(MainActivity.this, list));
- }
- }