最近老用到ListView,這個會了,其他的就更沒問題了,圖片異步加載的以後再說哈。
列表的顯示需要三個元素:
1.ListVeiw 用來展示列表的View。
2.適配器 用來把數據映射到ListView上的中介。
3.數據 具體的將被映射的字符串,圖片,或者基本組件。
用SimpleAdapter實現的話上面的按鈕是無法映射的,即使你成功的用佈局文件顯示出了按鈕也無法添加按鈕的響應,所以必須要重寫一個類繼承BaseAdapter。效果如下:
每一行的佈局文件list_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="80dp"
android:layout_margin="3dp"
android:background="@drawable/shape"
>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginLeft="8dp"
/>
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dp"
android:layout_marginTop="15dp"
android:maxWidth="75dp"
android:minWidth="75dp"
android:text="軟件大樓" />
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp">
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/rec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="14dp"
android:text="推薦熱度:" />
<RatingBar
style="?android:attr/ratingBarStyleSmall"
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars = "5"
android:stepSize = "0.5"
android:rating = "3"/>
</LinearLayout>
<TextView android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="14dp"
android:text="瀏覽人數:10人次" />
</LinearLayout>
<Button android:id="@+id/view_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕"
android:focusable="false"
android:layout_gravity="right" />
</LinearLayout>
程序代碼:
public class ListTestActivity extends Activity {
private List<Map<String, Object>> mData;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mData = getData();
MyAdapter adapter = new MyAdapter(this);
ListView lv = (ListView)findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
// TODO Auto-generated method stub
Log.v("Click","ListItemSelected:"+ a.toString()+"查看:"
+ v.toString()+"位置:"+"ID:"+ id);
Uri uri =Uri.parse("smsto://123456789");
Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body",position);
startActivity(intent);
}
});
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
String[] title={"軟件大樓","圖書館","文化街"};
String[] rating={"2","3","4"};
for(int i=0;i<3;i++)
{
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", title[i]);
map.put("img", R.drawable.ic_launcher);
map.put("rating",rating[i]);
list.add(map);
}
return list;
}
public final class ViewHolder{
public ImageView img;
public TextView title;
public RatingBar rating;
public Button view_btn;
}
public class MyAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public MyAdapter(Context context){
this.mInflater = LayoutInflater.from(context);
}
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
//獲取ListView每一行
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder=new ViewHolder();
convertView = mInflater.inflate(R.layout.list_item, null);
holder.img = (ImageView)convertView.findViewById(R.id.img);
holder.title = (TextView)convertView.findViewById(R.id.title);
holder.rating = (RatingBar)convertView.findViewById(R.id.rating);
holder.view_btn = (Button) convertView.findViewById(R.id.view_btn);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
holder.title.setText((String)mData.get(position).get("title"));
Log.v("R","R:"+(String) mData.get(position).get("rating"));
//holder.rating.setRating((Float)mData.get(position).get("rating"));
holder.view_btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showInfo();
}});
return convertView;
}
}
public void showInfo(){
new AlertDialog.Builder(this)
.setTitle("我的listview")
.setMessage("...")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
} }) .show(); }
}
listView在開始繪製的時候,系統首先調用getCount()函數,根據他的返回值得到listView的長度,然後根據這個長度,調用getView()逐一繪製每一行。如果你的getCount()返回值是0的話,列表將不顯示同樣return 1,就只顯示一行。
當手動完成適配時,必須手動映射數據,這需要重寫getView()方法。系統在繪製列表的每一行的時候將調用此方法。getView()有三個參數,position表示將顯示的是第幾行,covertView是從佈局文件中inflate來的佈局。我們用LayoutInflater的方法將定義好的list_item.xml文件提取成View實例用來顯示。然後將xml文件中的各個組件實例化(簡單的findViewById()方法)。這樣便可以將數據對應到各個組件上了。但是按鈕爲了響應點擊事件,需要爲它添加點擊監聽器,這樣就能捕獲點擊事件。至此一個自定義的listView就完成了,現在讓我們回過頭從新審視這個過程。在實際的運行過程中會發現listView的每一行沒有焦點了,這是因爲Button搶奪了listView的焦點,只要佈局文件中將Button設置爲沒有焦點就OK了。
運行效果截圖:
代碼在:http://download.csdn.net/detail/ristal/4231519點擊打開鏈接
好了,就這樣吧,睡覺啦!