标签:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView)findViewById(R.id.listView); datalist = new ArrayList<Map<String,Object>>(); //1.新建适配器 //2.适配器加载数据源 /* 各个参数含义 context:上下文 data:数据源 (List<Map<String,object>>)多个Map所组成的List集合 每一个Map都对应ListView列表中的一行 每一个Map(键-值对)中的键必须包含所有在from中所指定的键 resource:列表项的布局的文件ID from: Map中的键名 to: 绑定数据视图中的ID,与from成对应关系。 */ sim_adapter = new SimpleAdapter(this,getDATA(),R.layout.abc,new String[]{"first","second"},new int[]{R.id.pic,R.id.text}); //3.视图加载适配器 lv.setAdapter(sim_adapter); lv.setOnItemClickListener(this); lv.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("first",R.drawable.abc); map.put("second", "礼拜" + i); datalist.add(map); } return datalist; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String text = lv.getItemAtPosition(position)+ ""; //Toast.makeText(上下文,内容,显示时间); Toast toast =Toast.makeText(this,"位置="+position+"内容="+text,Toast.LENGTH_SHORT); //在屏幕中显示的位置,X轴的相对偏移,Y轴的相对偏移 toast.setGravity(Gravity.CENTER, 0 , 0); //运行展示 toast.show(); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { case SCROLL_STATE_FLING: Log.i("Main", "用户在手指离开屏幕,由于用力滑了一下,视图仍依靠惯性继续滑动"); Map<String,Object> map = new HashMap<String, Object>(); map.put("first",R.drawable.abc); map.put("second","增加项目"); datalist.add(map); sim_adapter.notifyDataSetChanged(); //刷新listview break; case SCROLL_STATE_IDLE: Log.i("Main","视图已停止滑动"); break; case SCROLL_STATE_TOUCH_SCROLL: Log.i("Main","手指没有离开屏幕,视图正在滑动"); break; } }
标签:
原文地址:http://www.cnblogs.com/zmaibbs7/p/4831880.html