Android ListView使用詳解
阿新 • • 發佈:2019-01-01
最近在做android上有關採用ListView進行資訊顯示的開發工作,發現有很多實現的方法,不過思想基本是一致的。網上大部分的測試程式採用的是陣列儲存要顯示的資訊,但是為了更好地對資訊進行管理,我採用的是用物件來儲存資訊方式。同時我要對item點選事件進行相應,中間遇到了一些困惑,不過都解決了,這裡把完整程式碼分享一下:
首先,list佈局檔案:
接著,item佈局檔案:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".ListTestActivity" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
接著,主程式:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ImageView android:id="@+id/img" android:layout_width="71dp" android:layout_height="71dp" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="242dp" android:layout_height="wrap_content" android:text="222" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/info" android:layout_width="match_parent" android:layout_height="47dp" android:text="11" /> </LinearLayout> </LinearLayout>
info類檔案:package my.test.ListTest; import java.util.ArrayList; import java.util.List; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.ContextMenu.ContextMenuInfo; import android.view.View.OnCreateContextMenuListener; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; @SuppressLint("ParserError") public class ListTestActivity extends Activity { ListView listView; //宣告一個ListView物件 private List<info> mlistInfo = new ArrayList<info>(); //宣告一個list,動態儲存要顯示的資訊 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView=(ListView)this.findViewById(R.id.listView); //將listView與佈局物件關聯 setInfo(); //給資訊賦值函式,用來測試 listView.setAdapter(new ListViewAdapter(mlistInfo)); //處理Item的點選事件 listView.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView<?> parent, View view,int position, long id) { info getObject = mlistInfo.get(position); //通過position獲取所點選的物件 int infoId = getObject.getId(); //獲取資訊id String infoTitle = getObject.getTitle(); //獲取資訊標題 String infoDetails = getObject.getDetails(); //獲取資訊詳情 //Toast顯示測試 Toast.makeText(ListTestActivity.this, "資訊ID:"+infoId,Toast.LENGTH_SHORT).show(); } }); //長按選單顯示 listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { public void onCreateContextMenu(ContextMenu conMenu, View view , ContextMenuInfo info) { conMenu.setHeaderTitle("選單"); conMenu.add(0, 0, 0, "條目一"); conMenu.add(0, 1, 1, "條目二"); conMenu.add(0, 2, 2, "條目三"); } }); } //長按選單處理函式 public boolean onContextItemSelected(MenuItem aItem) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)aItem.getMenuInfo(); switch (aItem.getItemId()) { case 0: Toast.makeText(ListTestActivity.this, "你點選了條目一",Toast.LENGTH_SHORT).show(); return true; case 1: Toast.makeText(ListTestActivity.this, "你點選了條目二",Toast.LENGTH_SHORT).show(); return true; case 2: Toast.makeText(ListTestActivity.this, "你點選了條目三",Toast.LENGTH_SHORT).show(); return true; } return false; } public class ListViewAdapter extends BaseAdapter { View[] itemViews; public ListViewAdapter(List<info> mlistInfo) { // TODO Auto-generated constructor stub itemViews = new View[mlistInfo.size()]; for(int i=0;i<mlistInfo.size();i++){ info getInfo=(info)mlistInfo.get(i); //獲取第i個物件 //呼叫makeItemView,例項化一個Item itemViews[i]=makeItemView( getInfo.getTitle(), getInfo.getDetails(),getInfo.getAvatar() ); } } public int getCount() { return itemViews.length; } public View getItem(int position) { return itemViews[position]; } public long getItemId(int position) { return position; } //繪製Item的函式 private View makeItemView(String strTitle, String strText, int resId) { LayoutInflater inflater = (LayoutInflater) ListTestActivity.this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // 使用View的物件itemView與R.layout.item關聯 View itemView = inflater.inflate(R.layout.item, null); // 通過findViewById()方法例項R.layout.item內各元件 TextView title = (TextView) itemView.findViewById(R.id.title); title.setText(strTitle); //填入相應的值 TextView text = (TextView) itemView.findViewById(R.id.info); text.setText(strText); ImageView image = (ImageView) itemView.findViewById(R.id.img); image.setImageResource(resId); return itemView; } public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) return itemViews[position]; return convertView; } } public void setInfo(){ mlistInfo.clear(); int i=0; while(i<10){ info information = new info(); information.setId(1000+i); information.setTitle("標題"+i); information.setDetails("詳細資訊"+i); information.setAvatar(R.drawable.i2); mlistInfo.add(information); //將新的info物件加入到資訊列表中 i++; } } }
package my.test.ListTest;
public class info {
private int id; //資訊ID
private String title; //資訊標題
private String details; //詳細資訊
private int avatar; //圖片ID
//資訊ID處理函式
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
//標題
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
//詳細資訊
public void setDetails(String info) {
this.details = info;
}
public String getDetails() {
return details;
}
//圖片
public void setAvatar(int avatar) {
this.avatar = avatar;
}
public int getAvatar() {
return avatar;
}
}
加入圖片檔案,執行即可!
貼上效果圖: