app中“发现”模块中有一个功能是“附近好友”,采用的是ListView加载好友头像与昵称,使用了第三方插件android-pullToRefresh,但是当我将所有设置好后,发现根本加载不出来,原因是布局文件中的layout_height不能用wrap_content,必须用match_parent。我也不知道原因(有网友说是pullTorefresh的源码规定的)
一、pullTorefresh使用方法为:
(1)将github上下载的插件解压缩后,里面有一个library文件夹,在eclipse中新建一个安卓项目,名为pullToRefresh,包名为(library文件夹里面的清单配置文件AndroidManifest.xml中的package属性),项目建成以后,将library文件中的src、res以及AndroidManifest.xml一起覆盖你刚才创建的项目的同名文件夹及文件。接下来刷新项目,可能会报错,你要把MainActivity.java类和他的布局文件activity_main.xml删除,因为library不需要这两个,所以删掉。再刷新一下项目就好了。
(2)在项目名上鼠标右键选properties,打开后选择左边“android”,在右边最下面,勾选上isLibrary,点击确定后,他就变成了一个库,你可以引用了
(3)假如你的另一个项目叫myapp,要引用刚才那个库,那么现在就在你的myapp项目名上鼠标右键选择properties,打开后选择左边“android”,在右边下面选择“add”按钮,添加刚才那个pullToRefresh库,确定后,这个库被添加成功后左边会有一个绿颜色的对勾,如下图所示
(4)布局文件中的ListView标签可以换成如下:
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/lv_nearby_user"
android:layout_width="match_parent"
android:layout_height="match_parent" />
(5)activity中的ListView也要更改成如下:
public class NearbyUserActivity extends Activity{
PullToRefreshListView listView; //控件声明时,直接声明为PullToRefreshListView
ShowUserReceiver receiver;
NearbyUserAdapter adapter;
ArrayList<UserEntity> list;
BitmapUtils bitmapUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.nearby_user);
setView();
addListener();
bitmapUtils=new BitmapUtils(this);
receiver=new ShowUserReceiver();//这行代码忘记的话,就会一直收不到广播
this.registerReceiver(receiver, new IntentFilter(Const.ACTION_GET_NEARYBY_USER));
Tools.showProgressDialog(this,"正在查询");
NearbyUserBiz.query(TApplication.currentUser);
} catch (Exception e) {
ExceptionUtil.handleException(e);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
this.unregisterReceiver(receiver);
}
private void addListener() {
listView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
new Thread() {
public void run() {
try {
this.sleep(1000);
//这里模拟从网上取到了新数据
UserEntity entity = new UserEntity();
entity.setName("联网取到的新用户");
list.add(0, entity);
runOnUiThread(new Runnable() {
public void run() {
try {
NearbyUserAdapter.update(list);
listView.onRefreshComplete();
} catch (Exception e) {
ExceptionUtil.handleException(e);
}
}
});
} catch (Exception e) {
ExceptionUtil.handleException(e);
}
};
}.start();
}
});
}
private void setView() {
listView=(PullToRefreshListView) findViewById(R.id.lv_nearby_user);//控件初始化时,强转为PullToRefreshListView
}
class ShowUserReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
try {
Tools.closeProgressDialog();
list=(ArrayList<UserEntity>) intent.getSerializableExtra(Const.KEY_DATA);
if(list==null){
Tools.showInfo(context, "没有取到数据");
}else {
if(adapter==null){
adapter=new NearbyUserAdapter(context, list, bitmapUtils);
listView.setAdapter(adapter);
//下面这行代码的作用是:快速滑动时,不会加载图片,等滑动停止后再加载
listView.setOnScrollListener(new PauseOnScrollListener(
bitmapUtils, false, true));
}else {
adapter.notifyDataSetChanged();
}
}
} catch (Exception e) {
ExceptionUtil.handleException(e);
}
}
}
}
二、你在github上下载的使用后,在你体验PullToRefreshListView的效果时,即在列表页手指按着屏幕往下拉,不要松开,你发现有效果,但是显示的是英文提示“Pull to refresh…”和“Release to refresh…”,以及“loading...”,你想改成中文的,方法如下:
(1)在你刚才那个库的res文件夹中的values文件夹下有一个pull_refresh_strings.xml,打开后就可以修改了