How to get the last scroll view position, scrollview

I am using TableLayout. which have 100 elements to make it scrollable. I am using Tablelayout inside ScrollView. But I have to determine if the user scrolls to the last line. If the user scrolled to the last view, the Toast message will be displayed to the user. But how do you know that the user has scrolled to the last row of the table. I passed the code from TableLayout inside ScrollVie w.

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

+5
source share
2 answers

If the new scroll position y + scroll view height> = table height, then you have reached the end of the list.

To do this, you need to write your custiom scroll view.

Step-1 You need to create your own scroll that extends 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

com.sunil package; import LDObservableScrollView;

public interface LDObservableScrollViewListener {

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

}

Step-3 in xml layout uses ScrollView instead of ScrollView

<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 a scroll event, use the following 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 ::");
        }
    }
+13
source

, y onScrollChanged , samsung 2 7 ". 2 10.1 " , ?

[] , , , 1000 . , 1200 10.1 "Galaxy Tab 2", .

[] , , , :

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);
}
+1

All Articles