1

我有一个标准的 Android Gallery 控件:

<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) {}
    });

我猜它的目的是这样的:当我滑动图像时,我会得到一个祝酒词,告诉我现在选择了哪个图像。

但是,这个 toast 出现图像停止滑动之前,而动画仍在运行。我想在它停止滑动后采取行动,以避免中断动画。

动画完成后,为了获得通知,我可以听什么?

4

1 回答 1

0

好吧,我找到了解决方案。请注意,这只是一个解决方案,不一定是最佳解决方案。

我的解决方案是忽略所有逻辑事件(如 onScroll 或 onAnimationEnd,因为我无法让它们中的任何一个工作),并监听子视图位置的变化。当子视图静止时,动画就结束了。

这样做的一个实际好处是它适用于拖动和投掷。

一个问题是 onItemSelected 函数将从您的 UI 线程之外的另一个线程调用。通过使用活动的 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 Gallery 的实现:

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);
        }

    }
}
于 2012-08-16T20:54:55.697 回答