码迷,mamicode.com
首页 > 其他好文 > 详细

PullToRefreshListView的使用

时间:2016-06-02 11:06:41      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

 

public class MainActivity extends Activity {

        //MENU itemid
        /**手动执行刷新*/
        static final int MENU_MANUAL_REFRESH = 0;
        /** 数据加载中是否允许滑动*/
        static final int MENU_DISABLE_SCROLL = 1;
        /**刷新模式,
         * 包括{
         * 1.DISABLED(0x0)              禁止通过手势和手动执行
         * 2.PULL_FROM_START(0x1)        可执行下拉刷新
         * 3.PULL_FROM_END(0x2)            可执行上拉刷新
         * 3.BOTH(0x3)                    上下都可执行
         * 4.MANUAL_REFRESH_ONLY(0x4)    禁止通过手势执行,但可以手动设置
         * }*/
        static final int MENU_SET_MODE = 2;
        //MENU itemid end
        /**这里我们来判断是下拉还是上拉*/
        private PullToRefreshBase.Mode CurrentMode;

        private LinkedList<String> mListItems;

        private PullToRefreshListView mPullRefreshListView;

        private ArrayAdapter<String> mAdapter;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ptr_list);

            mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);

            //监听列表被刷新时事件.
            mPullRefreshListView
                    .setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
                        @Override
                        public void onRefresh(
                                PullToRefreshBase<ListView> refreshView) {

                            int flags=DateUtils.FORMAT_SHOW_TIME
                                    | DateUtils.FORMAT_SHOW_DATE
                                    | DateUtils.FORMAT_ABBREV_ALL;

                            String label = DateUtils.formatDateTime(
                                    getApplicationContext(),
                                    System.currentTimeMillis(),flags
                            );
                            CurrentMode=refreshView.getCurrentMode();
                            // 更新最后刷新时间
                            refreshView.getLoadingLayoutProxy()
                                    .setLastUpdatedLabel(label);

                            // 执行加载更多数据任务.
                            new GetDataTask().execute();
                        }
                    });

            //监听滑动到底部的事件
            mPullRefreshListView
                    .setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {

                        @Override
                        public void onLastItemVisible() {
                            Toast.makeText(MainActivity.this,
                                    "已到列表底部!", Toast.LENGTH_SHORT).show();
                        }
                    });

            ListView actualListView = mPullRefreshListView.getRefreshableView();

            mListItems = new LinkedList<String>();
            mListItems.addAll(Arrays.asList(mStrings));

            mAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, mListItems);


            //这里可以根据状态的变化来添加声音事件
            SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(
                    this);
            soundListener.addSoundEvent(PullToRefreshBase.State.PULL_TO_REFRESH, R.raw.pull_event);
            soundListener.addSoundEvent(PullToRefreshBase.State.RESET, R.raw.reset_sound);
            soundListener.addSoundEvent(PullToRefreshBase.State.REFRESHING,
                    R.raw.refreshing_sound);
            mPullRefreshListView.setOnPullEventListener(soundListener);


            actualListView.setAdapter(mAdapter);
        }

        private class GetDataTask extends AsyncTask<Void, Void, String[]> {

            @Override
            protected String[] doInBackground(Void... params) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                }
                return mStrings;
            }

            @Override
            protected void onPostExecute(String[] result) {
                //这里是提供给我们比较MODE的方法,返回0则表示相当
                if (CurrentMode.compareTo(PullToRefreshBase.Mode.PULL_FROM_START)==0) {
                    mListItems.addFirst("+下拉新增数据");
                }else {
                    mListItems.addLast("+上拉新增数据");
                }

                mAdapter.notifyDataSetChanged();

                //当数据加载完成,需要调用onRefreshComplete.
                mPullRefreshListView.onRefreshComplete();

                super.onPostExecute(result);
            }
        }
        /**
         * 此方法只在初次加载时调用
         */
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            menu.add(0, MENU_MANUAL_REFRESH, 0, "手动刷新");
            menu.add(
                    0,
                    MENU_DISABLE_SCROLL,
                    1,
                    mPullRefreshListView.isScrollingWhileRefreshingEnabled() ? "加载中禁止滑动"
                            : "加载中可滑动");
            menu.add(
                    0,
                    MENU_SET_MODE,
                    0,
                    mPullRefreshListView.getMode() == PullToRefreshBase.Mode.BOTH ? "模式:下拉刷新"
                            : "模式:上下拉刷新");
            return super.onCreateOptionsMenu(menu);
        }
        /**
         * 此方法每次点击都会调用
         */
        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            MenuItem disableItem = menu.findItem(MENU_DISABLE_SCROLL);
            disableItem
                    .setTitle(mPullRefreshListView
                            .isScrollingWhileRefreshingEnabled() ? "加载中禁止滑动"
                            : "加载中可滑动");

            MenuItem setModeItem = menu.findItem(MENU_SET_MODE);
            setModeItem
                    .setTitle(mPullRefreshListView.getMode() == PullToRefreshBase.Mode.BOTH ? "模式:下拉刷新"
                            : "模式:上下拉刷新");

            return super.onPrepareOptionsMenu(menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

            switch (item.getItemId()) {
                case MENU_MANUAL_REFRESH:
                    new GetDataTask().execute();
                    mPullRefreshListView.setRefreshing(false);
                    break;
                case MENU_DISABLE_SCROLL:
                    mPullRefreshListView
                            .setScrollingWhileRefreshingEnabled(!mPullRefreshListView
                                    .isScrollingWhileRefreshingEnabled());
                    break;
                case MENU_SET_MODE:
                    mPullRefreshListView
                            .setMode(mPullRefreshListView.getMode() == PullToRefreshBase.Mode.BOTH ? PullToRefreshBase.Mode.PULL_FROM_START
                                    : PullToRefreshBase.Mode.BOTH);
                    break;
            }

            return super.onOptionsItemSelected(item);
        }

        private String[] mStrings = {     "当前数据", "当前数据", "当前数据", "当前数据", "当前数据",
                "当前数据", "当前数据", "当前数据", "当前数据", "当前数据",
                "当前数据", "当前数据", "当前数据", "当前数据", "当前数据",
                "当前数据", "当前数据", "当前数据", "当前数据", "当前数据" };
    }

PullToRefreshListView的使用

标签:

原文地址:http://www.cnblogs.com/zhou2016/p/5551928.html

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