1

我正在使用 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;
}

请帮助我。在此先感谢。

4

0 回答 0