Android——自訂滾動ViewGroup

來源:互聯網
上載者:User

標籤:

該自訂控制項對外提供getAdapter和setAdapter介面,能夠設定要滾動顯示的View(每個View預設顯示全螢幕寬高)

public class ScrollerViewGroup extends ViewGroup {    private int screenHeight;//the phone screen height    private int currentY;//記錄當前當前的Y位置    private int startScrollY;//記錄手指按下觸摸事件時的Y方向的滾動位置    private int endScrollY;//記錄手指抬起觸摸事件時的Y方向的滾動位置    private Adapter mAdapter;//顯示適配器    private Scroller mScroller;    public ScrollerViewGroup(Context context, AttributeSet attrs) {        this(context, null, attrs);    }    public ScrollerViewGroup(Context context) {        this(context, null, null);    }    public ScrollerViewGroup(Context context, Adapter adapter) {        this(context, adapter, null);    }    public ScrollerViewGroup(Context context, Adapter adapter, AttributeSet attrs) {        this(context, adapter, attrs, 0);    }    public ScrollerViewGroup(Context context, Adapter adapter, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        this.mAdapter = adapter;        init(context);    }    //初始化mScroller和screenHeight    private void init(Context context) {        mScroller = new Scroller(context);        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();        screenHeight = metrics.heightPixels;    }    public Adapter getAdapter() {        return mAdapter;    }    public void setAdapter(Adapter mAdapter) {        this.mAdapter = mAdapter;    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        if (mAdapter == null) return;        int count = mAdapter.getCount();        for (int i = 0; i < count; i++) {            View child = mAdapter.getView(i, null, this);            addView(child);            measureChild(child, widthMeasureSpec, heightMeasureSpec);        }    }    @Override    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {        return new MarginLayoutParams(p);    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        int count = getChildCount();        MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();        mlp.height = screenHeight * count;        setLayoutParams(mlp);        for (int i = 0, j = 0; i < count; i++) {            View child = getChildAt(i);            if (child.getVisibility() == VISIBLE) {                child.layout(l, j * screenHeight, r, (++j) * screenHeight);            }        }    }    @Override    public boolean onTouchEvent(MotionEvent event) {        int y = (int) event.getY();        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                currentY = y;                startScrollY = getScrollY();                break;            case MotionEvent.ACTION_MOVE:                if (!mScroller.isFinished())                    mScroller.abortAnimation();                int offset = currentY - y;                if (getScrollY() < 0)                    offset = 0;                if (getScrollY() > getHeight() - screenHeight)                    offset = 0;                scrollBy(0, offset);                currentY = y;                break;            case MotionEvent.ACTION_UP:                endScrollY = getScrollY();                int offsetY = endScrollY - startScrollY;                if (offsetY > 0) {                    if (offsetY > screenHeight / 3) {                        mScroller.startScroll(0, endScrollY, 0, screenHeight - offsetY);                    } else {                        mScroller.startScroll(0, endScrollY, 0, -offsetY);                    }                } else {                    if (-offsetY > screenHeight / 3) {                        mScroller.startScroll(0, endScrollY, 0, -screenHeight - offsetY);                    } else {                        mScroller.startScroll(0, endScrollY, 0, -offsetY);                    }                }                break;        }        postInvalidate();        return true;    }    @Override    public void computeScroll() {        super.computeScroll();        if (mScroller.computeScrollOffset()) {            scrollTo(0, mScroller.getCurrY());            postInvalidate();        }    }}

使用時:

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);        Adapter adapter = new ArrayAdapter<Integer>(this, 0, color) {            @Override            public View getView(int position, View convertView, ViewGroup parent) {                int color = getItem(position);                if (convertView == null) {                    convertView = new View(getContext());                    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(                            ViewGroup.LayoutParams.MATCH_PARENT,                            ViewGroup.LayoutParams.MATCH_PARENT                    );                    convertView.setLayoutParams(params);                }                convertView.setBackgroundColor(color);                return convertView;            }        };        svg = (ScrollerViewGroup) findViewById(R.id.svg);        if(svg==null) Log.e("main","null");        svg.setAdapter(adapter);    }    Integer[] color = new Integer[]{Color.RED, Color.BLUE, Color.YELLOW};

Android——自訂滾動ViewGroup

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.