安卓开发--ListView
package com.zx.listview01; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.R.integer; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener,OnScrollListener{ //ListView需要创建适配器用于绑定展示数据 private ArrayAdapter<String> arr_adapter; private SimpleAdapter simpleAdapter; private ListView lView; private List<Map<String, Object>> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String[] arrStrings={"京东","淘宝","网易"}; lView=(ListView)findViewById(R.id.listView1); arr_adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrStrings); //ListView绑定适配器 //lView.setAdapter(arr_adapter); data=new ArrayList<Map<String,Object>>(); simpleAdapter = new SimpleAdapter(this, GetData(), R.layout.itemmain, new String[]{"imageView1","textView1"}, new int[] {R.id.imageView1,R.id.textView1}); lView.setAdapter(simpleAdapter); lView.setOnItemClickListener(this); lView.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("imageView1", R.drawable.ic_launcher); map.put("textView1", "你好"+i); data.add(map); } return data; } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO 自动生成的方法存根 switch (scrollState) { case SCROLL_STATE_FLING://用户在手指离开屏幕之前,由于用力划了一下,视图仍然依靠惯性滑动 Map<String, Object> map=new HashMap<String, Object>(); map.put("imageView1", R.drawable.ic_launcher); map.put("textView1", "淘宝"); data.add(map); simpleAdapter.notifyDataSetChanged(); break; case SCROLL_STATE_IDLE://视图停止滑动 break; case SCROLL_STATE_TOUCH_SCROLL://手指没有离开屏幕,视图正在滑动 break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO 自动生成的方法存根 } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO 自动生成的方法存根 String txtString = lView.getItemAtPosition(position).toString(); Toast.makeText(this, "下标:"+position+",内容:"+txtString, Toast.LENGTH_SHORT).show(); } }