Android: Total height of ScrollView - android

I have a custom ScrollView (extended android.widget.ScrollView) which I use in my layout. I want to measure the total height of the contents of this scrollview. getHeight() and getMeasuredHeight() don't give me correct values (too high numbers).
Background information: I want to determine how far the user has scrolled. I use onScrollChanged to get the X value, but I need to know a percentage so I'll need the total scrollbar height.
Thanks a lot!
Erik

A ScrollView always has 1 child. All you need to do is get the height of the child to determine the total height:
int totalHeight = scrollView.getChildAt(0).getHeight();

See the source of ScrollView. Unfortunately, this method is private, but you could copy it into your own code.
Note that other answers don't take padding into account
private int getScrollRange() {
int scrollRange = 0;
if (getChildCount() > 0) {
View child = getChildAt(0);
scrollRange = Math.max(0,
child.getHeight() - (getHeight() - mPaddingBottom - mPaddingTop));
}
return scrollRange;
}

Related

google photos application timeline feature using the year of images when they created

I want to implement the google photos application timeline feature in the recycler view in which we can show the year of images as a label while scrolling as shown in below image.
Can anyone help in this or implemented something like this.
I have solved this problem by simply adding a new Linear-layout on top of the recyclerview with a child textview in the layout and then by calculating the height ratio(as per the count of images per year) with respect to the total height available.Also considering the minimum height of textview to 100 if the ratio is too small.
private float getEffectiveHeight(float totalHeight, float count, float totalCount) {
if (count * (totalHeight / totalCount) < 100)
return 100;
else
return count * (totalHeight / totalCount);
}

Android #View.getHeight() doesn't return correct value

I have a listView in my screen. This listView fills 2/3 of my screen (so its height varies on different devices). I have - lets say 50 items in my array list - but I need to put X number of items in it which is visible to the user (e.g on small screen 3, on normal screen 5, on tablet 8).
So the way I'm doing is getting the height of listView in PX, then get its DP size and since I know the height of each row is 60dp then divide height of listViwe in DP format to height of row (which 60dp). So it returns number of items that will be visible to the user and I pass this number to getCount() method of my adapter to populate this amount of items.
This is my code, I have an observer that will be assigned some where in onCreate() method like this this.mListView.getViewTreeObserver().addOnGlobalLayoutListener(mListViewGlobalListener);
ViewTreeObserver.OnGlobalLayoutListener mListViewGlobalListener = new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
removeListViewListener();
if (mListView == null || mAdapter == null)
{
return;
}
int heightInPx = mListView.getHeight();
int heightInDp = pixelsToDp(heightInPx, getResources().getDisplayMetrics());
int maxDisplayDriver = heightInDp / 60;
mAdapter.setMaxDisplayDrivers(maxDisplayDriver);
Logger.error("TEST", "heightInPx:" + heightInPx + ", heightInDp:" + heightInDp + ", maxDisplayDriver:" + maxDisplayDriver);
}
};
public static int pixelsToDp(int px, DisplayMetrics metrics)
{
return (int) (px / metrics.density);
}
Now when I run the app I have following log: E/TEST: heightInPx:96, heightInDp:32, maxDisplayDriver:0
I believe the height is wrong because I can put at least 4 items in my screen that will be 240dp, while the log shows something else.
You can get the height of listview this way because a listview can have n number of childs and getHeight() returns the actual height not the height of currently visible portion. So for listview and other similar view it doesn't return height as even system doesn't know because all childitems are not yet rendered.
So as suggested by other geeks as well, you need to calculate the height of item + margin+padding (if any) and then multiply it by number of child(how do you know this? read ahead).
Now number of child would be different in different screen size. So you need to put a counter inside getView to check how many times its getting called onCreate to get to know the number of childitem

RecyclerView offset drops when changing the row height in Android

Recently I've started working on custom scroll for RecyclerView but I've encountered a problem. I did header parallax and it worked great, but then I wanted to make rows expand when you scroll. For this method I needed to use recyclerView.computeVerticalScrollOffset() it works fine for the first two items in the list but then it falls apart.
Here is an example of my code:
FrameLayout main = (FrameLayout) holder.itemView;
if (main != null) {
int height = calculateHeight(offset, (position + i));
RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) main.getLayoutParams();
lp.height = height;
main.setLayoutParams(lp);
}
So I have a function that calculates the height based on the position in layout and offset of the recyclerview. When the second item reaches Y == 0 the offset drops for half (i.e. it drops from 1200 to 600 and then it goes on but the calculations are all wrong)
Any ideas on how to get a stable offset value or how to change it with something that is consistent with recyclerView position.
EDIT:
OK, I can describe the problem step by step:
Element at position(0) of the adapter has a height of 1200
All other elements have the default height of 300
When user starts to scroll down, the element at position(1) starts to expand from 300 to 1200 height
--------- This is where the code starts to crack... -----------
When element at position(1) with the new height of 1200 reaches the top of the screen (when getTop() == 0) offset here is around 900.
When this same element starts to get off the screen (user is scrolling down) the offset at the point where top screen and this element collides (0,0) breaks down by the half, so offset is now 450 when it should be 900, and then it goes from 450 on, but my element at position(1) is now only half the size it should be...
OK I created a workaround for this solution and I am posting this as answer, if someone needs it in the future.
Calculate your own offset:
public int offsetForVerticalScrolling(RecyclerView recyclerView) {
LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
int position = llm.findFirstVisibleItemPosition();
ViewHolder mViewHolder = (ViewHolder) recyclerView.findViewHolderForAdapterPosition(position);
View item = mViewHolder.itemView;
int y = (int) item.getY();
if (y < 0) y *= -1;
if (position == 0) return y;
else {
int offset = y;
for (int i = 0; i < position; i++) {
//Add your previous item heights to offset
}
return offset;
}
}

