Giter Site home page Giter Site logo

No On Scroll Event? about staggeredgridview HOT 4 OPEN

NathofGod avatar NathofGod commented on July 28, 2024
No On Scroll Event?

from staggeredgridview.

Comments (4)

GoMino avatar GoMino commented on July 28, 2024

I added the support in my version https://github.com/GoMino/StaggeredGridView, I still need to merge the code with this version of the StaggeredGridView, I based mine on the original one in the support library.

from staggeredgridview.

justinmc avatar justinmc commented on July 28, 2024

I also need this feature. The above fork does include it, but it seems to be missing a lot of the functionality in the main version right now (like onItemClick).

from staggeredgridview.

Awais16 avatar Awais16 commented on July 28, 2024

@justinmc you can add GoMino code:

paste this before return of onTouchEvent of maurycyw

switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                mVelocityTracker.clear();
                mScroller.abortAnimation();
                mLastTouchY = ev.getY();
                mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
                mTouchRemainderY = 0;
                break;

            case MotionEvent.ACTION_MOVE:
            {
                final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
                if (index < 0)
                {
                    Log.e(TAG, "onInterceptTouchEvent could not find pointer with id "
                            + mActivePointerId
                            + " - did StaggeredGridView receive an inconsistent " + "event stream?");
                    return false;
                }
                final float y = MotionEventCompat.getY(ev, index);
                final float dy = y - mLastTouchY + mTouchRemainderY;
                final int deltaY = (int) dy;
                mTouchRemainderY = dy - deltaY;

                if (Math.abs(dy) > mTouchSlop)
                {
                    setTouchMode(TOUCH_MODE_DRAGGING);
                }

                if (mTouchMode == TOUCH_MODE_DRAGGING)
                {
                    mLastTouchY = y;

                    if (!trackMotionScroll(deltaY, true))
                    {
                        // Break fling velocity if we impacted an edge.
                        mVelocityTracker.clear();
                    }
                }
            }
                break;

            case MotionEvent.ACTION_CANCEL:
                setTouchMode(TOUCH_MODE_IDLE);
                break;

            case MotionEvent.ACTION_UP:
            {
                mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                final float velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker,
                        mActivePointerId);
                if (Math.abs(velocity) > mFlingVelocity)
                { // TODO
                    setTouchMode(TOUCH_MODE_FLINGING);
                    mScroller.fling(0, 0, 0, (int) velocity, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
                    mLastTouchY = 0;
                    ViewCompat.postInvalidateOnAnimation(this);
                }
                else
                {
                    setTouchMode(TOUCH_MODE_IDLE);
                }

            }
                break;
        }

and in class add interface

public void setTouchMode(int newState){
//      mTouchMode = newState;
        switch (newState) {
        case TOUCH_MODE_DRAGGING:
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            break;
        case TOUCH_MODE_FLINGING:
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
            break;
        case TOUCH_MODE_IDLE:
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            break;
        }
    }

    private OnScrollListener mOnScrollListener;

    public void setOnScrollListener(OnScrollListener scrollListener) {
        mOnScrollListener = scrollListener;
        invokeOnItemScrollListener();
    }

    void reportScrollStateChange(int newState) {
        if (newState != mTouchMode) {
            mTouchMode = newState;
            if (mOnScrollListener != null) {
                mOnScrollListener.onScrollStateChanged(this, newState);
            }
        }
    }

    void invokeOnItemScrollListener() { 

        if (mOnScrollListener != null) {
            mOnScrollListener.onScroll(this, mFirstPosition, getChildCount(), mItemCount);
        }

    }

     /**
     * Interface definition for a callback to be invoked when the list or grid
     * has been scrolled.
     */
    public interface OnScrollListener {

        /**
         * The view is not scrolling. Note navigating the list using the trackball counts as
         * being in the idle state since these transitions are not animated.
         */
        public static int SCROLL_STATE_IDLE = 0;

        /**
         * The user is scrolling using touch, and their finger is still on the screen
         */
        public static int SCROLL_STATE_TOUCH_SCROLL = 1;

        /**
         * The user had previously been scrolling using touch and had performed a fling. The
         * animation is now coasting to a stop
         */
        public static int SCROLL_STATE_FLING = 2;

        /**
         * Callback method to be invoked while the list view or grid view is being scrolled. If the
         * view is being scrolled, this method will be called before the next frame of the scroll is
         * rendered. In particular, it will be called before any calls to
         * {@link Adapter#getView(int, View, ViewGroup)}.
         *
         * @param view The view whose scroll state is being reported
         *
         * @param scrollState The current scroll state. One of {@link #SCROLL_STATE_IDLE},
         * {@link #SCROLL_STATE_TOUCH_SCROLL} or {@link #SCROLL_STATE_IDLE}.
         */
        public void onScrollStateChanged(ViewGroup view, int scrollState);

        /**
         * Callback method to be invoked when the list or grid has been scrolled. This will be
         * called after the scroll has completed
         * @param view The view whose scroll state is being reported
         * @param firstVisibleItem the index of the first visible cell (ignore if
         *        visibleItemCount == 0)
         * @param visibleItemCount the number of visible cells
         * @param totalItemCount the number of items in the list adaptor
         */
        public void onScroll(ViewGroup view, int firstVisibleItem, int visibleItemCount, int totalItemCount);
    }

both things worked like charm for me. Didn't had time to merge

from staggeredgridview.

panzerdev avatar panzerdev commented on July 28, 2024

I did the same thing just using the AbsListView.OnScrollListener because libs like Universal Image Loader are based on that for pausing loading and so on.

 private OnScrollListener mOnScrollListener;

    public OnScrollListener getOnScrollListener() {
        return mOnScrollListener;
    }

    public void setOnScrollListener(OnScrollListener onScrollListener) {
        mOnScrollListener = onScrollListener;
    }

public void setTouchMode(int touchMode) {
        if (mOnScrollListener != null) {
            if (touchMode == TOUCH_MODE_IDLE && mTouchMode != TOUCH_MODE_IDLE) {
                mOnScrollListener.onScrollStateChanged(null, OnScrollListener.SCROLL_STATE_IDLE);
            } else if (touchMode == TOUCH_MODE_DRAGGING && mTouchMode != TOUCH_MODE_DRAGGING) {
                mOnScrollListener.onScrollStateChanged(null, OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            } else if (touchMode == TOUCH_MODE_FLINGING && mTouchMode != TOUCH_MODE_FLINGING) {
                mOnScrollListener.onScrollStateChanged(null, OnScrollListener.SCROLL_STATE_FLING);
            }
        }
        mTouchMode = touchMode;
    }

setTouchMode() is called everywhere the state changes between these three modes.

from staggeredgridview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.