android pull to refresh listview 框架使用demo
包含内容: 源码,全套工具
作者QQ549710689
首先布局使用:
<com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/ptrlv" android:layout_width="fill_parent" android:layout_height="fill_parent" > </com.handmark.pulltorefresh.library.PullToRefreshListView>
这个控件就直接是listview 监听上拉和下拉刷新,所以android代码里面需要添加list适配器和上拉和下拉动作监听。
android代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPtrlv=(PullToRefreshListView) findViewById(R.id.ptrlv); //设置模式,BOTH:上拉下拉 //DISABLED:取消上拉下拉 //PULL_FROM_END:只上拉 //PULL_FROM_START:只下拉 mPtrlv.setMode(Mode.BOTH); list=new ArrayList<String>(); initData(); setListener(); mAdapter=new MyAdapter(); mPtrlv.setAdapter(mAdapter); } /** * 初始化模拟数据 */ private void initData() { for (int i = 0; i < 10; i++) { list.add("模拟数据"+i); } } private void setListener() { mPtrlv.setOnRefreshListener(new OnRefreshListener2<ListView>() { /** * 下拉刷新 * 如果需要下拉加载上一页,在这个方法onPullDownToRefresh()做处理 */ int x=1; @Override public void onPullDownToRefresh( PullToRefreshBase<ListView> refreshView) { list.add(0, "下拉刷新<<<上一页:"+x++); //通知数据改变 mAdapter.notifyDataSetChanged(); new MyTask().execute(); } /** * 上拉加载 * 如果需要下拉加载上一页,在这个方法onPullUpToRefresh()做处理 */ int y=1; @Override public void onPullUpToRefresh( PullToRefreshBase<ListView> refreshView) { list.add("上拉加载>>>下一页:"+y++); //通知数据改变 mAdapter.notifyDataSetChanged(); new MyTask().execute(); } }); } /** * 自定义适配器 * @author Endoon * 2015-7-24 09:23:19 */ class MyAdapter extends BaseAdapter{ @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView mView=new TextView(getApplicationContext()); mView.setText(list.get(position)); mView.setTextColor(Color.RED); mView.setGravity(Gravity.CENTER); return mView; } } /** * 异步加载数据 * @author Endoon * 2015-7-24 09:23:12 */ class MyTask extends AsyncTask<Void, Void, Void>{ @Override protected Void doInBackground(Void... params) { return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); //Mark the current Refresh as complete. Will Reset the UI and hide the Refreshing View mPtrlv.onRefreshComplete();//刷新UI } };
代码里面注释写的很清楚,上拉和下拉的监听开关,和消息监听,以及异步刷新监听的子线程。