zoukankan      html  css  js  c++  java
  • listview添加onItemClickListener

    MainActivity.java

    package com.wyl.listview04;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.app.Activity;
    import android.os.Bundle;
    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.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements OnItemClickListener,OnScrollListener{
    	SimpleAdapter adapter;
    	List<Map<String,Object>> list;
    	ListView listview ;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.list);//以list.xml为主界面
    		/*
    		 * 实例化listview等
    		 */
    		listview = (ListView) findViewById(R.id.listview);
    		adapter = new SimpleAdapter(this, getData(), R.layout.textview, new String[]{"pic","txt"}, new int[]{R.id.imgview,R.id.textview});
    //		new SimpleAdapter(this, getData(), R.layout.textview, new String[]{"img","textview01"}, new int[]{R.id.imgview,R.id.textview});
    		listview.setAdapter(adapter);
    		listview.setOnItemClickListener(this);
    //		listview.setOnScrollListener(this);
    	}
    	private List<Map<String, Object>> getData() {
    		list = new ArrayList<Map<String,Object>>();
    		for(int i=0;i<50;i++){
    			Map<String,Object> map = new HashMap<String,Object>();
    			map.put("pic", R.drawable.yl);
    			map.put("txt", "wyl "+i*i);
    			System.out.println("====="+map.get("txt"));
    			list.add(map);
    		}
    		return list;
    	}
    	@Override
    	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    		String location = "位置:"+listview.getItemIdAtPosition(position);
    		String l = "   内容,"+listview.getItemAtPosition(position);
    		Toast.makeText(this, location+l, Toast.LENGTH_SHORT).show();
    		
    	}
    	@Override
    	public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
    		// TODO Auto-generated method stub
    		Toast.makeText(this, "onScroll方法", Toast.LENGTH_SHORT).show();
    	}
    	@Override
    	public void onScrollStateChanged(AbsListView arg0, int state) {
    		// TODO Auto-generated method stub
    		switch (state) {
    		case SCROLL_STATE_FLING:
    			Map<String,Object> map  = new HashMap<String, Object>();
    			map.put("pic", R.drawable.ic_launcher);
    			map.put("txt", "这是增加想");
    			list.add(map);
    			adapter.notifyDataSetChanged();//通知主线程界面刷新数据
    			Toast.makeText(this, "SCROLL_STATE_FLING", Toast.LENGTH_SHORT).show();
    			break;
    		case SCROLL_STATE_IDLE:
    			Toast.makeText(this, "SCROLL_STATE_IDLE", Toast.LENGTH_SHORT).show();
    			break;
    		case SCROLL_STATE_TOUCH_SCROLL:
    			Toast.makeText(this, "SCROLL_STATE_TOUCH_SCROLL", Toast.LENGTH_SHORT).show();
    			break;
    		
    			
    		default:
    			break;
    		}
    	}
    
    	
    
    }
    

      

  • 相关阅读:
    项目--Asp.net全局变量的设置和读(web.config 和 Gloab)
    项目--后台代码提示
    项目--给项目添加提示声音
    项目--正则表达式
    项目--HTML Canvas 和 jQuery遍历
    项目--用户自定义控件
    Bzoj2120/洛谷P1903 数颜色(莫队)
    Poj2482 Stars in Your Window(扫描线)
    Poj2182 Lost Cows(玄学算法)
    Poj3468 A Simple Problem with Integers (分块)
  • 原文地址:https://www.cnblogs.com/Sunnor/p/4715567.html
Copyright © 2011-2022 走看看