码迷,mamicode.com
首页 > 移动开发 > 详细

Android 学习之 开源项目PullToRefresh的使用

时间:2017-05-13 20:57:28      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:private   except   activity   ast   ide   java   fast   utf-8   oncreate   



技术分享





首先 下载 Android-PullToRefresh-master

技术分享

下载地址  https://github.com/chrisbanes/Android-PullToRefresh




下载之后将其解压

技术分享


如今  我们用eclipse 创建一个项目取名PullToRefresh


将上面的library 引入我们的项目

引入成功之后打开项目的project.properties文件我们能够看到

android.library.reference.1=../Android-PullToRefresh-master/library

这样就表示能够引用成功了



我们在res/layout创建 布局文件main.xml



<?

xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <!-- xmlns:ptr = "http://schemas.android.com/apk/res-auto" 为我们要使用PullToRefresh 里面一些属性须要引的命名空间 --> <com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr = "http://schemas.android.com/apk/res-auto" android:id="@+id/pull_refresh_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:dividerHeight="4dp" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true" ptr:ptrMode="both" /> </LinearLayout>




接着创建 MainActivity.java


package com.pulltorefresh;

import java.util.Arrays;
import java.util.LinkedList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshBase.State;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.extras.SoundPullEventListener;



public class MainActivity extends Activity {
	
	
	static final int MENU_MANUAL_REFRESH = 0;
	static final int MENU_DISABLE_SCROLL = 1;
	static final int MENU_SET_MODE = 2;
	static final int MENU_DEMO = 3;
	
	private LinkedList<String> mListItems;
	private PullToRefreshListView mPullRefreshListView;
	private ArrayAdapter<String> mAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
		
		
		/**
		 * 实现 接口  OnRefreshListener2<ListView>  以便与监听  滚动栏到顶部和究竟部
		 */
		mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {
			@Override
			public void onPullDownToRefresh( PullToRefreshBase<ListView> refreshView) {
				Toast.makeText(MainActivity.this, "onPullDownToRefresh", Toast.LENGTH_SHORT).show();
				new GetDataTask().execute();
			}
			@Override
			public void onPullUpToRefresh( PullToRefreshBase<ListView> refreshView) {
				Toast.makeText(MainActivity.this, "onPullUpToRefresh", Toast.LENGTH_SHORT).show();
				new GetDataTask().execute();
			}
		});

	

		ListView actualListView = mPullRefreshListView.getRefreshableView();

		// Need to use the Actual ListView when registering for Context Menu
		registerForContextMenu(actualListView);

		mListItems = new LinkedList<String>();
		mListItems.addAll(Arrays.asList(mStrings));

		mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);

		/**
		 * Add Sound Event Listener
		 */
		
		/**
		 *   设置下拉刷新和上拉载入时的 铃声(可有可无)
		 */
		SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(this);
		soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
		soundListener.addSoundEvent(State.RESET, R.raw.reset_sound);
		soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
		mPullRefreshListView.setOnPullEventListener(soundListener);

		// You can also just use setListAdapter(mAdapter) or
		// mPullRefreshListView.setAdapter(mAdapter)
		actualListView.setAdapter(mAdapter);
		
		
		
	}
	//模拟网络载入数据的   异步请求类
	//
	private class GetDataTask extends AsyncTask<Void, Void, String[]> {

		//子线程请求数据
		@Override
		protected String[] doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
			}
			return mStrings;
		}

		//主线程更新UI
		@Override
		protected void onPostExecute(String[] result) {
			
			//向RefreshListView Item 加入一行数据  并刷新ListView
			//mListItems.addLast("Added after refresh...");
			mListItems.addFirst("Added after refresh...");
			mAdapter.notifyDataSetChanged();

			//通知RefreshListView 我们已经更新完毕
			// Call onRefreshComplete when the list has been refreshed.
			mPullRefreshListView.onRefreshComplete();

			super.onPostExecute(result);
		}
	}
	
	
	
	//数据源
	private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
			"Acorn", "Adelost", "Affidelice au Chablis", "Afuega‘l Pitu", "Airag", "Airedale", "Aisy Cendre",
			"Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
			"Acorn", "Adelost", "Affidelice au Chablis", "Afuega‘l Pitu", "Airag", "Airedale", "Aisy Cendre",
			"Allgauer Emmentaler" };
}



眼下编码已经完毕  我们測试一下




技术分享                      技术分享




到此我们学习完毕








Android 学习之 开源项目PullToRefresh的使用

标签:private   except   activity   ast   ide   java   fast   utf-8   oncreate   

原文地址:http://www.cnblogs.com/liguangsunls/p/6850237.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!