ListView类
Class Overview
A view that shows items in a vertically scrolling list. The items come from the ListAdapter
associated with this view.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
2 数据适配器ListAdapter及实现类
ListAdapter
implements Adapter创建数据适配器:
//getView,getCount方法需要覆写,才会显示List记录
BaseAdapter adapter = new BaseAdapter() {
//List中所存放的View,如果是只是一个TextView(简单数据),则返回的是TextView.
TextView tv = new TextView(AdapterActivity.this);
tv.setPadding(8, 8, 8, 8);
tv.setTextSize(20);
tv.setText(arrs[position]);
//如果是一个自定义的复杂的布局,如微博的列表的布局,一行中包含有图片,摆放不同位置的TextView,返回的就是一个layout
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// 1、获得行布局 用LayoutInflater
// 2、更新行布局
return null;
}
//显示List条数
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
};
//只有一个TextView的行的布局,listitem.xml (如果是复杂的布局,用布局加控件)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
public class ArrayAdapterActivity extends Activity {
ListView lv ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
String[] arr ={"aaa","bbb","ccc","ddd","eee"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr);
lv.setAdapter(arrayAdapter);
}
}
simple_list_item_1是android系统提供的一个layout,每个列表项都是一个普通的TextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@drawable/libai" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/textView1"
android:textSize="20dp"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="20dp"
android:layout_below="@+id/imageView1"
android:text="TextView" />
</RelativeLayout>
private String[] names = new String[]{"虎头","李白","弄玉","清照"};
private int[] imagesIds = new int[]{R.drawable.tiger,R.drawable.libai,R.drawable.nongyu,R.drawable.qingzhao};
private String[] contents = new String[]{
"很可爱的小男孩的名字",
"唐代著名诗人,有一首诗,叫举头望明月,啊,故乡。。。。",
"一个小小女孩",
"寻寻觅觅,冷冷清清,凄凄惨惨戚戚。"
};
ListView lv ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.listView1);
//LayoutInflater inflater = getLayoutInflater();
BaseAdapter ada = new BaseAdapter() {
//获取每一行的布局
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
// TODO Auto-generated method stub
//获取行布局
LayoutInflater inflater = getLayoutInflater();
RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.custom_list, null);
//行布局中的控件
ImageView image = (ImageView)layout.findViewById(R.id.imageView1);
TextView tv1 = (TextView)layout.findViewById(R.id.textView1);
TextView tv2 = (TextView)layout.findViewById(R.id.textView2);
//更新行布局中的内容
image.setImageResource(imagesIds[position]);
tv1.setText(names[position]);
tv2.setText(contents[position]);
return layout;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return names.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
};
lv.setAdapter(ada);
}
}
Public Constructors
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
Constructor
Parameters
context | The context where the View associated with this SimpleAdapter is running |
---|---|
data | A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from" |
resource | Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to" |
from | A list of column names that will be added to the Map associated with each item. |
to | The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. |
public class MainActivity extends Activity {
private String[] names = new String[]{"虎头","李白","弄玉","清照"};
private int[] imagesIds = new int[]{R.drawable.tiger,R.drawable.libai,R.drawable.nongyu,R.drawable.qingzhao};
private String[] contents = new String[]{
"很可爱的小男孩的名字",
"唐代著名诗人,有一首诗,叫举头望明月,啊,故乡。。。。",
"一个小小女孩",
"寻寻觅觅,冷冷清清,凄凄惨惨戚戚。"
};
ListView lv ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.listView1);
List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
for (int i = 0; i < names.length; i++) {
Map<String,Object> listItem = new HashMap<String,Object>();
listItem.put("header", imagesIds[i]);
listItem.put("personName", names[i]);
listItem.put("content",contents[i]);
listItems.add(listItem);
}
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.custom_list,
new String[]{"personName","header","content"},
new int[]{R.id.name,R.id.header,R.id.content});
lv.setAdapter(adapter);
}
}
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)
Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters
parent | The AdapterView where the click happened. |
---|---|
view | The view within the AdapterView that was clicked (this will be a view provided by the adapter) |
position | The position of the view in the adapter. |
id | The row id of the item that was clicked. |
public abstract boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id)
Callback method to be invoked when an item in this view has been clicked and held. Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters
parent | The AbsListView where the click happened |
---|---|
view | The view within the AbsListView that was clicked |
position | The position of the view in the list |
id | The row id of the item that was clicked |
Returns
- true if the callback consumed the long click, false otherwise