android 卡片式浏览

来源:互联网 发布:profili软件怎么用 编辑:程序博客网 时间:2024/04/30 09:48


public class SlideBrowsePageView extends ViewGroup{

    private String TAG = "Workspace";
    private static final int INVALID_SCREEN = -1;
    private static final int SNAP_VELOCITY = 10;

    private int mDefaultScreen = 0;
    private boolean mFirstLayout = true;
    private int mCurrentScreen;
    private int mNextScreen = INVALID_SCREEN;
    private Scroller mScroller;
    private VelocityTracker mVelocityTracker;


    private float mLastMotionX;
    private int mMaximumVelocity;

    public SlideBrowsePageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);       
    }

    public SlideBrowsePageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setHapticFeedbackEnabled(false);
        initWorkspace();
    }

    private void initWorkspace() {
        Context context = getContext();
        mScroller = new Scroller(context);
        mCurrentScreen = mDefaultScreen;
        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
    }

    @Override
    public void computeScroll() {
       
        if (mScroller.computeScrollOffset()) {
            mScrollX = mScroller.getCurrX();
            mScrollY = mScroller.getCurrY();
            postInvalidate();
        } else if (mNextScreen != INVALID_SCREEN) {
            mCurrentScreen = Math.max(0, Math.min(mNextScreen, getChildCount() - 1));
            mNextScreen = INVALID_SCREEN;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int width = MeasureSpec.getSize(widthMeasureSpec);

        // The children are given the same width and height as the workspace
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
        }

        if (mFirstLayout) {
            scrollTo(mCurrentScreen * width, 0);
            mFirstLayout = false;
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int childLeft = 0;

        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                final int childWidth = child.getMeasuredWidth();
                child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight());
                childLeft += childWidth;
            }
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
 
        if (mVelocityTracker == null) {
           
            mVelocityTracker = VelocityTracker.obtain();
        }
       
        mVelocityTracker.addMovement(ev);

        final int action = ev.getAction();
        final float x = ev.getX();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
           
            if (!mScroller.isFinished()) {
                mScroller.abortAnimation();
            }

            // Remember where the motion event started
            mLastMotionX = x;
            break;
        case MotionEvent.ACTION_MOVE:
           
            final int deltaX = (int) (mLastMotionX - x);
            mLastMotionX = x;

            if (deltaX < 0 && getScrollX() > 0) {
               
                scrollBy(Math.max(-getScrollX(), deltaX), 0);
                invalidate();
               
            } else if (deltaX > 0) {

                final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - getScrollX() - getWidth();

                if (availableToScroll > 0) {
                   
                    scrollBy(Math.min(availableToScroll, deltaX), 0);
                    invalidate();
                }
            }
           
            break;
        case MotionEvent.ACTION_UP:
           
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity();

            if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
                // Fling hard enough to move left
                snapToScreen(mCurrentScreen - 1);
            } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
                // Fling hard enough to move right
                snapToScreen(mCurrentScreen + 1);
            } else {
                snapToDestination();
            }
            if (null != mVelocityTracker) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
           
            break;
           
       
        }

        return true;
    }

    void snapToDestination() {
        final int screenWidth = getWidth();
       
        final int delta =  (getScrollX() < (screenWidth * mCurrentScreen) ? screenWidth * - 6 / 10 : screenWidth * 6 / 10);
       
        final int whichScreen = (getScrollX() + delta) / screenWidth;

        snapToScreen(whichScreen);
    }

   
    void snapToScreen(int whichScreen) {
       
        if (!mScroller.isFinished()){
           
            return;
        }           

        whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
        boolean changingScreens = whichScreen != mCurrentScreen;
        mNextScreen = whichScreen;

        View focusedChild = getFocusedChild();
        if (focusedChild != null && changingScreens && focusedChild == getChildAt(mCurrentScreen)) {
           
            focusedChild.clearFocus();
        }

        final int newX = whichScreen * getWidth();

        final int delta = newX - getScrollX();
        mScroller.startScroll(getScrollX(), 0, delta, 0, (int) (Math.abs(delta) * 0.6));

        invalidate();
    }   
   
    @Override
    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
       
        int focusableScreen;
       
        if (mNextScreen != INVALID_SCREEN) {
           
            focusableScreen = mNextScreen;
        } else {
           
            focusableScreen = mCurrentScreen;
        }
       
        getChildAt(focusableScreen).requestFocus(direction, previouslyFocusedRect);
        return false;
    }
   
    /**
     * Interface used to allow the creator of a switcher to run some code when
     * a view on the switcher is clicked.
     */
    public static interface OnClickListener {
        /**
         * This method will be invoked when a view in the switcher is clicked.
         */
        public abstract void onClick(int position);
    }
   
    /**
     * Interface used to allow the creator of a switcher to run some code when
     * a view on the switcher is selected.
     */
    public static interface OnSelectListener {
        /**
         * This method will be invoked when a view in the switcher is selected.
         */
        public abstract void onSelect(int position);
    }
}

 

 

 

 

 

 

 

 

 

        WeatherPageAdapter mAdapter = new WeatherPageAdapter(list, this);

        for(int i=0;i<mAdapter.getCount();i++){
           
            switcher.addView(mAdapter.getView(i, null, null));
        }

原创粉丝点击