2011-07-18 39 views
0

我已經使用BaseAdapter爲我的應用程序設置了一個圖庫。這是我用於畫廊的代碼。setSelection上的圖庫導致錯誤的項目被選中

homeGallery = (Gallery) findViewById(R.id.homeimggallery); 
homeGallery.setSpacing(0); 
homeGallery.setSelection(0); 
homeGallery.setAdapter(new AddImgAdp(this)); 

private class AddImgAdp extends BaseAdapter { 
    private int GalItemBg, temp; 
    private Context cont; 
    private View convertView1; 
    private ViewGroup parent1; 
    private Bitmap mask = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.mask); 
    private Bitmap whiteBorder = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.stroke); 
    private Bitmap blueBorder = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.strokeactive); 
    private Bitmap src; 
    private Bitmap dest; 
    private Canvas canvas; 
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    private ImageView homeImgView; 
    private ImageView[] homeImgViewArr= new ImageView[arrThumbImage.length]; 


    public AddImgAdp(Context c) { 
     cont = c; 
     TypedArray typArray = obtainStyledAttributes(styleable.GalleryTheme); 
     GalItemBg = typArray.getResourceId(styleable.GalleryTheme_android_galleryItemBackground, 0); 
     typArray.recycle(); 
     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 
    } 
     public int getCount() { 
      return arrThumbImage.length; 
     } 
     public Object getItem(int position) { 
      return position; 
     } 
     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 

      homeImgView = new ImageView(cont); 

      try{  

       src = BitmapFactory.decodeResource(mContext.getResources(), arrThumbImage[position]); 

       dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888); 
       canvas = new Canvas(dest); 
       canvas.drawColor(Color.TRANSPARENT); 
       canvas.save(); 
       canvas.translate((canvas.getWidth() - src.getWidth())>> 1, (canvas.getHeight() - src.getHeight())>> 1); 
       canvas.drawBitmap(src, 0, 0, null); 
       canvas.drawBitmap(mask, 0, 0, paint); 
       canvas.drawBitmap(dest, 0, 0, paint); 

       homeImgView.setBackgroundDrawable(new BitmapDrawable(dest));    
       homeImgView.setImageResource(R.drawable.stroke); 

       homeImgView.setScaleType(ImageView.ScaleType.FIT_XY); 
       homeImgViewArr[position] = homeImgView; 

      } catch(Exception e) {} 


      return homeImgView; 
     } 
    } 

畫廊看起來象下面這樣:

enter image description here

在手指運動,這是靠右行駛預期到左或從左到右。現在我想添加一些onClick行動的項目。如果用戶點擊任何圖像,它將被選中並對齊。以下代碼用於此操作。

homeGallery.setOnItemClickListener(new OnItemClickListener() { 
        public void onItemClick(AdapterView parent, View v, int position, long id) { 
homeGallery.setSelection(position); 

        } 
       }); 

但它的結果是錯誤的。如果我選擇了產品編號2,產品編號3選擇得到了,雖然爲setSelection動作射擊對項目沒有2.如果我點擊上面的PIC的最右邊的項目,這是導致如下行:

enter image description here

什麼是我的代碼的問題?

+0

你知道嗎,指數從0開始,所以如果你正在設置第2然後在庫它會告訴你3項作爲選擇 – ingsaurabh

+0

雅,我知道。所有的位置從0開始。 –

回答

0

我正在與一個畫廊自己,但我不太清楚問題在這裏,因爲你的代碼比我的立場更遠。

反正,既然你點擊項目3?並且您可以將項目4居中,我可以想到幾個選項:

- 數組索引有什麼問題,可能需要記錄位置。

- 個人而言,我使用Integer數組和getView()方法工作,我只是獲得了比我相信更容易的位置。

public ArrayList mImageIds = new ArrayList();數組... i.setImageResource(mImageIds.get(position));並在getView()中進行排序。

希望這是有益

+0

我做了同樣的事情,但圖像需要一些修改,所以我使用掩膜進行修改。 –

相關問題