今天,學習了ListView組件,ListView組件是android中很常見的一個組件,在ListView組件中,有兩個很重要的屬性,一個是要設置適配器,也就是設置ListView中的每一項都設置布局,綁定對象,返回成員信息,設置id,第二個就是,對ListView設置子控件點擊事件。
//listview適配器
public class MyAdapter extends BaseAdapter{
private int count;
public MyAdapter(int count) {
super();
this.count = count;
}
//返回條目數量
@Override
public int getCount() {
return count;
}
//返回當前position位置對應的條目的object對象
@Override
public Object getItem(int position) {
return null;
}
//返回當前位置條目的id,一般設置當前position
@Override
public long getItemId(int position) {
return position;
}
/**
* 返回每一個條目返回的具體內容
* 一個界面顯示幾個條目,就被調用幾次,在滾動條滾動時,新出現的條目也會調用
* @param parent 表示父控件(當前listview)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//系統服務-->通過資源文件創建view對象,第二個參數表示根元素
View view = inflater.inflate(R.layout.listview, null);
//尋找兩個組件
TextView name = (TextView) view.findViewById(R.id.tv_name);
TextView age = (TextView) view.findViewById(R.id.tv_age);
name.setText("name:xiongmao"+position);
age.setText("age:"+position);
return view;
}
}以上是一個自定義的適配器類
以下是設置子控件點擊事件
//設置子控件點擊事件
listView.setOnItemClickListener(new OnItemClickListener() {
/**
* parent listview父控件
* view 被點擊的view對象
* position 當前位置
* id 當前id
*/
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView tvname = (TextView) view.findViewById(R.id.tv_name);
String name = tvname.getText().toString();
Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
}
});免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。