pulltorefresh_library刷新样式

这篇文章我们介绍下pulltorefresh_library刷新样式,先看下效果图:

介绍下效果图:大家可以看到效果图中不同的刷新样式

看下代码:PullToRefreshBase.java:

在这个类中有 ROTATE ,FLIP 两种刷新样式 ,主要修改样式的是getDefault(){}方法:

	public static enum AnimationStyle {
		/**
		 * This is the default for Android-PullToRefresh. Allows you to use any
		 * drawable, which is automatically rotated and used as a Progress Bar.
		 */
		ROTATE,

		/**
		 * This is the old default, and what is commonly used on iOS. Uses an
		 * arrow image which flips depending on where the user has scrolled.
		 */
		FLIP;

		static AnimationStyle getDefault() {
			return ROTATE;
		}

		/**
		 * Maps an int to a specific mode. This is needed when saving state, or
		 * inflating the view from XML where the mode is given through a attr
		 * int.
		 * 
		 * @param modeInt - int to map a Mode to
		 * @return Mode that modeInt maps to, or ROTATE by default.
		 */
		static AnimationStyle mapIntToValue(int modeInt) {
			switch (modeInt) {
				case 0x0:
				default:
					return ROTATE;
				case 0x1:
					return FLIP;
			}
		}

		LoadingLayout createLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {
			switch (this) {
				case ROTATE:
				default:
					return new RotateLoadingLayout(context, mode, scrollDirection, attrs);
				case FLIP:
					return new FlipLoadingLayout(context, mode, scrollDirection, attrs);
			}
		}
	}

修改样式的方法:getDefault(){} : 返回的样式是:reture ROTATE ; return FLIP;

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private String[] mStrings = { "Abbaye de Belloc",
            "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu",
            "Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler",
            "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
            "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
            "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler" };
    private PullToRefreshListView lv_list;
    private List<String> str = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_list = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        //设置pull-to-refresh模式为Mode.Both 上拉和下拉都可以监听到
        lv_list.setMode(PullToRefreshBase.Mode.BOTH);
        //给listview设置刷新的监听;
        lv_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                //上拉和下拉都可以监听到.那么问题来了。如何区分是上啦还是下拉;
                //获取当前时间
                String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

//			 更新上一次的时间标签
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                if (refreshView.ismHeaderShow()) {//下拉刷新
                    Log.e("onRefresh", "下拉刷新");
                    new GetDataTask(true).execute();
                }else {//上啦;
                    Log.i("onRefresh", "上拉加载");
                    new GetDataTask(false).execute();
                }
            }
        });

        // 初始化数据
        initData();
        // 设置适配器
        adapter = new Mydapter();
        lv_list.setAdapter(adapter);
    }

    // 初始化数据
    private void initData() {
        for (int i = 0; i < mStrings.length; i++) {
            str.add(mStrings[i]);
        }
    }

    // 异步加载的框架
    private class GetDataTask extends AsyncTask<Void, Void, String> {
        //添加有参构造 ,传入boolean
        boolean isheader;
        public GetDataTask(boolean isheader){
            this.isheader = isheader;

        }

        @Override
        // 运行在子线程;在onPreExecute方法执行之后。用于做一些耗时的工作。联网。拷贝数据;
        protected String doInBackground(Void... params) {
            SystemClock.sleep(2000);
            // 获取当前时间
            String label = DateUtils.formatDateTime(getApplicationContext(),
                    System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
                            | DateUtils.FORMAT_SHOW_DATE
                            | DateUtils.FORMAT_ABBREV_ALL);

            return label;
        }

        @Override
        // 运行在主线程.用于接收子线程返回的结果。
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (isheader) {
                //拿到联网获取的结果。添加到集合中头部。
                str.add(0, result);
            }else {
                //拿到联网获取的结果。添加到集合中的尾部。
                str.add(result);
            }
            adapter.notifyDataSetChanged();
            //拿到结果后关闭下拉刷新效果
            lv_list.onRefreshComplete();

        }
    }

    private Mydapter adapter;

    public class Mydapter extends BaseAdapter {

        @Override
        public int getCount() {
            return str.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = null;
            Holder holder = null;
            if (convertView == null) {
                view = View.inflate(getApplicationContext(), R.layout.item_list, null);
                holder = new Holder();
                holder.tv_info = (TextView) view.findViewById(R.id.tv_text);
                view.setTag(holder);
            } else {
                view = convertView;
                holder = (Holder) view.getTag();
            }
            holder.tv_info.setText(str.get(position));

            return view;
        }

        public class Holder {
            TextView tv_info;
        }
    }
}
设置pull-to-refresh模式为Mode.Both   上拉和下拉都可以监听到

刷新模式:

lv_list.setMode(PullToRefreshBase.Mode.BOTH);

Mode.DISABLED 取消上拉 下拉

Mode.PULL_FROM_END 上拉加载

Mode.PULL_FROM_STAET 下拉刷新

源码:http://download.csdn.net/detail/lijinweii/9893041

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值