Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last scroll view position, scrollview

I am using TableLayout. which have 100 of items to make it scrollable I am using Tablelayout inside ScrollView. But I have to detect whether the user have scrolled to the last row. If the user have scrolled to the last view then user will be shown a Toast message. But How to know that the user has scrolled to the last row of the tablelayout. I have referred the code from TableLayout inside ScrollView.

http://huuah.com/using-tablelayout-on-android/

like image 376
bapi Avatar asked Sep 30 '11 10:09

bapi


2 Answers

If new scrolled y position + scroll view height >= tableview height that means you have reached the end of list.

To achieve this you have to write your custiom scrollview.

Step-1 You have to create custom scroll view extending ScrollView

package com.sunil;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class LDObservableScrollView extends ScrollView {

    private LDObservableScrollViewListener scrollViewListener = null;

    public LDObservableScrollView(Context context) {
        super(context);
    }

    public LDObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public LDObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScrollViewListener(LDObservableScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if(scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }

}

Step-2 Create a Listener to detect scrollchanged

package com.sunil; import LDObservableScrollView;

public interface LDObservableScrollViewListener {

    void onScrollChanged(LDObservableScrollView scrollView, int x, int y, int oldx, int oldy);

}

Step-3 in layout xml instead of ScrollView use the custom scroll view

<com.abc.LDObservableScrollView android:layout_width="fill_parent" android:layout_marginTop="1dip"
        android:id="@+id/OLF_ScrollView" android:layout_height="fill_parent" android:background="@drawable/small_list_back">
        <TableLayout android:layout_width="fill_parent" android:id="@+id/OLF_tableLayout_TableLayout" android:layout_height="fill_parent">
        </TableLayout>
    </com.abc.LDObservableScrollView>

Step-4 In your activity class or where you want to detect the scroll event use the follwoing code

public class DetectHere implements LDObservableScrollViewListener{
...


LDObservableScrollView scrollView = (LDObservableScrollView)view.findViewById(R.id.OLF_ScrollView);
         scrollView.setScrollViewListener(this);

.....

@Override
    public void onScrollChanged(LDObservableScrollView scrollView, int x,
            int y, int oldx, int oldy) {
        // TODO Auto-generated method stub
        if((scrollView.getHeight()+y) >= tableLayout.getHeight()){
            Log.e("Custom Listener ", "END OF LIST OCCURRED ::");
        }
    }
like image 186
Sunil Kumar Sahoo Avatar answered Sep 29 '22 00:09

Sunil Kumar Sahoo


This also worked for me, but my y value on the onScrollChanged never gets higher than the height on my samsung galaxy tab 2 7". But it works on the galaxy tab 2 10.1" Any ideas why?

[Edit] I've seen now that this occurs when the screen is less than a certain height, 1000px for instance. Because when I made the content of my scroll less than 1200 on the 10.1" Galaxy tab 2, it stopped working for the maximum height detection.

[Edit] I've found the solution, it wasn't detecting the size of the scroll correctly, in order to do that, what I've done was the following:

protected void onScrollChanged(int horizontalScrollPosition, int verticalScrollPosition, int previousHorizontalScrollPosition, int previousVerticalScrollPosition) {
    int scrollTotalHeight = this.getChildAt(0).getHeight() - super.getHeight();
    if(_previousImage != null)
        if(verticalScrollPosition <= 0) _previousImage.setVisibility(View.GONE);
    if(_nextImage != null)
        if(verticalScrollPosition >= scrollTotalHeight) _nextImage.setVisibility(View.GONE);
    super.onScrollChanged(horizontalScrollPosition, verticalScrollPosition, previousHorizontalScrollPosition, previousVerticalScrollPosition);
}
like image 43
Edison Santos Avatar answered Sep 29 '22 01:09

Edison Santos