Exact position of scroll in GridView [duplicate]

I am trying to build my own grid view functions - extending on the GridView.
The only thing I cannot solve is how to get the current scroll position of the GridView.
getScrollY() does always return 0, and the onScrollListener's parameters are just a range of visible child views, not the actual scroll position.
This does not seem very difficult, but I just can't find a solution in the web.
Anybody here who have an idea?
I did not find any good solution,
but this one is at least able to maintain the scroll position kind of pixel-perfectly:
int offset = (int)(<your vertical spacing in dp> * getResources().getDisplayMetrics().density);
int index = mGrid.getFirstVisiblePosition();
final View first = container.getChildAt(0);
if (null != first) {
offset -= first.getTop();
}
// Destroy the position through rotation or whatever here!
mGrid.setSelection(index);
mGrid.scrollBy(0, offset);
By that you can not get an absolute scroll position, but a visible item + displacement pair.
NOTES:
This is meant for API 8+.
You can get with mGrid.getVerticalSpacing() in API 16+.
You can use mGrid.smoothScrollToPositionFromTop(index, offset) in API 11+ instead of the last two lines.
Hope that helps and gives you an idea.
On Gingerbread, GridView getScrollY() works in some situations, and in some doesn't. Here is an alternative based on the first answer. The row height and the number of columns have to be known (and all rows must have equal height):
public int getGridScrollY()
{
int pos, itemY = 0;
View view;
pos = getFirstVisiblePosition();
view = getChildAt(0);
if(view != null)
itemY = view.getTop();
return YFromPos(pos) - itemY;
}
private int YFromPos(int pos)
{
int row = pos / m_numColumns;
if(pos - row * m_numColumns > 0)
++row;
return row * m_rowHeight;
}
The first answer also gives a good clue on how to pixel-scroll a GridView. Here is a generalized solution, which will scroll a GridView equivalent to scrollTo(0, scrollY):
public void scrollGridToY(int scrollY)
{
int row, off, oldOff, oldY, item;
// calc old offset:
oldY = getScrollY(); // getGridScrollY() will not work here
row = oldY / m_rowHeight;
oldOff = oldY - row * m_rowHeight;
// calc new offset and item:
row = scrollY / m_rowHeight;
off = scrollY - row * m_rowHeight;
item = row * m_numColumns;
setSelection(item);
scrollBy(0, off - oldOff);
}
The functions are implemented inside a subclassed GridView, but they can be easily recoded as external.

How to get scroll position from GridView?

I am trying to build my own grid view functions - extending on the GridView.
The only thing I cannot solve is how to get the current scroll position of the GridView.
getScrollY() does always return 0, and the onScrollListener's parameters are just a range of visible child views, not the actual scroll position.
This does not seem very difficult, but I just can't find a solution in the web.
Anybody here who have an idea?
I did not find any good solution,
but this one is at least able to maintain the scroll position kind of pixel-perfectly:
int offset = (int)(<your vertical spacing in dp> * getResources().getDisplayMetrics().density);
int index = mGrid.getFirstVisiblePosition();
final View first = container.getChildAt(0);
if (null != first) {
offset -= first.getTop();
}
// Destroy the position through rotation or whatever here!
mGrid.setSelection(index);
mGrid.scrollBy(0, offset);
By that you can not get an absolute scroll position, but a visible item + displacement pair.
NOTES:
This is meant for API 8+.
You can get with mGrid.getVerticalSpacing() in API 16+.
You can use mGrid.smoothScrollToPositionFromTop(index, offset) in API 11+ instead of the last two lines.
Hope that helps and gives you an idea.
On Gingerbread, GridView getScrollY() works in some situations, and in some doesn't. Here is an alternative based on the first answer. The row height and the number of columns have to be known (and all rows must have equal height):
public int getGridScrollY()
{
int pos, itemY = 0;
View view;
pos = getFirstVisiblePosition();
view = getChildAt(0);
if(view != null)
itemY = view.getTop();
return YFromPos(pos) - itemY;
}
private int YFromPos(int pos)
{
int row = pos / m_numColumns;
if(pos - row * m_numColumns > 0)
++row;
return row * m_rowHeight;
}
The first answer also gives a good clue on how to pixel-scroll a GridView. Here is a generalized solution, which will scroll a GridView equivalent to scrollTo(0, scrollY):
public void scrollGridToY(int scrollY)
{
int row, off, oldOff, oldY, item;
// calc old offset:
oldY = getScrollY(); // getGridScrollY() will not work here
row = oldY / m_rowHeight;
oldOff = oldY - row * m_rowHeight;
// calc new offset and item:
row = scrollY / m_rowHeight;
off = scrollY - row * m_rowHeight;
item = row * m_numColumns;
setSelection(item);
scrollBy(0, off - oldOff);
}
The functions are implemented inside a subclassed GridView, but they can be easily recoded as external.

Categories

Resources