• 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;
    		}
    	}
    
    	
    
    }
    

      

  • 相关阅读:
    多线程 线程队列的实现
    如何使用vagrant在虚拟机安装hadoop集群
    【leetcode】Rotate List
    麒麟(Kylin)与Ubuntu的寓意相通
    山寨APP恶意吸费隐患 门槛底致监管盲点
    2013年软件设计师之考前复习笔记:IP地址
    .gitkeep
    cmd 里面运行git提示“不是内部或外部命令,也不是可运行的程序”的解决办法...
    windows常用命令行命令
    PhpStorm terminal无法输入命令的解决方法
  • 原文地址:https://www.cnblogs.com/Sunnor/p/4715567.html
Copyright © 2020-2023  润新知