How to scroll horizontally GridView programmatically in Android? - android

RecycleView can be scrolled with smoothScrollBy method. But Gridview is not extending RecycleView. How can I scroll GridView like the way RecycleView?
Tried with gridView.smoothScrollBy(0, dy); but it does not has any effect.

I think you're looking for smoothScrollByOffset(int offset).
Offset - The amount to offset from the adapter position to scroll to.
You can also use smoothScrollToPosition(int position).

I think the method you're looking for is scrollListBy(int y) from the AbsListView:
Scrolls the list items within the view by a specified number of
pixels.
You could also use smoothScrollBy(int y, int duration) for a smooth scroll.

Related

Get position of ListView scrollbar thumb on screen

I am trying to get the location on screen of the ListView scrollbar thumb indicator. I'd like to position a label next to it and require the X,Y screen coordinates.
I attempted to get the height of the ListView and use the value from the 'firstVisibleItem' value obtained from the ListView OnScrollListener to position the label but due to the ListView items having varying heights this hasn't proved feasible.
Are there any other solutions to this?
Thanks
I suggest you can use a little bit of hack:
View c = listview.getChildAt(0);
int scrolly = -c.getTop() + listview.getFirstVisiblePosition() * c.getHeight();
But it will work correctly only if you will have same height for all list items.

Scrolling of View happens after the position of scroll by touch

I have a customised View in an android application which is put inside a HorizontalScrollView as shown.
<HorizontalScrollView
android:id="#+id/feedBackScroller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<com.my.views.OfflineFeedbackView
android:id="#+id/feedBackView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/session_rec_page_title_bar_height"
android:layout_marginBottom="#dimen/session_rec_page_ctrl_bar_height"/>
</HorizontalScrollView>
The OfflineFeedbackView shows the pitch track of the audio track that I am playing and I scroll this view by calling scrollTo(newXPos, newYPos) based on the current time of the playback. The problem that I am facing is that if I scroll the screen by touching and then start the playback, the reference of scrolling seems to get changed on my view and the scrolling takes place from the position to which I scrolled to by touch. I went through the API docs and found that the scrollTo(newXPos, newYPos) internally calls onScrollChanged(int l, int t, int oldl, int oldt) where oldl and oldt are the old horizontal and vertical origins respectively. So, what I am interpreting from this is that when I scroll by touching on the screen these origin values get changed. So, I also tried calling onScrollChanged(newX, newY, 0, 0) but this just freezes the screen and there is no scroll. Am I doing something wrong? What can be other better ways to handle this?
Here problem looks like as you said HorizontalScrollView positions gets changed when you touch.
So one solution may be find the initial top-left position of your scroll view
HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.ScrollView);
int x, y;
x = hsv.getLeft();
y = hsv.getTop();
and always scroll your child view relative to these stored x,y positions i.e
childView.scrollTo(x+newXPos,y+newYPos);

Animate alpha from View's StartX to EndX (Growing horizontal Bar)

Current animation changes alpha for every part of view. I want to change the alpha through view's x and y positions. Think it as a growing horizontal bar. I have a view which is drawn by myself (canvas). Also the issue is this drawn views are in list view, they are the listview elements. So probably i need to animate ListView itself not the elements to achieve desired outcome
How can i achieve it?

Center a ListView on its current selection

Does anyone know of a way to center a ListView based on its current selection or selection set with setSelection?
I did see this other StackOverflow question without any answers: Android ListView center selection
Thanks,
Kevin
First, get the height of the ListView using getHeight, which returns the height of the ListView in pixels.
Then, get the height of the row's View using the same method.
Then, use setSelectionFromTop and pass in half of the ListView's height minus half of the row's height.
Something like:
int h1 = mListView.getHeight();
int h2 = v.getHeight();
mListView.setSelectionFromTop(position, h1/2 - h2/2);
Or, instead of doing the math, you might just pick a constant for the offset from the top, but I would think it might be more fragile on different devices since the second argument for setSelectionFromTop appears to be in pixels rather than device independent pixels.
I haven't tested this code, but it should work as long as your rows are all roughly the same height.
You will need to have the scroll view and the view of the item selected. Then you can simply do:
scrollView.smoothScrollTo(0, selectedView.getTop() - (scrollView.getHeight() / 2) + (selectedView.getHeight() / 2), 0);
This will center the scroll view exactly on selectedView
I haven't tried any of this but based on the current selection could you use public void smoothScrollByOffset (int offset) to get the view to scroll to where you want so that your selection is in the middle of the view?

GridView Scroll By one row

Hi i am implementing a gridView and i have trouble making it scroll down 1 row at a time with every scroll event.
my grid has a height of 1 row item (items height is 75dp). i don't want scrolling to be left in a middle of a row.
is there a way i can intercept and modify the scroll distance so that it only returns fixed value ex: +-75dp.
i would appreciate any help or suggestions you can give me. tnx
APIv8 has new function, called smoothScrollBy(int distance, int duration)[1]
i think you should catch all scroll events & implement own method to scroll view.
if you want to scroll by 75dp, just convert it to pixels & use function above.
float density = getContext().getResources().getDisplayMetrics().density;
int scrollBy = (int)(density * 75);
smoothScrollBy(scrollBy, 0);
But would be nice to calculate scrollBy from your GridView, instead of using some constant value (like 75dp)
[1]: http://developer.android.com/reference/android/widget/AbsListView.html#smoothScrollBy(int, int)

Categories

Resources