首页 > 代码库 > Android -- PullToRefresh应用
Android -- PullToRefresh应用
PullToRefresh
支持ListView、ExpandableListView、GridView、WebView
下载地址:https://github.com/chrisbanes/Android-PullToRefresh
注意,若是要放到android studio中的话,需要在eclipse中以android studio工程的方式导出。
Code
布局:
<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:orientation="vertical" tools:context=".MyActivity"> <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/lv_main" android:layout_width="fill_parent" android:layout_height="fill_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView></LinearLayout>
配置:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); mList = new ArrayList<String>(); for (int i = 0; i < mStr.length; i++) { mList.add(mStr[i]); } mListView = (PullToRefreshListView) findViewById(R.id.lv_main);// mListView.setOnRefreshListener(new MyRefreshListener()); mListView.setOnRefreshListener(new MyRefreshListener2()); mMyAdapter = new MyAdapter(); mListView.setAdapter(mMyAdapter); mListView.setMode(PullToRefreshBase.Mode.BOTH); mListView.getLoadingLayoutProxy().setLastUpdatedLabel("setLastUpdatedLabel"); mListView.getLoadingLayoutProxy().setLoadingDrawable(getResources().getDrawable(R.drawable.ic_launcher)); mListView.getLoadingLayoutProxy().setPullLabel("setPullLabel"); mListView.getLoadingLayoutProxy().setRefreshingLabel("setRefreshingLabel"); mListView.getLoadingLayoutProxy().setReleaseLabel("setReleaseLabel"); }
PullToRefreshBase.Mode.BOTH指的是下拉和上拉刷新两张模式。
class MyRefreshListener implements PullToRefreshBase.OnRefreshListener { @Override public void onRefresh(PullToRefreshBase refreshView) { } } class MyRefreshListener2 implements PullToRefreshBase.OnRefreshListener2 { @Override public void onPullDownToRefresh(PullToRefreshBase refreshView) { mList.clear(); for (int i = 0; i < mStr.length; i++) { mList.add(mStr[i]); } new Thread(new MyRunnable()).start(); } @Override public void onPullUpToRefresh(PullToRefreshBase refreshView) { for (int i = 0; i < mStr.length; i++) { mList.add(mStr[i]); } new Thread(new MyRunnable()).start(); } }
这里有两个监听器,我们来简单说一下PullToRefreshBase.OnRefreshListener2 ,很明显,分别指的是一个是上拉,一个是下拉的刷新的回调。
public class MyActivity extends Activity { private PullToRefreshListView mListView;//pulltorefresh控件 private List<String> mList;//存放数据 private String[] mStr = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"};//数据 private MyAdapter mMyAdapter;//适配器 //一个延时,然后处理 private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 1: Log.i("handleMessage", "handleMessage"); mListView.onRefreshComplete(); mMyAdapter.notifyDataSetChanged(); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); mList = new ArrayList<String>();//初始化 for (int i = 0; i < mStr.length; i++) { mList.add(mStr[i]);//准备数据 } mListView = (PullToRefreshListView) findViewById(R.id.lv_main);// mListView.setOnRefreshListener(new MyRefreshListener()); mListView.setOnRefreshListener(new MyRefreshListener2());//设置监听器 mMyAdapter = new MyAdapter();//初始化适配器 mListView.setAdapter(mMyAdapter); mListView.setMode(PullToRefreshBase.Mode.BOTH);//模式为上拉和下拉 mListView.getLoadingLayoutProxy().setLastUpdatedLabel("setLastUpdatedLabel"); mListView.getLoadingLayoutProxy().setLoadingDrawable(getResources().getDrawable(R.drawable.ic_launcher));//loading的时候的图片 mListView.getLoadingLayoutProxy().setPullLabel("setPullLabel"); mListView.getLoadingLayoutProxy().setRefreshingLabel("setRefreshingLabel"); mListView.getLoadingLayoutProxy().setReleaseLabel("setReleaseLabel"); } class MyRefreshListener implements PullToRefreshBase.OnRefreshListener { @Override public void onRefresh(PullToRefreshBase refreshView) { } } class MyRefreshListener2 implements PullToRefreshBase.OnRefreshListener2 { @Override public void onPullDownToRefresh(PullToRefreshBase refreshView) { mList.clear(); for (int i = 0; i < mStr.length; i++) {//添加数据 mList.add(mStr[i]); } new Thread(new MyRunnable()).start();//执行延时 } @Override public void onPullUpToRefresh(PullToRefreshBase refreshView) { for (int i = 0; i < mStr.length; i++) {//添加数据 mList.add(mStr[i]); } new Thread(new MyRunnable()).start();//执行延时 } } class MyAdapter extends BaseAdapter { @Override public int getCount() { return mList.size(); } @Override public Object getItem(int i) { return mList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if (view == null) { view = LayoutInflater.from(MyActivity.this).inflate(R.layout.item, null); TextView tv = (TextView) view.findViewById(R.id.txt); tv.setText(mList.get(i)); } else { TextView tv = (TextView) view.findViewById(R.id.txt); tv.setText(mList.get(i)); } return view; } } class MyRunnable implements Runnable { @Override public void run() { try { Thread.sleep(2000);//延时2s } catch (InterruptedException e) { e.printStackTrace(); } Log.i("MyRunnable", "MyRunnable"); mHandler.sendEmptyMessage(1); } }}
我是天王盖地虎的分割线
源代码:https://github.com/pinguo-yuyidong/PullToRefresh
Android -- PullToRefresh应用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。