上拉下拉刷新(1)

上拉下拉刷新(1)_第1张图片上拉下拉刷新(1)_第2张图片

布局中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zhangli.xiala.MainActivity" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>

</LinearLayout>


代码中:

package com.zhangli.xiala;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

	private ArrayList<String> date;
	private ArrayAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		date = new ArrayList<String>();
		for (int i = 0; i < 20; i++) {
			date.add("" + i);
		}

		ListView listView = (ListView) findViewById(R.id.listView);
		adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, date);
		listView.setAdapter(adapter);

		listView.setOnScrollListener(new OnScrollListener() {

			private int firstVisibleItem, visibleItemCount, totalItemCount;
			// firstVisibleItem:第一条数据
			// visibleItemCount:屏幕显示的数据数量
			// totalItemCount:总的数据数量
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// SCROLL_STATE_IDLE表示刷新完成
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
					Log.d("000000000", "firstVisibleItem=" + firstVisibleItem + ",visibleItemCount=" + visibleItemCount
							+ ",totalItemCount=" + totalItemCount);
					boolean b = (firstVisibleItem + visibleItemCount) < totalItemCount;
					if (firstVisibleItem == 0) {
						Topadd();

					} else if (firstVisibleItem == totalItemCount - visibleItemCount) {
						bottomadd();

					}
				}
			}
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				this.firstVisibleItem = firstVisibleItem;
				this.visibleItemCount = visibleItemCount;
				this.totalItemCount = totalItemCount;
			}
		});
	}

	public void Topadd() {
		date.add(0, "头部数据:" + date.size());
		adapter.notifyDataSetChanged();
	}

	public void bottomadd() {
		date.add("底部数据:" + date.size());
		adapter.notifyDataSetChanged();
	}
}


 上拉下拉刷新(2):http://blog.csdn.net/zhangli_/article/details/50332475

 

你可能感兴趣的:(上拉下拉刷新(1))