PullToRefresh的使用
下載地址:https://github.com/chrisbanes/Android-PullToRefresh
1、改變控件頂部和底部是否可拉動(dòng)。
1)、在代碼中調(diào)用控件的 setMode(Mode.BOTH)方法;
2)、在配置文件中引用 xmlns:ptr="http://schemas.android.com/apk/res-auto" 命名空間,
設(shè)置ptr:ptrMode="both"
2、改變控件頂部和底部拖動(dòng)時(shí)顯示的圖標(biāo)和文字。
1)、在代碼中設(shè)置
//上端
ILoadingLayout loadingLayout = mPullRefreshListView.getLoadingLayoutProxy(true,false);
loadingLayout.setPullLabel("下拉刷新");
loadingLayout.setRefreshingLabel("正在刷新...");
loadingLayout.setReleaseLabel("放開刷新...");
loadingLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.default_ptr_flip));
//下端
loadingLayout = mPullRefreshListView.getLoadingLayoutProxy(false,true);
loadingLayout.setPullLabel("上拉加載");
loadingLayout.setRefreshingLabel("正在加載...");
loadingLayout.setReleaseLabel("放開加載...");
loadingLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.default_ptr_flip));
2)、在配置文件中引用 xmlns:ptr="http://schemas.android.com/apk/res-auto" 命名空間,
設(shè)置ptr:ptrMode="ptrDrawable"
3)、到library中values查看attr.xml提供的配置文件提供的屬性。
1)、 在代碼中提供設(shè)置屬性的方法在ILoadingLayout類中。
2)、在配置文件中設(shè)置屬性引用命名空間,提供屬性名稱。如:ptr:ptrMode="both"
4)、實(shí)例代碼
public final class PullToRefreshListActivity extends ListActivity {
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;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ptr_list);
mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
mPullRefreshListView.setMode(Mode.BOTH);
// Set a listener to be invoked when the list should be refreshed.
/* mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
// Update the LastUpdatedLabel
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
// Add an end-of-list listener
mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
Toast.makeText(PullToRefreshListActivity.this, "End of List!", Toast.LENGTH_SHORT).show();
}
});*/
/**
* 設(shè)置兩端加載時(shí)顯示的內(nèi)容
*/
//上端
ILoadingLayout loadingLayout = mPullRefreshListView.getLoadingLayoutProxy(true,false);
loadingLayout.setPullLabel("下拉刷新");
loadingLayout.setRefreshingLabel("正在刷新...");
loadingLayout.setReleaseLabel("放開刷新...");
//loadingLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.default_ptr_flip));
//下端
loadingLayout = mPullRefreshListView.getLoadingLayoutProxy(false,true);
loadingLayout.setPullLabel("上拉加載");
loadingLayout.setRefreshingLabel("正在加載...");
loadingLayout.setReleaseLabel("放開加載...");
//loadingLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.default_ptr_flip));
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
System.out.println("down");
new GetDataTask().execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
System.out.println("up");
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(4000);
} catch (InterruptedException e) {
}
return mStrings;
}
@Override
protected void onPostExecute(String[] result) {
mListItems.addFirst("Added after refresh...");
mAdapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_MANUAL_REFRESH, 0, "Manual Refresh");
menu.add(0, MENU_DISABLE_SCROLL, 1,
mPullRefreshListView.isScrollingWhileRefreshingEnabled() ? "Disable Scrolling while Refreshing"
: "Enable Scrolling while Refreshing");
menu.add(0, MENU_SET_MODE, 0, mPullRefreshListView.getMode() == Mode.BOTH ? "Change to MODE_PULL_DOWN"
: "Change to MODE_PULL_BOTH");
menu.add(0, MENU_DEMO, 0, "Demo");
return super.onCreateOptionsMenu(menu);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle("Item: " + getListView().getItemAtPosition(info.position));
menu.add("Item 1");
menu.add("Item 2");
menu.add("Item 3");
menu.add("Item 4");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem disableItem = menu.findItem(MENU_DISABLE_SCROLL);
disableItem
.setTitle(mPullRefreshListView.isScrollingWhileRefreshingEnabled() ? "Disable Scrolling while Refreshing"
: "Enable Scrolling while Refreshing");
MenuItem setModeItem = menu.findItem(MENU_SET_MODE);
setModeItem.setTitle(mPullRefreshListView.getMode() == Mode.BOTH ? "Change to MODE_FROM_START"
: "Change to MODE_PULL_BOTH");
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_MANUAL_REFRESH:
new GetDataTask().execute();
mPullRefreshListView.setRefreshing(false);
break;
case MENU_DISABLE_SCROLL:
mPullRefreshListView.setScrollingWhileRefreshingEnabled(!mPullRefreshListView
.isScrollingWhileRefreshingEnabled());
break;
case MENU_SET_MODE:
mPullRefreshListView.setMode(mPullRefreshListView.getMode() == Mode.BOTH ? Mode.PULL_FROM_START
: Mode.BOTH);
break;
case MENU_DEMO:
mPullRefreshListView.demo();
break;
}
return super.onOptionsItemSelected(item);
}
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" };
}
5)、配置文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<!-- 背景色 -->
ptr:ptrAdapterViewBackground="#660"
<!-- 拉動(dòng)模式,兩端拉動(dòng) -->
ptr:ptrMode="both">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:text="@string/filler_text"
android:textSize="16sp" />
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</LinearLayout>
創(chuàng)新互聯(lián)建站專業(yè)提供成都主機(jī)托管四川主機(jī)托管成都服務(wù)器托管四川服務(wù)器托管,支持按月付款!我們的承諾:貴族品質(zhì)、平民價(jià)格,機(jī)房位于中國電信/網(wǎng)通/移動(dòng)機(jī)房,大邑服務(wù)器托管服務(wù)有保障!
分享題目:PullToRefresh的使用
文章源于:http://jinyejixie.com/article20/pggsjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、用戶體驗(yàn)、外貿(mào)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、網(wǎng)站改版、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)