1

ビュースイッチャーを使用して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