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

    下载此实例>>

  • 相关阅读:
    斑马打印交叉线制作方法
    c# 导出2007格式的Excel的连接字符串
    MySql数据库 timeout超时报警的解决方法
    c# 根据域名的到对应的IP
    c# 开发+MySql数据库
    c# datagridview导出Excel文件 问题
    ae GP制作缓冲区分析
    ae 地理坐标与投影坐标转换 [转]
    Dev Winform 简洁界面模板制作
    Dev TreeList 总结
  • 原文地址:https://www.cnblogs.com/tinyphp/p/3849302.html
Copyright © 2020-2023  润新知