pulltorefresh:QQ下拉刷新应用

首先到此网址下载第三方包https://github.com/chrisbanes/Android-PullToRefresh

点击打开链接

下载完成之后找到library文件夹  将此文件夹导入到eclipse当中并勾选islibraray。新建的工程里面add library;

然后开始写代码

package com.example.pulltorefresh1;

import java.util.ArrayList;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
	private ArrayList<String> data;
	PullToRefreshListView pulltorefresh;
	ArrayAdapter<String> array;
	private int count;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		data = new ArrayList<String>();

		pulltorefresh = (PullToRefreshListView) findViewById(R.id.pulll);

		pulltorefresh.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				new MyTask().execute();
			}
		});
		array = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
		pulltorefresh.setAdapter(array);

	}

	public class MyTask extends AsyncTask {

		@Override
		protected Object doInBackground(Object... params) {
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return count++;
		}

		@Override
		protected void onPostExecute(Object result) {
			data.add(0, result + "");
			array.notifyDataSetChanged();
			pulltorefresh.onRefreshComplete();

		}

	}

}
接下来是布局文件

<RelativeLayout 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.example.pulltorefresh1.MainActivity" >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pulll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值