教你怎么使用下拉刷新(第三方)

很久没用开源项目,重新复习一下下拉刷新 也是想总结下一个项目,应该需要那些东西, 为自己打下基础, 你也可以自己去下 library,关于源码我会在后面放在

效果图

\

pulltorefresh(下拉刷新) 有一个类是 PullToRefreshListView 因为我只讲下拉刷新,所以只取了一部分的类。 首先复制 将一个包

粘贴进 你的工程

\

 

需要注意 这个类还需要一些资源文件:

layout : header.xml 头部出现的布局 pull_to_refresh_header.xml 刷新的时候出现的布局 如果你们老板不喜欢可以自己定制

drawable-hdpi:目录下 有两张图片 pulltorefresh_down_arrow.png pulltorefresh_up_arrow.png 看名字知道 一个是下拉时图片。一个下拉到一个距离 出现的

还需要一个res/values/attrs.xml 自定义标签

 

还有res/values/Strings.xml

加入 这里你可以编辑 下拉时候出现的文字

 

?
1
2
3
4
<string name= "pull_to_refresh_pull_label" >Pull to refresh...</string>
<string name= "pull_to_refresh_release_label" >Release to refresh...</string>
<string name= "pull_to_refresh_refreshing_label" >Loading...</string>
<string name= "pull_to_refresh_tap_label" >Tap to refresh...</string>

 

其实对于这个开源项目,你可以理解为一个线性布局,因为它内置了一个Listview ,就和ListFagment一样。

 

相信大家都已经把项目建好了,我在上面已经说过了,可以当作一个线性布局,so 你 就拿着当线性布局用就行了 先写布局

 

 

?
1
<pre class="<a href=" http:="" www.2cto.com="" kf="" qianduan="" css="" "="" target="_blank" style="font-family: 'Courier New', monospace; margin-top: 0px; margin-bottom: 1em; width: 630px; overflow: auto; padding: 10px 0px; color: rgb(51, 51, 51); line-height: 28px; background-color: rgb(255, 255, 255);">html" name="code">

 

?
1
 
?
1
 
pulltorefresh.PullToRefreshListView 是 PullToRefreshListView类的全名 为了避免出错 ,直接复制包名 打开 PullToRefreshListView 子节点 直接右击一下 选择 copy qualifiled Name <span style="font-size: 18px;">开始写java代码吧 </span> 既然在xml 里定义了,肯定要在java代码中实例化, 
private PullToRefreshListView pullToRefreshListView; pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.ptrlv);

然后通过 getRefreshableView(); 得到一个 listview然后用adapter填充数据
接着 下拉刷新有个方法 setOnRefreshListener 下拉监听 通常情况下:请求网络新数据、解析、展示
?
1
 
?
1
 

 

全部代码

<span style="font-size:18px;">package com.example.refreshdemo;

import com.handmark.pulltorefresh.library.MyListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.R.string;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Interpolator.Result;
import android.text.format.DateUtils;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

	private PullToRefreshListView refresh;

	public MyListView lv;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		refresh = (PullToRefreshListView) findViewById(R.id.RefreshListView);
		 lv = refresh.getRefreshableView();
		
		 ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, initData());
	        lv.setAdapter(adapter);
		 lv.setAdapter(adapter);
		  refresh.setOnRefreshListener(new OnRefreshListener<MyListView>() {
	
			public void onRefresh(PullToRefreshBase<MyListView> refreshView) {
				String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),  
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);  
				     refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);  
				     new MyAsyncT().execute();
			}

		});
		refresh.setMode(Mode.BOTH);
		  
	}

	
	private String[] initData() {
		 String [] datas=new String[10];
		   for (int i = 0; i < datas.length; i++) {
	            datas[i]="item"+i;
	        }
	        return datas;
	}


	@Override
	public void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
	}


	private void setSelection(int i) {
	}

	public class  MyAsyncT extends AsyncTask<Void, String, Result>{


		@Override
		protected Result doInBackground(Void... params) {
			try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
		}
		
		@Override
		protected void onPostExecute(Result result) {
			refresh.onRefreshComplete();
			super.onPostExecute(result);
		}
		
	}

}</span>


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值