码迷,mamicode.com
首页 > 移动开发 > 详细

Android -- PullToRefresh应用

时间:2014-12-16 00:55:43      阅读:452      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   ar   color   sp   for   

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应用

标签:android   style   blog   http   io   ar   color   sp   for   

原文地址:http://www.cnblogs.com/yydcdut/p/4166123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!