ListView+适配器
扫描二维码
随时随地手机看文章
public class MainActivity extends ActionBarActivity implements ListView.OnItemClickListener,ListView.OnScrollListener { private ListView listView; private ArrayAdapter arrayAdapter; private SimpleAdapter simpleAdapter; private List<Map> data_list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView)findViewById(R.id.listView); String[] data = {"列表1","列表2","列表3","列表4","列表5"}; data_list = new ArrayList<Map>(); arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,data); /** * 上下文 * 数据源 * 布局xml * 数据源的字段 * 布局xml中对应的字段 */ simpleAdapter = new SimpleAdapter(this,getData(),R.layout.item,new String[]{"pic","text"},new int[]{R.id.pic,R.id.text}); //listView.setAdapter(arrayAdapter); listView.setAdapter(simpleAdapter); listView.setOnItemClickListener(this); listView.setOnScrollListener(this); } private List<Map> getData(){ for(int i = 0; i < 40 ; ++i){ Mapmap = new HashMap(); map.put("pic",R.mipmap.ic_launcher); map.put("text", "文本" + i); data_list.add(map); } return data_list; } @Override public void onItemClick(AdapterView parent, View view, int position, long id) { String str = listView.getItemAtPosition(position).toString(); Toast.makeText(getApplicationContext(),position+"=>"+str,Toast.LENGTH_SHORT).show(); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState){ case SCROLL_STATE_FLING: Log.i("main", "手指快速滑动屏幕并离开,导致视图一直滑动"); Mapmap = new HashMap(); //简单的滑动刷新 map.put("pic",R.mipmap.ic_launcher); map.put("text", "新的文本"+ System.currentTimeMillis()); data_list.add(map); listView.deferNotifyDataSetChanged(); //通知数据源 break; case SCROLL_STATE_IDLE: Log.i("main","视图停止滑动"); break; case SCROLL_STATE_TOUCH_SCROLL: Log.i("main","手指真正屏幕上滑动"); break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } }
item.xml