1.数据listView
public class MainActivity extends Activity implements OnClickListener{
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView);
String[] arr_data = {"data1", "data2", "data3", "data4",};
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr_data);
listView.setAdapter(arrayAdapter);
}
}
2. simple ListV
布局item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="demo" />
</LinearLayout>
代码
public class MainActivity extends Activity implements OnItemClickListener, OnScrollListener{
private ListView listView;
private SimpleAdapter simpleAdapter;
private List<Map<String, Object>> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView);
dataList = new ArrayList<Map<String, Object>>();
simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic", "text"}, new int[]{R.id.pic, R.id.text});
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(this);
listView.setOnScrollListener(this);
}
private List<Map<String, Object>> getData(){
for (int i = 0; i < 20; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("pic", R.drawable.ic_launcher);
map.put("text", "demo" + i);
dataList.add(map);
}
return dataList;
}
@Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
switch (arg1) {
case SCROLL_STATE_FLING:
Log.i("main", "手指离开,由惯性,视图继续滑动");
break;
case SCROLL_STATE_IDLE:
Log.i("main", "视图已停止滑动");
break;
case SCROLL_STATE_TOUCH_SCROLL:
Log.i("main", "手指没有离开屏幕,视图滑动");
break;
}
}
@Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String text = listView.getItemAtPosition(arg2) + "";
Toast.makeText(this, "position=" + arg2 + "text=" +text, Toast.LENGTH_SHORT).show();
}
}