Smoothscrolltopositionfromtop() Is Not Always Working Like It Should

smoothScrollToPositionFromTop() is not always working like it should

This is a known bug. See https://code.google.com/p/android/issues/detail?id=36062

However, I implemented this workaround that deals with all edge cases that might occur:

First call smothScrollToPositionFromTop(position) and then, when scrolling has finished, call setSelection(position). The latter call corrects the incomplete scrolling by jumping directly to the desired position. Doing so the user still has the impression that it is being animation-scrolled to this position.

I implemented this workaround within two helper methods:

smoothScrollToPositionFromTop()

public static void smoothScrollToPositionFromTop(final AbsListView view, final int position) {
View child = getChildAtPosition(view, position);
// There's no need to scroll if child is already at top or view is already scrolled to its end
if ((child != null) && ((child.getTop() == 0) || ((child.getTop() > 0) && !view.canScrollVertically(1)))) {
return;
}

view.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(final AbsListView view, final int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
view.setOnScrollListener(null);

// Fix for scrolling bug
new Handler().post(new Runnable() {
@Override
public void run() {
view.setSelection(position);
}
});
}
}

@Override
public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount,
final int totalItemCount) { }
});

// Perform scrolling to position
new Handler().post(new Runnable() {
@Override
public void run() {
view.smoothScrollToPositionFromTop(position, 0);
}
});
}

getChildAtPosition()

public static View getChildAtPosition(final AdapterView view, final int position) {
final int index = position - view.getFirstVisiblePosition();
if ((index >= 0) && (index < view.getChildCount())) {
return view.getChildAt(index);
} else {
return null;
}
}

smoothScrollToPositionFromTop for Froyo ListView?

Use

setSelection (int position)

smoothScrollToPositionFromTop not smooth when going to a lower index

Solved! I had to use a workaround, basically:

int topPosition = mListView.getFirstVisiblePosition();
if (targetPosition > topPosition) {
mListView.smoothScrollToPositionFromTop(targetPosition, 0, duration);
} else if (targetPosition < topPosition) {
View firstView = mListViewWeeks.getChildAt(0);
int height = firstView.getHeight();
mListView.smoothScrollBy(-height * (topPosition - targetPosition), duration);
}

smoothScrollToPosition after notifyDataSetChanged not working in android

Use the post() method to wait for the list to finish updating after you call notifyDataSetChanged():

adapter.notifyDataSetChanged();
list.post( new Runnable() {
@Override
public void run() {
//call smooth scroll
}
});

(Smooth)ScrollToPosition doesn't work properly with RecyclerView

Finally I was able to make it work! LinearLayoutManager.scrollToPositionWithOffset(int, int) did the trick.

smoothScrollToPosition Not Working-Android GridView

Instead of using gv.smoothScrollBy() try using gv.smoothScrollTo() and mention the specific position where you want it to show because scrollBy will scroll by that many pixels instead of pointing to that particular position.

And also see, if the values you provided are not in the bounds of screen, then it will start showing weird results, happens generally when you are trying out different device having different screen size and the pixels mentioned by you doesn't exist in visible area. For that try using:

private int getVisibleArea()
{
int measureValue = 0;
switch (getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_MEDIUM:
measureValue =(screenHeight);
break;
case DisplayMetrics.DENSITY_HIGH:
measureValue = (screenHeight);
break;
case DisplayMetrics.DENSITY_XHIGH:
measureValue = (screenHeight);
break;
}
return measureValue;
}

where screenHeight is:

Display display = getWindowManager().getDefaultDisplay();
screenHeight = display.getHeight();

Listview - How do I do a smooth scroll to top from the 10th item?

See this answer.

Quote:

First call smothScrollToPositionFromTop(position) and then, when
scrolling has finished, call setSelection(position). The latter call
corrects the incomplete scrolling by jumping directly to the desired
position. Doing so the user still has the impression that it is being
animation-scrolled to this position.



Related Topics



Leave a reply



Submit