cn.org.eshow.framwork.view.sliding.AbSlidingPageView.java Source code

Java tutorial

Introduction

Here is the source code for cn.org.eshow.framwork.view.sliding.AbSlidingPageView.java

Source

/*
 * Copyright (C) 2012 www.amsoft.cn
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.org.eshow.framwork.view.sliding;

/*
 * Copyright (C) 2012 www.amsoft.cn
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import android.content.Context;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

// TODO: Auto-generated Javadoc

/**
 * ??,?Touch?.
 *
 */
public class AbSlidingPageView extends ViewGroup {

    /** . */
    private String TAG = AbSlidingPageView.class.getSimpleName();

    /** . */
    private boolean D = true;

    /** . */
    private Scroller mScroller;

    /** . */
    private VelocityTracker mVelocityTracker;

    /** View. */
    public static final int SCREEN_STATE_NEXT = 0;

    /** ?View. */
    public static final int SCREEN_STATE_PROVIOUS = 1;

    /** ???. */
    private int mScreenState = SCREEN_STATE_PROVIOUS;

    /** View??. */
    private int nextViewOffset = 50;

    /** ???. */
    private OnPageChangeListener onPageChangeListener = null;

    /** ??. */
    private boolean finish = true;

    /**
     * .
     * @param context the context
     */
    public AbSlidingPageView(Context context) {
        super(context);
        mScroller = new Scroller(context);

    }

    /**
     * .
     * @param context the context
     * @param attrs the attrs
     */
    public AbSlidingPageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * ??View?.
     *
     * @param changed the changed
     * @param l the l
     * @param t the t
     * @param r the r
     * @param b the b
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //?      
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            int height = child.getMeasuredHeight();
            int width = child.getMeasuredWidth();
            child.setFocusable(true);
            if (D)
                Log.d(TAG, "--onLayout--:" + width);
            if (i == 0) {
                child.layout(-nextViewOffset, 0, nextViewOffset + width, height);
            } else {
                child.layout(width - nextViewOffset, 0, 2 * width - nextViewOffset, height);
            }
        }

    }

    /**
     * ???View.
     * @param widthMeasureSpec the width measure spec
     * @param heightMeasureSpec the height measure spec
     * @see android.view.View#onMeasure(int, int)
     */
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (D)
            Log.d(TAG, "--onMeasure--");
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    /**
     * View.
     */
    public void showNext() {
        if (!finish || mScreenState == SCREEN_STATE_NEXT) {
            return;
        }
        finish = false;
        mScreenState = SCREEN_STATE_NEXT;
        if (onPageChangeListener != null) {
            onPageChangeListener.onPageSelected(1);
        }
        if (D)
            Log.d(TAG, "--showNext--:" + getScrollX() + " dx " + (getChildAt(1).getWidth() - 2 * nextViewOffset));
        //?onlayout???
        mScroller.startScroll(getScrollX(), 0, getChildAt(1).getWidth() - 2 * nextViewOffset, 0, 800);
        invalidate();
    }

    /**
     * View.
     */
    public void showPrevious() {
        if (!finish || mScreenState == SCREEN_STATE_PROVIOUS) {
            return;
        }
        mScreenState = SCREEN_STATE_PROVIOUS;
        if (onPageChangeListener != null) {
            onPageChangeListener.onPageSelected(0);
        }
        if (D)
            Log.d(TAG, "--showPrevious--:" + getScrollX() + " dx -" + getScrollX());
        //onlayout0?
        mScroller.startScroll(getScrollX(), 0, -getScrollX(), 0, 800);
        invalidate();
    }

    /**
     * ??.
     * @see android.view.View#computeScroll()
     */
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
        //?
        if (mScroller.getFinalX() == mScroller.getCurrX()) {
            finish = true;
        }
    }

    /**
     * ?.
     * @param event the event
     */
    private void obtainVelocityTracker(MotionEvent event) {
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(event);
    }

    /**
     * .
     */
    private void releaseVelocityTracker() {
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
    }

    /**
     * ???.
     * @return the screen state
     */
    public int getScreenState() {
        return mScreenState;
    }

    /**
     * View.
     *
     * @param view the view
     */
    public void addContentView(View view) {
        addView(view, 0, getLayoutParams());
        view.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    showPrevious();
                }

            }
        });

        view.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    showPrevious();
                }
                return false;
            }
        });
    }

    /**
     * View.
     *
     * @param view the view
     */
    public void addNextView(View view) {
        addView(view, 1, getLayoutParams());
        view.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    showNext();
                }

            }
        });

        view.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    showNext();
                }
                return false;
            }
        });
    }

    /**
     * Gets the next view offset.
     *
     * @return the next view offset
     */
    public int getNextViewOffset() {
        return nextViewOffset;
    }

    /**
     * ??.
     *
     * @param nextViewOffset the new next view offset
     */
    public void setNextViewOffset(int nextViewOffset) {
        this.nextViewOffset = nextViewOffset;
    }

    /**
     * Gets the on page change listener.
     *
     * @return the on page change listener
     */
    public OnPageChangeListener getOnPageChangeListener() {
        return onPageChangeListener;
    }

    /**
     * ???.
     *
     * @param onPageChangeListener the new on page change listener
     */
    public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener) {
        this.onPageChangeListener = onPageChangeListener;
    }

}