本文共 2463 字,大约阅读时间需要 8 分钟。
public class SecondActivity extends Activity implements AdapterView.OnItemClickListener,AbsListView.OnScrollListener{ private ListView listView; private ArrayAdapter arrayAdapter; private SimpleAdapter simpleAdapter; private List > dataList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); listView = (ListView)findViewById(R.id.listView); /* ArrayAdapter //1.新建一个数据适配器 //2.适配器加载数据源 //3.视图加载适配器 String[]arr_data = {"测试1","测试2","测试3","测试4","测试5"}; //ArrayAdapter(上下文,布局文件,数据源) arrayAdapter = new ArrayAdapter (this,android.R.layout.simple_list_item_1,arr_data); listView.setAdapter(arrayAdapter); */ //SimpleAdapter /* * context:上下文 * data:数据源(List >data) 一个map组成的list集合 * resource:列表项的布局文件ID * from:Map中的键名 * to:绑定数据视图中的ID,与from成对应关系 * */ dataList = new ArrayList >(); simpleAdapter = new SimpleAdapter(this,getData(),R.layout.cell,new String[]{"cellpic","celltext"},new int[]{R.id.cellpic,R.id.celltext}); listView.setAdapter(simpleAdapter); listView.setOnItemClickListener(this); listView.setOnScrollListener(this); } private List >getData(){ for (int i=0;i<20;i++){ Map map = new HashMap (); map.put("cellpic",R.drawable.icon); map.put("celltext","测试"+i); dataList.add(map); } return dataList; } @Override public void onItemClick(AdapterView parent, View view, int position, long id) { String text = listView.getItemAtPosition(position).toString(); //可以缩写listView.getItemAtPosition(position)+“”; Toast.makeText(this,"position = "+position+" text = "+text,Toast.LENGTH_SHORT).show(); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState){ case SCROLL_STATE_FLING: System.out.print("用户手指离开屏幕,但屏幕继续减速滑动过程中"); //添加数据源 Map map = new HashMap (); map.put("cellpic",R.drawable.icon); map.put("celltext","增加项"); dataList.add(map); //适配器刷新数据 simpleAdapter.notifyDataSetChanged(); break; case SCROLL_STATE_IDLE: System.out.print("停止滑动"); break; case SCROLL_STATE_TOUCH_SCROLL: System.out.print("正在滑动"); break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { }}
转载于:https://my.oschina.net/u/2346786/blog/807752