很久没用开源项目,重新复习一下下拉刷新 也是想总结下一个项目,应该需要那些东西, 为自己打下基础, 你也可以自己去下 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
|
、
|
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>