Gallery居左 點(diǎn)擊禁止滑動(dòng)
兩種方法:
1、重寫Gallery,需要設(shè)置Gallery的寬度為固定值,fill_parent也不行;
2、設(shè)置Gallery屬性android:ScrollX的值,缺點(diǎn)setOnClickItemListener時(shí),由于水平偏移,導(dǎo)致item點(diǎn)擊事件位置不準(zhǔn)確
目前,對(duì)于Gallery的兩端對(duì)齊暫時(shí)無法實(shí)現(xiàn)
重寫Gallery方法如下:
public class BookcaseGallery extends Gallery { private Camera mCamera; private int mWidth; private int mPaddingLeft; private boolean flag; private static int firstChildWidth; private static int firstChildPaddingLeft; private int offsetX; private IOnItemClickListener mListener; public BookcaseGallery(Context context) { super(context); mCamera = new Camera(); this.setStaticTransformationsEnabled(true); } public BookcaseGallery(Context context, AttributeSet attrs) { super(context, attrs); mCamera = new Camera(); setAttributesValue(context, attrs); this.setStaticTransformationsEnabled(true); } public BookcaseGallery(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mCamera = new Camera(); setAttributesValue(context, attrs); this.setStaticTransformationsEnabled(true); } private void setAttributesValue(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, new int[] { attr.paddingLeft }); mPaddingLeft = typedArray.getDimensionPixelSize(0, 0); typedArray.recycle(); } protected boolean getChildStaticTransformation(View child, Transformation t) { t.clear(); t.setTransformationType(Transformation.TYPE_MATRIX); mCamera.save(); final Matrix imageMatrix = t.getMatrix(); if (flag) { firstChildWidth = getChildAt(0).getWidth(); firstChildPaddingLeft = getChildAt(0).getPaddingLeft(); } offsetX = firstChildWidth / 2 + firstChildPaddingLeft + mPaddingLeft - mWidth / 2 + 10; mCamera.translate(offsetX, 0f, 0f); mCamera.getMatrix(imageMatrix); mCamera.restore(); return true; } @Override public boolean onTouchEvent(MotionEvent event) { event.offsetLocation(-offsetX, 0); return super.onTouchEvent(event); } protected void onSizeChanged(int w, int h, int oldw, int oldh) { if (!flag) { mWidth = w; getLayoutParams().width = mWidth; flag = true; } super.onSizeChanged(w, h, oldw, oldh); } @Override public boolean onSingleTapUp(MotionEvent e) { try {//若return false將導(dǎo)致onClickItemListener無法使用,獲得位置后就可以接著用 Field f = BookcaseGallery.class.getSuperclass().getDeclaredField( "mDownTouchPosition"); f.setAccessible(true); int position = f.getInt(this); Log.i("aaa", "mDownTouchPosition = " + position); if (null != mListener && position >= 0) { mListener.onItemClick(position); } } catch (SecurityException e1) { e1.printStackTrace(); } catch (NoSuchFieldException e1) { e1.printStackTrace(); } catch (IllegalArgumentException e2) { e2.printStackTrace(); } catch (IllegalAccessException e3) { e3.printStackTrace(); } return false; } public void setOnItemClickListener(IOnItemClickListener listener) { mListener = listener; } public interface IOnItemClickListener { public void onItemClick(int position); } }
工程下載:AlignLeftGallery.rar

浙公網(wǎng)安備 33010602011771號(hào)