zoukankan      html  css  js  c++  java
  • android 学习 ListView使用补充

      前面两篇学习适配器的时候用的就是listview,主要是简单的添加,今晚在看了下listview滚动状态事件和动态加载数据,一个小demo。

    listview的滚动状态主要有三种,onScrollStateChanged

    SCROLL_STATE_IDLE   空闲状态 ,既是滚动停止状态
    SCROLL_STATE_FLING  滚动状态 , 既是滚动中的状态
    SCROLL_STATE_TOUCH_SCROLL  触摸后滚动 , 刚开始滚动的状态
    /**
    * Callback method to be invoked while the list view or grid view is being scrolled. If the
    * view is being scrolled, this method will be called before the next frame of the scroll is
    * rendered. In particular, it will be called before any calls to
    * {@link Adapter#getView(int, View, ViewGroup)}.
    *
    * @param view The view whose scroll state is being reported
    *
    * @param scrollState The current scroll state. One of {@link #SCROLL_STATE_IDLE},
    * {@link #SCROLL_STATE_TOUCH_SCROLL} or {@link #SCROLL_STATE_IDLE}.
    */


    onScroll 函数应该是滚动完成后的回调函数
    firstVisibleItem  该页面第一个可视索引
    visibleItemCount  该页面显示的列表个数
    totalItemCount    整个listview 的item 个数

    源码注释
    * Callback method to be invoked when the list or grid has been scrolled. This will be
    * called after the scroll has completed
    * @param view The view whose scroll state is being reported
    * @param firstVisibleItem the index of the first visible cell (ignore if
    * visibleItemCount == 0)
    * @param visibleItemCount the number of visible cells
    * @param totalItemCount the number of items in the list adaptor
    
    
     adapter.notifyDataSetChanged()  是适配器数据改变之后需要通过该函数,通知listview

    知道这两个回调,就可以做到动态加载数据了,既是在滚动完成后,判断是否到达末尾,如果到达末尾就添加进一个数据,然后通知listview更新。
     
    public class ArrayAdapterActivity extends Activity {
    
        private ListView listView;
        private ArrayAdapter<String> adapter;
        private ArrayList<String>  list;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_array_adapter);
            listView = (ListView)findViewById(R.id.listView3);
            list = new ArrayList<String>();
            adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,getData());
            listView.setAdapter(adapter);
            listView.setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    switch(scrollState){
                        case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:{
                            Log.i("scroll","空闲状态");
                            break;
                        }
                        case AbsListView.OnScrollListener.SCROLL_STATE_FLING:{
                            Log.i("scroll","滚动状态");
                            break;
                        }
                        case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:{
                            Log.i("scroll","触摸后滚动");
                            break;
                        }
                    }
                }
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    if(firstVisibleItem + visibleItemCount == totalItemCount){
                        Log.i("scroll","到达底部");
                        list.add(""+list.size());
                        adapter.notifyDataSetChanged();
                    }
                }
            });
        }
        ArrayList<String> getData(){
            for(int i =0;i<40;i++) {
                list.add("" + i);
            }
            return list;
        }
    }
  • 相关阅读:
    ExtAspNet下通过文档路径实现文档的下载
    ExtjS学习--------Ext.define定义类
    【C语言天天练(二三)】errno变量
    Linux 内核kobject 层次, kset, 和子系统
    Linux 内核释放函数和 kobject 类型
    Linux 内核引用计数的操作
    Linux 内核 kobject 初始化
    Linux 内核 嵌入的 kobjects
    Kobjects, Ksets 和 Subsystems
    Kobjects, Ksets 和 Subsystems
  • 原文地址:https://www.cnblogs.com/techdreaming/p/4931449.html
Copyright © 2011-2022 走看看