简体   繁体   English

如何获取最后一个滚动视图位置,scrollview

[英]How to get last scroll view position, scrollview

I am using TableLayout. 我正在使用TableLayout。 which have 100 of items to make it scrollable I am using Tablelayout inside ScrollView. 有100个项目可以滚动我在ScrollView中使用Tablelayout。 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. 如果用户已滚动到最后一个视图,则将向用户显示Toast消息。 But How to know that the user has scrolled to the last row of the tablelayout. 但是如何知道用户已滚动到tablelayout的最后一行。 I have referred the code from TableLayout inside ScrollVie w. 我已经在ScrollVie w中引用了TableLayout中的代码。

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

If new scrolled y position + scroll view height >= tableview height that means you have reached the end of list. 如果新滚动的y位置+滚动视图高度> = tableview高度,则表示您已到达列表的末尾。

To achieve this you have to write your custiom scrollview. 要实现这一点,你必须写你的custiom scrollview。

Step-1 You have to create custom scroll view extending ScrollView 步骤1您必须创建扩展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 步骤2创建一个侦听器以检测scrollchanged

package com.sunil; 包com.sunil; import LDObservableScrollView; 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 布局xml中的步骤3而不是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 the scroll event use the follwoing code 步骤4在您的活动类中或您要检测滚动事件的位置使用以下代码

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

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? 这对我也有用,但是onScrollChanged上的y值永远不会高于我的三星Galaxy tab 2 7“的高度。但它适用于星系标签2 10.1”任何想法为什么?

[Edit] I've seen now that this occurs when the screen is less than a certain height, 1000px for instance. [编辑]我现在已经看到,当屏幕小于某个高度时会发生这种情况,例如1000px。 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. 因为当我在10.1“Galaxy选项卡2上使我的卷轴内容小于1200时,它停止工作以进行最大高度检测。

[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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM