Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7068945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:23:08+00:00 2026-05-28T05:23:08+00:00

Does smoothScrollToPosition() method for GridView works correctly? I have found an open bug report

  • 0

Does smoothScrollToPosition() method for GridView works correctly?
I have found an open bug report and mine is not working correctly either.

setSelection() is working fine but I want a smooth scroll effect.

Do you have any idea about this issue?

If this is a persistent problem, where should I start to implement a good scroll effect?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:23:09+00:00Added an answer on May 28, 2026 at 5:23 am

    While I’m not seeing any issues running the example code you posted, if you’re not seeing consistent results in your application it’s not too tricky to create your own scroll controller. Here’s an example implementation you could use:

    private class ScrollPositioner {
        private static final int SCROLL_DURATION = 20;
        private static final int DIR_UP = 1;
        private static final int DIR_DOWN = 2;
    
        int mTargetPosition = AdapterView.INVALID_POSITION;
        int mDirection = AdapterView.INVALID_POSITION;
        int mLastSeenPosition = AdapterView.INVALID_POSITION;
        int mExtraScroll;
        GridView mGrid;
    
        public ScrollPositioner(GridView grid) {
            mGrid = grid;
            mExtraScroll = ViewConfiguration.get(mGrid.getContext()).getScaledFadingEdgeLength();
        }
    
        Handler mHandler = new Handler();
        Runnable mScroller = new Runnable() {
            public void run() {
                int firstPos = mGrid.getFirstVisiblePosition();
                switch(mDirection) {
                case DIR_UP: {
                    if (firstPos == mLastSeenPosition) {
                        // No new views, let things keep going.
                        mHandler.postDelayed(mScroller, SCROLL_DURATION);
                        return;
                    }
    
                    final View firstView = mGrid.getChildAt(0);
                    if (firstView == null) {
                        return;
                    }
                    final int firstViewTop = firstView.getTop();
                    final int extraScroll = firstPos > 0 ? mExtraScroll : mGrid.getPaddingTop();
    
                    mGrid.smoothScrollBy(firstViewTop - extraScroll, SCROLL_DURATION);
    
                    mLastSeenPosition = firstPos;
    
                    if (firstPos > mTargetPosition) {
                        mHandler.postDelayed(mScroller, SCROLL_DURATION);
                    }
                    break;
                }
    
                case DIR_DOWN: {
                    final int lastViewIndex = mGrid.getChildCount() - 1;
                    final int lastPos = firstPos + lastViewIndex;
    
                    if (lastViewIndex < 0) {
                        return;
                    }
    
                    if (lastPos == mLastSeenPosition) {
                        // No new views, let things keep going.
                        mHandler.postDelayed(mScroller, SCROLL_DURATION);
                        return;
                    }
    
                    final View lastView = mGrid.getChildAt(lastViewIndex);
                    final int lastViewHeight = lastView.getHeight();
                    final int lastViewTop = lastView.getTop();
                    final int lastViewPixelsShowing = mGrid.getHeight() - lastViewTop;
                    final int extraScroll = lastPos < mGrid.getAdapter().getCount() - 1 ? mExtraScroll : mGrid.getPaddingBottom();
    
                    mGrid.smoothScrollBy(lastViewHeight - lastViewPixelsShowing + extraScroll, SCROLL_DURATION);
    
                    mLastSeenPosition = lastPos;
                    if (lastPos < mTargetPosition) {
                        mHandler.postDelayed(mScroller, SCROLL_DURATION);
                    }
                    break;
                }
    
                default:
                    break;
                }
            }
        };
    
        public void scrollToPosition(int position) {
            mTargetPosition = position;
            mLastSeenPosition = AdapterView.INVALID_POSITION;
    
            if(position < mGrid.getFirstVisiblePosition()) {
                mDirection = DIR_UP;
            } else if (position > mGrid.getLastVisiblePosition()) {
                mDirection = DIR_DOWN;
            } else {
                return;
            }
            mHandler.post(mScroller);
        }
    }
    

    Here’s a snippet from the example you linked that includes where this new class to do the scrolling in place of the framework implementation:

    GridView g;
    boolean t= false;
    @Override
    public void onBackPressed() {
        //Instantiate and attach the custom positioner
        if(mPositioner == null) {
            mPositioner = new ScrollPositioner(g);
        }
        //Use the custom object to scroll the view
        mPositioner.scrollToPosition(0);
        if(t) {
            super.onBackPressed();
        }
        else {
            t = true;
        }
    }
    

    If you want to add a feature where the selected position is always scrolled to the top even when the scrolling direction is down, you could do that. This is not something the framework’s implementation is designed to do, but you could accomplish it by adding some code in DIR_DOWN to continue scrolling until the first visible position matches target (like DIR_UP does). You must also beware of the case where the scrolling ends before the position reaches the top, so you aren’t constantly posting the handler in cases where you will never get a different result.

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have a working, step-by-step example of how to implement IEnumerable and IEnumerator
Does Google force employees who have offers from Facebook to leave immediately?
Does the Java language have delegate features, similar to how C# has support for
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does anyone have any recommendations of tools that can be of assistance with moving
Does anyone use have a good regex library that they like to use? Most
Does C# have built-in support for parsing strings of page numbers? By page numbers,
Does anyone have a definitive answer to whether Sql Server Management Objects is compatible
Does any one have a solution to make the SpecFlow autocomplete in Visual Studio
Does anyone know of a Mootools script that provides nested sortable but also works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.