How to get last scroll view position, scrollview

bapi picture bapi · Sep 30, 2011 · Viewed 14.8k times · Source

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/

Answer

Sunil Kumar Sahoo picture Sunil Kumar Sahoo · Oct 15, 2011

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 ::");
        }
    }