Gallery居左 點擊禁止滑動

來源:互聯網
上載者:User

兩種方法:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.