Android PullToRefresh上拉和下拉刷新
2018-07-20 来源:open-open
Github链接:
https://github.com/chrisbanes/Android-PullToRefresh
GUIDE:
https://github.com/chrisbanes/Android-PullToRefresh/wiki/Quick-Start-Guide
1. 设置Listview
layout文件中定义PullToRefreshListView
Xml代码
<com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" />
2. 设置Mode
PullToRefreshListView mListView = (PullToRefreshListView) findViewById(R.id.list_view); mListView.setMode(Mode.BOTH);
可支持的Mode如下:
- Mode.BOTH:同时支持上拉下拉
- Mode.PULL_FROM_START:只支持下拉Pulling Down
- Mode.PULL_FROM_END:只支持上拉Pulling Up
如果Mode设置成Mode.BOTH,需要设置监听OnRefreshListener2,并实现onPullDownToRefresh()、onPullUpToRefresh()两个方法。
如果Mode设置成Mode.PULL_FROM_START或Mode.PULL_FROM_END,需要设置监听OnRefreshListener,同时实现onRefresh()方法。当然也可以设置为OnRefreshListener2,但是Mode.PULL_FROM_START的时候只调用onPullDownToRefresh()方法,Mode.PULL_FROM的时候只调用onPullUpToRefresh()方法。
4. 具体代码如下:public class MainActivity extends Activity { private ArrayList<String> mListItems; private PullToRefreshListView mListView; private SampleListAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sample); // 设置初期数据 mListItems = new ArrayList<String>(); for (int i = 1; i <= 10; i++) { mListItems.add("Item " + Integer.toString(i)); } // 设置ListView mListView = (PullToRefreshListView) findViewById(R.id.list_view); mAdapter = new SampleListAdapter(); mListView.setAdapter(mAdapter); // 设置PullToRefresh mListView.setMode(Mode.BOTH); mListView.setOnRefreshListener(new OnRefreshListener2<ListView>(){ // 下拉Pulling Down @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { // 下拉的时候数据重置 mListItems = new ArrayList<String>(); for (int i = 1; i <= 10; i++) { mListItems.add("Item " + Integer.toString(i)); } mAdapter.notifyDataSetChanged(); new FinishRefresh().execute(); } // 上拉Pulling Up @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { // 上拉的时候添加选项 int count = mListItems.size(); mListItems.add("Item " + Integer.toString(++count)); mAdapter.notifyDataSetChanged(); new FinishRefresh().execute(); } }); } private class SampleListAdapter extends BaseAdapter { @Override public int getCount() { return mListItems.size(); } @Override public Object getItem(int index) { return mListItems.get(index); } @Override public long getItemId(int index) { return index; } @Override public View getView(int index, View view, ViewGroup arg2) { if(view == null){ LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.list_item, null); } TextView textView = (TextView)view.findViewById(R.id.listItemText); textView.setText(mListItems.get(index)); return view; } } private class FinishRefresh extends AsyncTask<Void, Void, Void>{ @Override protected Void doInBackground(Void... params) { return null; } @Override protected void onPostExecute(Void result){ mListView.onRefreshComplete(); } } }
标签: 代码
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。