Smooth Scroll For Gridview
Solution 1:
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:
privateclassScrollPositioner {
privatestaticfinalintSCROLL_DURATION=20;
privatestaticfinalintDIR_UP=1;
privatestaticfinalintDIR_DOWN=2;
intmTargetPosition= AdapterView.INVALID_POSITION;
intmDirection= AdapterView.INVALID_POSITION;
intmLastSeenPosition= AdapterView.INVALID_POSITION;
int mExtraScroll;
GridView mGrid;
publicScrollPositioner(GridView grid) {
mGrid = grid;
mExtraScroll = ViewConfiguration.get(mGrid.getContext()).getScaledFadingEdgeLength();
}
HandlermHandler=newHandler();
RunnablemScroller=newRunnable() {
publicvoidrun() {
intfirstPos= mGrid.getFirstVisiblePosition();
switch(mDirection) {
case DIR_UP: {
if (firstPos == mLastSeenPosition) {
// No new views, let things keep going.
mHandler.postDelayed(mScroller, SCROLL_DURATION);
return;
}
finalViewfirstView= mGrid.getChildAt(0);
if (firstView == null) {
return;
}
finalintfirstViewTop= firstView.getTop();
finalintextraScroll= 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: {
finalintlastViewIndex= mGrid.getChildCount() - 1;
finalintlastPos= firstPos + lastViewIndex;
if (lastViewIndex < 0) {
return;
}
if (lastPos == mLastSeenPosition) {
// No new views, let things keep going.
mHandler.postDelayed(mScroller, SCROLL_DURATION);
return;
}
finalViewlastView= mGrid.getChildAt(lastViewIndex);
finalintlastViewHeight= lastView.getHeight();
finalintlastViewTop= lastView.getTop();
finalintlastViewPixelsShowing= mGrid.getHeight() - lastViewTop;
finalintextraScroll= 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;
}
}
};
publicvoidscrollToPosition(int position) {
mTargetPosition = position;
mLastSeenPosition = AdapterView.INVALID_POSITION;
if(position < mGrid.getFirstVisiblePosition()) {
mDirection = DIR_UP;
} elseif (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;
@OverridepublicvoidonBackPressed() {
//Instantiate and attach the custom positionerif(mPositioner == null) {
mPositioner = newScrollPositioner(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
Solution 2:
Had the same issue- I built a simple GridView and for some reason the smoothScrollToPosition()
didn't work at all (just bounce from time to time).
after lot of debugging it turn out to be that I need to remove the android:padding
parameter from the GridView.
very strange, I really don't know why it is like this, but at least now it works.
Post a Comment for "Smooth Scroll For Gridview"