안녕하세요

당췌 해결책이 나오지 않아 문의 드립니다.

참고로 공지사항은 다 읽었고 검색도 마쳤습니다.  

 

Gallery 를 이용해서 무한스크롤이 되는 Menu 를 구현하고 있습니다.

선택된 Menu 는 Center 로 이동하고 간격이 벌어집니다.

 

setOnItemSelectedListener 를 등록 하고 onItemSelected 함수에서 position 을 받으면

선택한 menu 의 position 이 들어오지 않고 다른 position 이 들어 옵니다.

간간히 정상적인 position 도 들어오는데 랜덤이라 더 문제입니다.

간격을 벌어지게 만들지 않으면 position 이 정상적으로 들어오는데 간격이 벌어지면서 

선택된 position 이 정상적으로 전달되지 않네요

이유를 알 수가 없습니다. ㅜㅡ

 

소스 첨부 합니다.   

 

 public class CustomGalleryActivity extends Activity implements OnItemSelectedListener{
 public static int[] mImageID = { R.drawable.color_1, R.drawable.color_2, R.drawable.color_3, R.drawable.color_4, R.drawable.color_5,  R.drawable.color_6, R.drawable.color_7};
 private int maxCnt = 7;
 private Gallery mCustomGallery;
 private CustomGalleryImageAdapter adapter;
 private int selectedPos = -1;
 private boolean isItemClick = false;
 
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mCustomGallery = (Gallery) findViewById(R.id.gallery_main_menu);
        adapter = new CustomGalleryImageAdapter(this, mImageID);
        mCustomGallery.setOnItemSelectedListener(this);
        mCustomGallery.setAdapter(adapter);
        
     // 선택될 아이템 
     int selectedItemPosition = 0;     
     // 한 화면 최대 표출 개수를 초과 하였을 때   
     if(this.mImageID.length > maxCnt) {
      selectedItemPosition = this.mImageID.length + (maxCnt / 2);      
     } else {
      if(mImageID.length > 0) selectedItemPosition = Math.round(mImageID.length / 2);
     }
     mCustomGallery.setSelection(selectedItemPosition); 
    }
 public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
  
  if(this.mImageID.length >= maxCnt) {
   adapter.setSelectedPos(position % this.mImageID.length);
   if (position < this.mImageID.length) {
    mCustomGallery.setSelection(position + this.mImageID.length);
   } else if (position >= this.mImageID.length * 2) {
    mCustomGallery.setSelection(position - this.mImageID.length);
   } else {
    adapter.notifyDataSetChanged();
   }
  } else {
   adapter.setSelectedPos(position);
   adapter.notifyDataSetChanged();
  }
 }
 public void onNothingSelected(AdapterView<?> parent) {
  // TODO Auto-generated method stub
  
 }
}
 


   

 

 public class CustomGalleryImageAdapter extends BaseAdapter {
 private Context mContext;
 private ImageView image;
 private LayoutInflater mInflater;
 private int selectedPos = 0;
 private int[] mImageID = null;
 public CustomGalleryImageAdapter(Context c, int[] mImageID) {
  mContext = c;
  mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  this.mImageID = mImageID;
 }
 
 public int getCount() {
  return mImageID.length * 3;
 }
 
 
 public Object getItem(int position) {
  return mImageID[position % mImageID.length];
 }
 
 public long getItemId(int position) {
  return position;
 }
 public View getView(int position, View convertView, ViewGroup parent) {
  position = position % mImageID.length;
  View mview = convertView;
  if (mview == null) {
   mview = mInflater.inflate(R.layout.gallery, null);
  }
  FrameLayout llayout = (FrameLayout) mview.findViewById(R.id.ll_main_menu_list_row);
  image = (ImageView) mview.findViewById(R.id.image);
  image.setBackgroundResource(mImageID[position]);
  image.setLayoutParams(new FrameLayout.LayoutParams(120, 110));
  if (selectedPos == position) { 
   llayout.setPadding(70, 0, 70, 0);
  } else {
   llayout.setPadding(0, 0, 0, 0);
  }
  return mview;
 }
 
 public void setSelectedPos(int position) {
  this.selectedPos = position;
 }
}