Android:监听ListView
本文目录
监听ListView点击事件
使用监听器OnItemClickListener
package com.example.testsend; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends Activity{ private ArrayAdapter<String> arr_adapt; private ListView list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); list =(ListView)findViewById(R.id.list); String[] arr_data = {"数据1","数据2","数据3"}; arr_adapt =new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr_data); list.setAdapter(arr_adapt); ListClickListener l = new ListClickListener(); list.setOnItemClickListener(l); } //监听类 class ListClickListener implements OnItemClickListener{ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub String text = list.getItemAtPosition(position)+""; Log.i("tag",text); } } }
监听ListView滚动事件
使用监听器ListScrollListener
package com.example.testsend; 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.util.Log; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity{ private ListView list; private SimpleAdapter sim_adapter; private List<Map<String, Object>>dataList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); list =(ListView)findViewById(R.id.list); //填充数据 dataList = new ArrayList<Map<String,Object>>(); for(int i=0;i<20;i++){ Map<String, Object>map = new HashMap<String,Object>(); map.put("pic", R.drawable.ic_launcher); map.put("text","我的数据"+i); dataList.add(map); } sim_adapter = new SimpleAdapter(this, dataList, R.layout.item,new String[]{"pic","text"},new int[]{R.id.pic,R.id.text}); list.setAdapter(sim_adapter); //监听 ListScrollListener l = new ListScrollListener(); list.setOnScrollListener(l); } //监听类 class ListScrollListener implements OnScrollListener{ @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub //每一条数据都是一个Map switch(scrollState){ case SCROLL_STATE_FLING: Log.i("Main","用户在手指离开屏幕之前,由于滑了一下,视图仍然依靠惯性继续滑动"); //添加一条数据 Map<String, Object>map = new HashMap<String,Object>(); map.put("pic", R.drawable.ic_launcher); map.put("text","新的在这里啊!"); dataList.add(map); //刷新 sim_adapter.notifyDataSetChanged(); 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) { // TODO Auto-generated method stub手指跟屏幕接触就触发 Log.i("onscroll", "测试"); } } }
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/pic" android:layout_marginLeft="6dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:text="文字" /> </LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。