Android Open Source - iPhoroidUI Scroll Detectable Gallery






From Project

Back to project page iPhoroidUI.

License

The source code is released under:

Apache License

If you think the Android project iPhoroidUI listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2011 by KLab Inc., All rights reserved.
 */*w  w  w.j av a  2  s . c om*/
 * Programmed by Naohide Sano
 */

package org.klab.iphoroid.widget.gallery;

import org.klab.iphoroid.widget.adpterview.OnScrollListener;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;
import android.widget.Scroller;


/**
 * ListView ?????????? OnScrollListener ?????????? Gallery? 
 *
 * @author <a href="mailto:sano-n@klab.jp">Naohide Sano</a> (sano-n)
 * @version 0.00 2011/07/09 sano-n initial version <br>
 */
public class ScrollDetectableGallery extends Gallery {

    /** */
    public ScrollDetectableGallery(Context context) {
        super(context);
    }

    /** */
    public ScrollDetectableGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /** */
    public ScrollDetectableGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /** */
    private OnScrollListener onScrollListener;

    /** */
    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    /** */
    private int scrollState = OnScrollListener.SCROLL_STATE_IDLE;

    /***
     * Executes the delta scrolls from a fling or scroll movement.
     */
    private FlingRunnable mFlingRunnable = new FlingRunnable();

    /**
     * Responsible for fling behavior. Use {@link #startUsingVelocity(int)} to
     * initiate a fling. Each frame of the fling is handled in {@link #run()}.
     * A FlingRunnable will keep re-posting itself until the fling is done.
     *
     */
    private class FlingRunnable implements Runnable {
        /***
         * Tracks the decay of a fling scroll
         */
        private Scroller mScroller;

        public FlingRunnable() {
            mScroller = new Scroller(getContext());
        }

        private void startCommon() {
            // Remove any pending flings
            removeCallbacks(this);
        }
        
        public void startUsingVelocity(int initialVelocity) {
            if (initialVelocity == 0) {
                return;
            }
            
            startCommon();
            
            int initialX = initialVelocity < 0 ? Integer.MAX_VALUE : 0;
            mScroller.fling(initialX, 0, initialVelocity, 0, 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE);
            post(this);
        }

        private void endFling(boolean scrollIntoSlots) {
            // Force the scroller's status to finished (without setting its
            // position to the end)
            mScroller.forceFinished(true);

            fireScrollStateChanged(OnScrollListener.SCROLL_STATE_IDLE);
        }

        public void run() {
            if (getChildCount() == 0) {
                endFling(true);
                return;
            }

            final Scroller scroller = mScroller;
            boolean more = scroller.computeScrollOffset();

            if (more) {
                post(this);
            } else {
                endFling(true);
            }
        }
    }    

    /** */
    protected void fireScrollStateChanged(int scrollState) {
        if (this.scrollState != scrollState) {
            onScrollListener.onScrollStateChanged(this, scrollState);
            this.scrollState = scrollState;
        }
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        mFlingRunnable.startUsingVelocity((int) -velocityX);
        fireScrollStateChanged(OnScrollListener.SCROLL_STATE_FLING);
        return super.onFling(e1, e2, velocityX, velocityY);
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        fireScrollStateChanged(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
        return super.onScroll(e1, e2, distanceX, distanceY);
    }
}




Java Source Code List

org.klab.iphoroid.util.ActivityUtil.java
org.klab.iphoroid.util.Cache.java
org.klab.iphoroid.widget.adpterview.OnScrollListener.java
org.klab.iphoroid.widget.coverflow.CoverFlowGallery.java
org.klab.iphoroid.widget.coverflow.CoverFlowImageAdapterBase.java
org.klab.iphoroid.widget.flowview.CircleFlowIndicator.java
org.klab.iphoroid.widget.flowview.FlowIndicator.java
org.klab.iphoroid.widget.flowview.FlowView.java
org.klab.iphoroid.widget.flowview.TitleFlowIndicator.java
org.klab.iphoroid.widget.flowview.TitleProvider.java
org.klab.iphoroid.widget.gallery.ScrollDetectableGallery.java
org.klab.iphoroid.widget.listview.AdapterWrapper.java
org.klab.iphoroid.widget.listview.EndlessAdapter.java
org.klab.iphoroid.widget.listview.PullToRefreshEndlessListView.java
org.klab.iphoroid.widget.listview.PullToRefreshListView.java
org.klab.iphoroid.widget.listview.RefreshableArrayAdapter.java
org.klab.iphoroid.widget.support.DownloadTask.java
org.klab.iphoroid.widget.support.HasImage.java
org.klab.iphoroid.widget.support.ImageCache.java
org.klab.iphoroid.widget.support.ImageDownloadTask.java
org.klab.iphoroid.widget.support.SimpleImageDownloadTask.java