第三方开源的下拉刷新PullToRefresh

本文详细介绍了Android开发中常用的第三方下拉刷新库PullToRefresh的使用方法和核心原理,包括如何集成、配置及自定义样式,帮助开发者实现更佳的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


package com.example.pulltorefresh;

import java.util.ArrayList;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
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;
import android.widget.TextView;

public class MainActivity extends Activity {

	private	ArrayList<String> data;
	private	int count=0;
	private	ArrayAdapter adapter;
	private	PullToRefreshListView mListView;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		
		data=new ArrayList<String>();
		
		mListView=(PullToRefreshListView) findViewById(R.id.list_view);
		mListView.setMode(Mode.PULL_FROM_START);
		mListView.setOnRefreshListener(new OnRefreshListener<ListView>(){

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				//完成你的业务逻辑
				new MyTask().execute();
			}});
		
		adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);
		mListView.setAdapter(adapter);
		
		TextView text=new TextView(this);
		text.setText("请下拉刷新");
		mListView.setEmptyView(text);
	}
	
	private	class	MyTask extends	AsyncTask{

		@Override
		protected void onPreExecute() {
			mListView.setRefreshing();
		}
		
		@Override
		protected Object doInBackground(Object... params) {
			
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			return count++;
		}
		
		@Override
		protected void onPostExecute(Object result) {
			data.add(0, "更新数据"+result);
			
			mListView.setLastUpdatedLabel("最后更新时间:11月24日16:04"+System.currentTimeMillis());
			
			adapter.notifyDataSetChanged();
			mListView.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.pulltorefresh.MainActivity" >

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

开源来源(jithub):https://github.com/chrisbanes/Android-PullToRefresh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值