我ListView展示了一些物品.我想对当前显示在可见部分的项目执行一些操作ListView,具体取决于ListView滚动的方式; 因此我想要实现OnScrollListener的ListView.根据Android api引用,onScroll方法"将在滚动完成后调用".这似乎是我所需要的,因为一旦滚动完成,我就会对其执行操作ListView(onScroll方法返回显示的第一个项目的索引和显示的项目数).
但是一旦实现,我就会看到LogCatonScroll方法不仅在滚动完成后被触发,而且对于从滚动的开始到结束进入显示视图的每个新项目都被触发.这不是我期望的行为,也不是我需要的行为.相反,侦听器的另一个方法(onScrollStateChanged)不提供有关当前显示的项目的信息ListView.
那么,有没有人知道如何使用这两种方法来检测滚动的结尾并获取有关显示项目的信息?api引用与方法的实际行为之间的不一致使我感到困惑.提前致谢.
PS:我已经看到了一些类似的话题,但没有什么能帮助我理解整个事情的运作方式......!
我创建了一个扩展Thread的类,通过LocationManager在非ui线程中检索用户位置.我将它作为一个线程实现,因为它必须在请求时启动并在有限的时间内完成它的工作.顺便说一句,我必须在线程中添加一个Looper对象,以便能够为LocationManager(onLocationChanged)创建处理程序.
这是代码:
public class UserLocationThread extends Thread implements LocationListener {
//...
public void run() {
try {
Looper.prepare();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Looper.loop();
Looper.myLooper().quit();
} catch (Exception e) {
//...
}
}
@Override
public void onLocationChanged(Location location) {
locationManager.removeUpdates(this);
//...
handler.sendMessage(msg); //this is the handler for communication with father thread
}
//...}
Run Code Online (Sandbox Code Playgroud)
我希望线程启动,接收用户位置数据(在这种情况下只是一次),通过消息将数据发送到主线程给处理程序,然后死掉.问题是,在我的情况下,一旦run方法结束,线程就不会再死了(应该没问题,因为否则onLocationChanged将不会收到新的位置).
但是通过这种方式,假设线程的停止和挂起方法已被弃用,那么至少在这种情况下,使用looper die创建一个线程会是一个好方法吗?
提前致谢 ;)
我一直在寻找关于扩展ItemizedOverlay和OverlayItem的正确方法的清晰信息,但我得到的结果还不能满足我.简而言之,我需要在地图上显示与多个位置相关的不同类型的标记; 要显示的标记的类型由位置本身的特定属性确定.我还需要在选择标记时显示某个标记,并在标记未选中或未聚焦时显示另一个标记.根据我的理解,这是我写的:
public class MyItemizedOverlay extends ItemizedOverlay<MyOverlayItem> {
ArrayList<MyOverlayItem> items;
//...
public MyItemizedOverlay(Drawable drawable, /*...*/) {
super(boundCenterBottom(drawable));
//...
populate();
}
public void addOverlay(MyOverlayItem overlay) {
return this.items.add(overlay);
populate();
@Override
protected MyOverlayItem createItem(int index) {
return this.items.get(index);
}
@Override
public int size() {
return this.items.size();
}}
public class MyOverlayItem extends OverlayItem {
//...
public Drawable getMarker(int stateBitset) {
Drawable drawable;
try {
if (stateBitset == 0) {
if (this.property.Equals("ON")) {
drawable = this.context.getResources().getDrawable(R.drawable.on);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
return drawable;
} else …Run Code Online (Sandbox Code Playgroud)