码迷,mamicode.com
首页 > 移动开发 > 详细

Android:监听ListView

时间:2014-07-17 21:36:55      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   java   color   

本文目录

监听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);            
        }
    }
    
}

 bubuko.com,布布扣

 监听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>

bubuko.com,布布扣

下载此实例>>

Android:监听ListView,布布扣,bubuko.com

Android:监听ListView

标签:android   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/tinyphp/p/3849302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!