2010-07-30 3 views
1

안녕하세요 저는 gallary 요소를 반복적으로 표시하고 싶습니다. 즉 앞으로 또는 뒤로 이동하면 갤러리 이미지의 끝은 필요 없습니다 .23 개의 elemets 배열을 사용하여 갤러리에 이미지를 다시 할당하면 반복해서 앞뒤로 움직이면됩니다.이 중 하나는 나에게 몇 가지 제안을 해주세요. 미리 감사드립니다.갤러리 요소를 반복하십시오

답변

6

이것은 거의 this question과 비슷합니다. getView() 메소드에서 조건을 작성하여 마지막 요소에 있는지 확인한 다음 getCount의 모듈러스를 사용하여 처음에 다시 시작해야합니다.

편집 이 다시 사용할 수있는 예가 될 수있다 : 코드 아래

public class TestGallery extends Activity { 
private Integer[] mImageIds = { R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4 }; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gallery); 

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

    g.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, long id) { 
      if (position >= mImageIds.length) { 
       position = position % mImageIds.length; 
      } 
      Toast.makeText(TestGallery.this, "" + position, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

public class ImageAdapter extends BaseAdapter { 
    int mGalleryItemBackground; 
    private Context mContext; 

    public ImageAdapter(Context c) { 
     mContext = c; 
     TypedArray a = obtainStyledAttributes(R.styleable.default_gallery); 
     mGalleryItemBackground = a.getResourceId(R.styleable.default_gallery_android_galleryItemBackground, 0); 

     a.recycle(); 
    } 

    public int getCount() { 
     return Integer.MAX_VALUE; 
    } 

    public Object getItem(int position) { 
     if (position >= mImageIds.length) { 
      position = position % mImageIds.length; 
     } 
     return position; 
    } 

    public long getItemId(int position) { 
     if (position >= mImageIds.length) { 
      position = position % mImageIds.length; 
     } 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView i = new ImageView(mContext); 
     if (position >= mImageIds.length) { 
      position = position % mImageIds.length; 
     } 
     i.setImageResource(mImageIds[position]); 
     i.setLayoutParams(new Gallery.LayoutParams(80, 80)); 
     i.setScaleType(ImageView.ScaleType.FIT_XY); 
     i.setBackgroundResource(mGalleryItemBackground); 
     return i; 
    } 

    public int checkPosition(int position) { 
     if (position >= mImageIds.length) { 
      position = position % mImageIds.length; 
     } 
     return position; 
    } 
} 

}

+0

미안 나의 나쁜, 난 당신에 복사하기 전에 뭔가를 잊어 버렸습니다. "checkPosition (position); 앞에"position = "을 추가하십시오." in getView – Sephy

+0

나는 위의 예제를 수정하여 모든 것을 작동시켰다. – Sephy

+0

getItem 메소드에서 실수 한 것처럼 보입니다. – Zammbi

관련 문제