0

I have already look on some forums but the tricks do not work for me.

I have this code:

scrollview = (ScrollView)v.findViewById(R.id.scrollView);
    scrollview.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {

        @Override
        public void onScrollChanged() {

            Log.d(MainActivity.TAG, "scroll ch: "+scrollview.getScrollY()+"/"+scrollview.getChildAt(0).getHeight());
        }
    });

In my layout.xml

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:id="@+id/principal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@drawable/horiz"
        android:orientation="vertical"
        android:showDividers="end|middle" >
    </LinearLayout>
</ScrollView>

When I scroll down to the last I get the getScrollY() smaller than getChildAt(0).getHeight().

How can I get both having the same total size ?

user3502626
  • 838
  • 11
  • 34

2 Answers2

7

Just to formally answer this, since I ran into the same issue.

If you log: scrollView.getScrollY(); and scroll down to the bottom of your view you'll get a number that represents the height of your ScrollView. You'll notice that scrollView.getHeight() only gives you the height of the currently visible view. So you want to use: scrollView.getChildAt(0).getHeight(); as seen here. But that gives you a number greater than the maximum value of scrollView.getScrollY(). So you need to subtract it by the height of the scrollView.

TLDR:

totalHeightOfScrollView = scrollView.getChildAt(0).getHeight() 
    - scrollView.getHeight();

This will equal scrollView.getScrollY() at the bottom-most point.

Community
  • 1
  • 1
Ali Hirani
  • 191
  • 1
  • 8
  • Ok. but a better way to know if the Listview has reached the latest view or his end, you can try `listview.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { Log.d(getClass().getSimpleName(), "total now:"+(firstVisibleItem+visibleItemCount)+"/"+totalItemCount); } });` – user3502626 Jun 16 '16 at 23:02
  • I believe that's a different question, perhaps that was your originally intended question, but I was just outlining your own answer (in the form of a comment) to your original question. – Ali Hirani Jun 17 '16 at 19:39
0

Maybe change

layout_height="wrap_content"

to

layout_height="match_parent"
Patricia
  • 2,885
  • 2
  • 26
  • 32