2012-09-14 5 views
0

ViewSwitcher을 이미지 마법사처럼 사용하려고합니다.다음 및 이전 단추가있는 ViewSwitcher

갤러리가 아니라 ViewSwitcher에서 이미지를 변경하려면 다음 및 이전 버튼이 있습니다. 나는 안드로이드 샘플 응용 프로그램의 API Demo에서 참조를 가져 왔습니다.

는 점에서 그들은 ViewSwitcherGallery을 사용했다하지만 난 NextPrev 버튼 대신 사용합니다. 그러나 나는 그것을 어떻게하는지 모른다. ImageAdapter 자체가 ViewSwitcher에 존재하는 이미지 뷰에 새로운 이미지를 추가 계속 어디 샘플 응용 프로그램

으로는 그들은

Gallery g = (Gallery) findViewById(R.id.gallery); 
g.setAdapter(new ImageAdapter(this)); 
g.setOnItemSelectedListener(this); 

을 사용했다. 그렇다면 다음 버튼과 이전 버튼에서 어떻게 동일한 작업을 수행 할 수 있습니까? 당신이 ImageSwitcher를 사용하는 경우

Sample App Screen

답변

1

이 할 수있는 매우 간단한 일이다.

private int[] mImageIds= //.. the ids of the images to use 
private int mCurrentPosition = 0; // an int to monitor the current image's position 
private Button mPrevious, mNext; // our two buttons 

buttons는이 두 가지 onClick 콜백 : 사용하지 않도록 기억해야합니다

public void goPrevious(View v) { 
    mCurrentPosition -= 1; 
    mViewSwitcher.setImageResource(mImageIds[mCurrentPosition]); 
    // this is required to kep the Buttons in a valid state 
    // so you don't pass the image array ids boundaries 
    if ((mCurrentPosition - 1) < 0) { 
     mPrevious.setEnabled(false); 
    } 
    if (mCurrentPosition + 1 < mImageIds.length) { 
     mNext.setEnabled(true); 
    } 
} 

public void goNext(View v) { 
    mCurrentPosition += 1; 
    mViewSwitcher.setImageResource(mImageIds[mCurrentPosition]); 
    // this is required to kep the Buttons in a valid state 
    // so you don't pass the image array ids boundaries 
    if ((mCurrentPosition + 1) >= mImageIds.length) { 
     mNext.setEnabled(false); 
    } 
    if (mCurrentPosition - 1 >= 0) { 
     mPrevious.setEnabled(true); 
    } 
} 

당신은 당신이 Buttons으로 Gallery을 교체하고 ImageSwitcher로 연결해야합니다 onCreate 메서드의 이전 Button (배열의 첫 번째 이미지부터 시작).

관련 문제