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

      

  • 相关阅读:
    Vsftp的PASV mode(被动模式传送)和Port模式及 Linux下VsFTP配置全方案
    vsftpd:500 OOPS: vsftpd: refusing to run with writable root inside chroot ()错误的解决方法
    CentOS7.2部署FTP
    Apache与Nginx的优缺点比较
    MySQL存储引擎--MyISAM与InnoDB区别
    CentOS 7下搭建配置SVN服务器
    Remi 安装源
    tmpx75 I2C 温度传感器驱动程序添加
    QT 5.7.0 交叉编译记录
    am335x SGX 移植
  • 原文地址:https://www.cnblogs.com/Sunnor/p/4715567.html
Copyright © 2020-2023  润新知