自定義viewgroup(6)--使用adapter適配資料

封魔之殤發表於2016-12-20

在自定義viewgroup(5):http://blog.csdn.net/qq_18148011/article/details/53761587的基礎上,新增使用adapter來適配資料,這樣更加的方便,這裡只是使用adapter適配資料,不能更新。

package com.example.libingyuan.horizontallistview.ScrollViewGroup;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.Scroller;

/**
 * 自定義ViewGroup(橫向滾動)
 */
public class ScrollViewGroup extends ViewGroup {
    //滾動計算輔助類
    private Scroller mScroller;
    //螢幕寬度
    private int screenWidth;
    //可以移動的最大距離
    private int mMaxDistance;
    //自定義手勢監聽類
    private ScrollTouchLisener mTouchLisener;
    //手勢監聽
    private GestureDetector mDetector;

    private BaseAdapter mAdapter;

    /**
     * 使用new關鍵字建立物件的時候呼叫
     */
    public ScrollViewGroup(Context context) {
        this(context, null);
    }

    /**
     * 在XML檔案中使用的時候呼叫
     */
    public ScrollViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * 在xml檔案中呼叫,並且使用了自定義屬性的時候呼叫
     */
    public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 初始化方法
     * 初始化滾動輔助類Scroller以及計算出螢幕寬度
     */
    private void init(Context context) {
        //初始化輔助類
        mScroller = new Scroller(context);
        //獲取螢幕寬度
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        screenWidth = outMetrics.widthPixels;
        //手勢指示器初始化
        mTouchLisener = new ScrollTouchLisener();
        mDetector = new GestureDetector(context, mTouchLisener);
    }

    /**
     * 滾動時需要重寫的方法,用於控制滾動
     */
    @Override
    public void computeScroll() {
        //判斷滾動時候停止
        if (mScroller.computeScrollOffset()) {
            //滾動到指定的位置
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            //這句話必須寫,否則不能實時重新整理
            invalidate();
        }
    }

    /**
     * 手指觸屏事件監聽
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mDetector.onTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_UP) {
            this.onUp(event);
        }
        return true;
    }


    public void setAdapter(BaseAdapter adapter) {
        this.mAdapter = adapter;
        requestLayout();
    }

    /*
     *測量方法,測量父佈局的寬度和高度
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        removeAllViews();
        for (int i = 0; i < mAdapter.getCount(); i++) {
            View child = mAdapter.getView(i, null, null);
            addView(child);
        }
        //重新設定寬高
        this.setMeasuredDimension(measureWidth(widthMeasureSpec, heightMeasureSpec), measureHeight(widthMeasureSpec, heightMeasureSpec));
    }

    /**
     * 測量寬度
     */
    private int measureWidth(int widthMeasureSpec, int heightMeasureSpec) {
        // 寬度
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        //父控制元件的寬(wrap_content)
        int width = 0;
        int childCount = getChildCount();

        //重新測量子view的寬度,以及最大高度
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
           // LayoutParams lp = child.getLayoutParams();
           /* MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            width += childWidth;*/
            width += child.getMeasuredWidth();
        }
        return modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width;
    }


    /**
     * 測量高度
     */
    private int measureHeight(int widthMeasureSpec, int heightMeasureSpec) {
        //高度
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        //父控制元件的高(wrap_content)
        int height = 0;
        int childCount = getChildCount();

        //重新測量子view的寬度,以及最大高度
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
           /* MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            height += childHeight;*/
            height += child.getMeasuredHeight();
        }
        height = height / childCount;
        return modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height;
    }

    /**
     * 給子佈局設定位置
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childLeft = 0;//子View左邊的間距
        int childWidth;//子View的寬度
        int height = getHeight();//螢幕的寬度
        int childCount = getChildCount();//子View的數量
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
         /*   MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;*/
            childWidth = child.getMeasuredWidth();
            child.layout(childLeft, 0, childLeft + childWidth, height);
            childLayout(child);
            childLeft += childWidth;
        }
    }

    /**
     *讓子View的子View和子View大小一樣
     */
    private void childLayout(View child){
        if (child==null){
            throw new IllegalStateException("ScrollViewGroup must has one child");
        }
        if (child instanceof ViewGroup){
           if(((ViewGroup)child).getChildCount()>0)
               throw new IllegalStateException("view can host only one direct child");
            ((ViewGroup) child).getChildAt(0).layout(0,0,child.getWidth(),child.getHeight());
        }

    }
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    /*
     *按下事件 ACTION_DOWN
     */
    public boolean onDown(MotionEvent e) {
        //得到最後一個子View
        View lastChild = getChildAt(getChildCount() - 1);
        //獲取滑動的最大滑動距離(最後一個Child的右邊框的座標減去螢幕的寬度)
        int finalyChild = (int) (lastChild.getX() + lastChild.getWidth() - screenWidth);
        mMaxDistance = finalyChild;
        //如果停止滾動則取消動畫(即手指按下就停止滾動)
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        return false;
    }

    /*
    *抬起事件 ACTION_UP
    */
    public boolean onUp(MotionEvent e) {
        //如果滑動的距離小於第一個控制元件的最左邊(0)則回彈至(0,0)點
        if (getScrollX() <= 0) {
            scrollTo(0, 0);
        }
        //如果滑動的距離大於最大可滑動距離則滑動到最後一個子View
        if (getScrollX() >= mMaxDistance) {
            scrollTo(mMaxDistance, 0);
        }
        //重新整理介面
        invalidate();
        return false;
    }

    /*
     *ACTION_DOWN 、短按不移動
     */
    public void onShowPress(MotionEvent e) {
    }

    /*
     *短按ACTION_DOWN、ACTION_UP
     */
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    /*
     *ACTION_DOWN 、慢滑動
     */
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        //滾動
        scrollBy((int) distanceX, 0);
        return false;
    }

    // ACTION_DOWN 、長按不滑動
    public void onLongPress(MotionEvent e) {
    }

    /*
     *ACTION_DOWN 、快滑動、 ACTION_UP
     */
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        mScroller.fling(getScrollX(), 0, (int) -velocityX, 0, 0, mMaxDistance, 0, 0);
        return false;
    }

    /**
     * 自定義手勢監聽類
     */
    private class ScrollTouchLisener implements GestureDetector.OnGestureListener {

        //按下事件
        @Override
        public boolean onDown(MotionEvent e) {
            return ScrollViewGroup.this.onDown(e);
        }

        //單擊事件
        @Override
        public void onShowPress(MotionEvent e) {
            ScrollViewGroup.this.onShowPress(e);
        }

        //手指抬起事件
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return ScrollViewGroup.this.onSingleTapUp(e);
        }

        //滾動事件
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return ScrollViewGroup.this.onScroll(e1, e2, distanceX, distanceY);
        }

        //長按事件
        @Override
        public void onLongPress(MotionEvent e) {
            ScrollViewGroup.this.onLongPress(e);
        }

        //滑動事件
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return ScrollViewGroup.this.onFling(e1, e2, velocityX, velocityY);
        }
    }

}

相關文章