android下拉刷新,上拉获取更多

来源:互联网 发布:薛之谦 上海炮王 知乎 编辑:程序博客网 时间:2024/05/07 18:42

很经典的功能,可以尝试去写写,项目赶的很,就借用了别人的。github上搜索的,选择了 chrisbanes/Android-PullToRefresh(https://github.com/chrisbanes/Android-PullToRefresh)。效果实现了,在此记录下。

1、配置环境,这个比较简单,下载后作为一个library直接导入;

2、实现一个下拉刷新和上拉获取更多,(借鉴自带的一个listview):

MainActivity.java

package com.ttdevs.pullrefresh;import java.util.Arrays;import java.util.LinkedList;import android.app.ListActivity;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.Mode;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;import com.handmark.pulltorefresh.library.PullToRefreshListView;public class MainActivity extends ListActivity {private PullToRefreshListView mPullRefreshListView;private LinkedList<String> mListItems;private ArrayAdapter<String> mAdapter;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);mPullRefreshListView.setMode(Mode.BOTH);mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {@Overridepublic void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {Toast.makeText(getApplicationContext(), "下拉刷新", Toast.LENGTH_LONG).show();new GetDataTask().execute();}@Overridepublic void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {Toast.makeText(getApplicationContext(), "上拉获取更多", Toast.LENGTH_LONG).show();new GetDataTask().execute();}});ListView actualListView = mPullRefreshListView.getRefreshableView();// Need to use the Actual ListView when registering for Context MenuregisterForContextMenu(actualListView);mListItems = new LinkedList<String>();mListItems.addAll(Arrays.asList(mStrings));mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);actualListView.setAdapter(mAdapter);}private class GetDataTask extends AsyncTask<Void, Void, String[]> {@Overrideprotected String[] doInBackground(Void... params) {try {Thread.sleep(1000);} catch (InterruptedException e) {}return mStrings;}@Overrideprotected void onPostExecute(String[] result) {mListItems.addFirst("Added after refresh...");mAdapter.notifyDataSetChanged();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" };}


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <!-- The PullToRefreshListView replaces a standard ListView widget. -->    <com.handmark.pulltorefresh.library.PullToRefreshListView        android:id="@+id/pull_refresh_list"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:cacheColorHint="#00000000"        android:divider="#19000000"        android:dividerHeight="4dp"        android:fadingEdge="none"        android:fastScrollEnabled="false"        android:footerDividersEnabled="false"        android:headerDividersEnabled="false"        android:smoothScrollbar="true" /></LinearLayout>

3、主要是拷贝demo中的listview那个,实现的监听器是OnRefreshListener2,看了下源码,OnRefreshListener和OnRefreshListener2只可以设置一个,也就是最后设置的那个有效,最后就是设置滑动的模式:mPullRefreshListView.setMode(Mode.BOTH);这个很重要,否则上拉无效,最后效果如下:

4、Chrisbanes的PullToRefresh:下载