我有一个标准的Android图库控件:
<Gallery
android:id="@+id/galArt"
android:spacing="10dp"
android:fadingEdgeLength="0dp"
android:unselectedAlpha="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
我用这段代码听取事件:
galArt.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
showMessageToast("Selected: " + pos);
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
这可行,因为我想这是为了:当我滑动图像时,我得到一个祝酒词,告诉我现在选择了哪个图像。
但是,在图像停止滑动之前,此动画显示,同时动画仍在运行。我希望在停止滑动后采取行动,以避免中断动画。
在动画完成后我可以收听什么才能收到通知?
答案 0 :(得分:0)
好的,我找到了解决方案。请注意,这只是 a 解决方案,而不一定是最佳解决方案。
我的解决方案是忽略所有逻辑事件(例如onScroll或onAnimationEnd,因为我无法让它们中的任何一个工作),并听取子视图位置的变化。当子视图静止时,动画结束。
这样做的一个实际好处是,这适用于拖动和投掷。
问题是onItemSelected函数将从除UI线程之外的另一个线程调用。通过使用您的activity的runOnUIThread函数解决这个问题,如示例所示。
监听变化的方式(请注意,这不是常规的 onItemSelected 函数,而是我自己的 onItemReallySelected ):
galArt.setOnItemReallySelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
_activity.runOnUiThread(new Runnable() {
public void run() {
//Do your stuff here ...
}
});
}
public void onNothingSelected(AdapterView<?> arg0) {
//... or here.
}
});
我对Android图库的实施:
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Gallery;
public class ArtGallery extends Gallery {
OnItemSelectedListener _listener;
Timer _timer = new Timer();
public ArtGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ArtGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ArtGallery(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP){
setTimer();
}
return super.onTouchEvent(event);
}
private int _lastScrollX = Integer.MIN_VALUE;
private void setTimer() {
//Cancel existing tasks (if any), and create a new timer.
_timer.cancel();
_timer = new Timer();
//Schedule our animation check.
_timer.schedule(new TimerTask() {
@Override
public void run() {
//Just some value that will change while the animation is running.
int x = getSelectedView().getLeft();
if(_lastScrollX != x){
//Value has changed; save current value, and reset the timer.
_lastScrollX = x;
setTimer();
}else{
//The value hasn't changed during the last 50ms. That probably means that the animation has stopped.
fireOnSelected();
}
}
}, 50);
}
public void setOnItemReallySelectedListener(OnItemSelectedListener listener){
_listener = listener;
}
//This function is copied from the Android Gallery source code, and works exactly like the original one.
private void fireOnSelected() {
if (_listener == null)
return;
int selection = this.getSelectedItemPosition();
if (selection >= 0) {
_listener.onItemSelected(this, getSelectedView(), selection, getAdapter().getItemId(selection));
} else {
_listener.onNothingSelected(this);
}
}
}