你的位置:首页 > 操作系统

[操作系统]刷新实现原理


 

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();  }}