• 刷新实现原理


    package com.zzw.testrefresh;
    
    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.SystemClock;
    import android.widget.AbsListView;
    import android.widget.AbsListView.OnScrollListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    public class MainActivity extends Activity implements OnScrollListener {
    
        private ArrayList<String> data;
        private ArrayAdapter<String> adapter;
    
        private int firstVisibleItem;
        private int visibleItemCount;
        private int totalItemCount;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            data = new ArrayList<String>();
            for (int i = 0; i < 20; i++) {
                data.add("原有数据--" + i);
            }
    
            ListView mListView = (ListView) findViewById(R.id.listView);
    
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
    
            mListView.setAdapter(adapter);
    
            mListView.setOnScrollListener(this);
        }
    
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (OnScrollListener.SCROLL_STATE_IDLE == scrollState) {
    
                if (firstVisibleItem == 0) {
                    addTop();
                }
    
                if (firstVisibleItem + visibleItemCount == totalItemCount) {
                    addBottom();
                }
    
            }
        }
    
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            this.firstVisibleItem = firstVisibleItem;
            this.visibleItemCount = visibleItemCount;
            this.totalItemCount = totalItemCount;
        }
    
        private void addTop() {
            data.add(0, "头部添加数据--" + (totalItemCount + 1));
            adapter.notifyDataSetChanged();
        }
    
        private void addBottom() {
            data.add("尾部添加数据--" + (totalItemCount + 1));
            adapter.notifyDataSetChanged();
        }
    
    }
  • 相关阅读:
    20170926-构建之法:现代软件工程-阅读笔记
    我的swift的ui标签
    内存管理:内存泄漏和空悬指针
    闭包
    泛型,修饰符和异常处理
    类型转换,接口和扩展
    初始化2
    类的继承和初始化1
    枚举与可选值
    swift中的类和结构
  • 原文地址:https://www.cnblogs.com/zzw1994/p/5048995.html
Copyright © 2020-2023  润新知