Gallery居左 点击禁止滑动

两种方法:

1、重写Gallery,需要设置Gallery的宽度为固定值,fill_parent也不行;

2、设置Gallery属性android:ScrollX的值,缺点setOnClickItemListener时,由于水平偏移,导致item点击事件位置不准确

目前,对于Gallery的两端对齐暂时无法实现

重写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将导致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

你可能感兴趣的:(gallery)