Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 刷新實現原理,刷新原理

刷新實現原理,刷新原理

編輯:關於android開發

刷新實現原理,刷新原理


 

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

}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved