使用ListView来模仿微博随便看看栏
【分析】
要完成这个效果,我们需要:
1.ListView及List_Itrm
extends Activity的编写
2.定义数据集合
3.创建SimpleAdapter绑定数据
4.添加单击事件
【编码】
MainActivity:
package com.example.androidweibo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class MainActivity extends Activity {
List<Map<String, ?>> data;
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = getData();
SimpleAdapter adapter = new SimpleAdapter(this, data,
R.layout.list_itrm, new String[] { "photo", "name", "publish",
"content" }, new int[] { R.id.photo, R.id.name,
R.id.publish, R.id.content });
listView=(ListView) this.findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new ListClickHandler());
}
private class ListClickHandler implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
Map<String, String> item=(Map<String, String>) data.get(position);
Toast.makeText(MainActivity.this, item.get("name").toString(), Toast.LENGTH_LONG).show();
}
}
private List<Map<String, ?>> getData() {
List<Map<String, ?>> data = new ArrayList<Map<String, ?>>();
Map<String, Object> item = new HashMap<String, Object>();
item.put("photo", R.drawable.p1);
item.put("name", "晓月");
item.put("publish", "1分钟前");
item.put("content", "正在学习AndroidListView,嘎嘎嘎嘎嘎嘎嘎嘎嘎");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p2);
item.put("name", "张三");
item.put("publish", "10分钟前");
item.put("content", "今天真高兴啊!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p3);
item.put("name", "米粒");
item.put("publish", "2015—5-11");
item.put("content", "今天糗大了!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p4);
item.put("name", "高未");
item.put("publish", "2015—5-10");
item.put("content", "今天遇到一件好玩的事情!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p5);
item.put("name", "花花");
item.put("publish", "2015—5-10");
item.put("content", "今天天气真好哈!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p6);
item.put("name", "露露");
item.put("publish", "2015—5-9");
item.put("content", "今天真高兴啊!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p7);
item.put("name", "小新");
item.put("publish", "2015—5-9");
item.put("content", "我很开心今天!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p8);
item.put("name", "苏苏");
item.put("publish", "2015—5-8");
item.put("content", "世界好大啊!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p9);
item.put("name", "依晨");
item.put("publish", "2015—5-7");
item.put("content", "我今天买了一个大熊猫!");
data.add(item);
return data;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
【布局】
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/holo_blue_bright"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
list_itrm.xml:
<!-- 左边图片 -->
<ImageView
android:id="@+id/photo"
android:padding="1dp"
android:layout_width="50dp"
android:layout_height="50dp" />
<!-- 右边布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- 上边布局 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- 发布人 -->
<TextView
android:id="@+id/name"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 发布时间 -->
<TextView
android:id="@+id/publish"
android:textColor="@android:color/black"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right" />
</LinearLayout>
<!-- 下边发布内容 -->
<TextView
android:id="@+id/content"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />