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; } } }
时间: 2024-12-17 17:16:32