<?xml version="1.0" encoding="utf-8"?> <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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <!-- 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" /> </RelativeLayout>
package tech.androidstudio.pulltorefresh; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.AbsListView; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.Toast; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import com.handmark.pulltorefresh.library.extras.SoundPullEventListener; import java.util.Arrays; import java.util.LinkedList; public class MainActivity extends AppCompatActivity { 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; private int mPage=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); /** * 实现 接口 OnRefreshListener2<ListView> 以便与监听 滚动条到顶部和到底部 */ mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { Toast.makeText(MainActivity.this, "onPullDownToRefresh", Toast.LENGTH_SHORT).show(); mPage=1; new GetDataTask().execute(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { Toast.makeText(MainActivity.this, "onPullUpToRefresh", Toast.LENGTH_SHORT).show(); mPage++; 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(PullToRefreshBase.State.PULL_TO_REFRESH, R.raw.pull_event); // soundListener.addSoundEvent(PullToRefreshBase.State.RESET, R.raw.reset_sound); // soundListener.addSoundEvent(PullToRefreshBase.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..."); if(mPage==1){ mListItems.clear(); mListItems.addAll(Arrays.asList(mStrings)); }else { 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" }; }
完整的代码:
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <in.srain.cube.views.ptr.PtrFrameLayout android:id="@+id/store_house_ptr_frame" xmlns:cube_ptr="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" cube_ptr:ptr_resistance="1.7" cube_ptr:ptr_ratio_of_header_height_to_refresh="1.2" cube_ptr:ptr_duration_to_close="300" cube_ptr:ptr_duration_to_close_header="2000" cube_ptr:ptr_keep_header_when_refresh="true" cube_ptr:ptr_pull_to_fresh="false" > <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/list_item"></ListView> </in.srain.cube.views.ptr.PtrFrameLayout> </LinearLayout>
package tech.androidstudio.ultrapulltorefreshdemo; import android.os.AsyncTask; import android.os.Build; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AbsListView; import android.widget.Adapter; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.Arrays; import in.srain.cube.views.ptr.PtrDefaultHandler; import in.srain.cube.views.ptr.PtrFrameLayout; import in.srain.cube.views.ptr.PtrHandler; import in.srain.cube.views.ptr.PtrUIHandler; import in.srain.cube.views.ptr.header.StoreHouseHeader; import in.srain.cube.views.ptr.indicator.PtrIndicator; public class MainActivity extends AppCompatActivity implements AbsListView.OnScrollListener { private ArrayList<String> list; private ArrayAdapter<String> adapter; private int mPage=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView =(ListView)findViewById(android.support.v7.appcompat.R.id.list_item); list = new ArrayList<String>(); adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list); listView.setAdapter(adapter); listView.setOnScrollListener(this); final PtrFrameLayout ptrFrameLayout = (PtrFrameLayout)findViewById(R.id.store_house_ptr_frame); StoreHouseHeader header = new StoreHouseHeader(this); header.setPadding(0, 20, 0, 20); header.initWithString("Updating"); ptrFrameLayout.setDurationToCloseHeader(2500); ptrFrameLayout.setHeaderView(header); ptrFrameLayout.addPtrUIHandler(header); ptrFrameLayout.setPtrHandler(new PtrHandler() { @Override public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) { return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header); } @Override public void onRefreshBegin(PtrFrameLayout frame) { mPage=1; new GetDataTask().execute(); ptrFrameLayout.postDelayed(new Runnable() { @Override public void run() { ptrFrameLayout.refreshComplete(); } }, 1500); } }); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if(scrollState==AbsListView.OnScrollListener.SCROLL_STATE_IDLE&& view.getLastVisiblePosition()>=list.size()-1){ mPage++; new GetDataTask().execute(); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } //模拟网络加载数据的 异步请求类 // 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 new String[]{"hello"}; } //主线程更新UI @Override protected void onPostExecute(String[] result) { if(mPage==1) { list.clear(); list.addAll(Arrays.asList(array)); }else{ list.add("new"); } adapter.notifyDataSetChanged(); //向RefreshListView Item 添加一行数据 并刷新ListView //mListItems.addLast("Added after refresh..."); super.onPostExecute(result); } } private String[] array={"0","1","2","3","1","2","3","1","2","3","1","2","3","1","2","3","1","2","3"}; }