• 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/Study_Demos/tree/master/PullToReFreshDemo

  • 相关阅读:
    Delphi Code Editor 之 几个特性(转)
    如何访问局域网的Access数据库?
    Delphi Live Bindings 初探
    重装Delphi10.2的IDE必要设置
    TClientDataSet数据源设置
    js删除数组里的某个数据
    初识localstorage用法
    Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.
    css实现文本溢出用...显示
    原生js和jquery
  • 原文地址:https://www.cnblogs.com/yydcdut/p/4166123.html
Copyright © 2020-2023  润新知