如何在viewswitcher的视图发生变化时启动操作?

时间:2012-10-22 09:07:36

标签: android viewswitcher

我正在使用View Switcher来显示2个不同的视图。我只想确定切换器动态显示哪个视图,并希望相应地启动一些操作。这是代码:

private ViewSwitcher switcher;
private GestureDetector gesturedetector = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    switcher = (ViewSwitcher)findViewById(R.id.viewSwitcher);

    gesturedetector = new GestureDetector(this, this);

    if(switcher.getDisplayedChild() == 1)
    {
        DisplayDots(0);
    }
    else
    {
        DisplayDots(1);
    }
}


private void DisplayDots(int paramInt)
{
    Bitmap localBitmap1;
    Bitmap localBitmap2;
    try
    {
        Resources localResources = this.getResources();
        localBitmap1 = BitmapFactory.decodeResource(localResources, R.drawable.whitedot);
        localBitmap2 = BitmapFactory.decodeResource(localResources, R.drawable.graydot);
        if (paramInt == 0)
        {
            ((ImageView)this.findViewById(R.id.home_dotimage1)).setImageBitmap(localBitmap1);
            ((ImageView)this.findViewById(R.id.home_dotimage2)).setImageBitmap(localBitmap2);
            //              ((ImageView)this.findViewById(R.id.home_dotimage3)).setImageBitmap(localBitmap2);
            //              ((ImageView)this.findViewById(R.id.home_dotimage4)).setImageBitmap(localBitmap2);
            //              ((ImageView)this.findViewById(R.id.home_dotimage5)).setImageBitmap(localBitmap2);
            return;
        }
        if (paramInt == 1)
        {
            ((ImageView)this.findViewById(R.id.home_dotimage1)).setImageBitmap(localBitmap2);
            ((ImageView)this.findViewById(R.id.home_dotimage2)).setImageBitmap(localBitmap1);
            //              ((ImageView)this.findViewById(R.id.home_dotimage3)).setImageBitmap(localBitmap2);
            //              ((ImageView)this.findViewById(R.id.home_dotimage4)).setImageBitmap(localBitmap2);
            //              ((ImageView)this.findViewById(R.id.home_dotimage5)).setImageBitmap(localBitmap2);
            return;
        }
    }
    catch (Exception localException)
    {
        localException.printStackTrace();
    }
    catch (Error localError)
    {
        localError.printStackTrace();
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return gesturedetector.onTouchEvent(event);
}
int SWIPE_MIN_VELOCITY = 100;
int SWIPE_MIN_DISTANCE = 100;

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {

    float ev1X = e1.getX();
    float ev2X = e2.getX();

    final float xdistance = Math.abs(ev1X - ev2X);

    final float xvelocity = Math.abs(velocityX);

    if( (xvelocity > SWIPE_MIN_VELOCITY) && (xdistance > SWIPE_MIN_DISTANCE) )
    {
        if(ev1X > ev2X)
        {
            switcher.showNext(); //Error in this part
        }
        else
        {
            switcher.showPrevious(); //Error in this part
        }
    }

    return false;
}

请帮帮我。谢谢。

0 个答案:

没有答案