android listView控件学习之一
1.基本应用:
1>res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/list_view"
/>
</LinearLayout>
2>listView 一般形式
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //ArrayAdapter为系统自带Adapter
Adapter myListAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1, mStrings));
listView = (ListView) findViewById(R.id.list_view);
}
3.listView 设置成像checkboxes 多选方法.
1>设置choice mode属性
2>list用CHOICE_MODE_MULTIPLE mode.
如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(this,
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
4. listView 设置成像checkboxes 单选方法
1>设置choice mode属性
2>list用CHOICE_MODE_SINGLE
public void onCreate(Bundle savedInstanceState) {
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, GENRES));
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
5. listView 自己定义Adapter 实现效果每行前面为图片后面紧跟文字.(imageView and textView)
1>构造MyListAdapter类
2>Adapter样式
res/layout/list_item_icon_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent"><ImageView android:id="@+id/icon" android:layout_width="48dip"
android:layout_height="48dip" /><TextView android:id="@+id/text" android:layout_gravity="center_vertical"
android:layout_width="0dip" android:layout_weight="1.0"
android:layout_height="wrap_content" />
</LinearLayout>
3>引用方式
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); myListAdapter = new MyListAdapter(this);
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(myListAdapter);
}
4>如何将自定义Adapter样式与View联系在一起.
mInflater = LayoutInflater.from(context);
View convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
5>重写getView()方法
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary
// calls
// to findViewById() on each row.
ViewHolder holder; // When convertView is not null, we can reuse it directly, there is no
// need
// to reinflate it. We only inflate a new View when the convertView
// supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null); // Creates a ViewHolder and store references to the two children
// views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon); convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
} // position = position % DATA.length; //实现向下循环
// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); return convertView;
}static class ViewHolder {
TextView text;
ImageView icon;
}
}
标签:控件,layout,text,list,convertView,android,listView,holder
From: https://blog.51cto.com/u_3124497/6913767