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

发布于 2024-12-07 03:17:57 字数 388 浏览 0 评论 0原文

我正在使用表格布局。其中有 100 个项目使其可滚动我在 ScrollView 中使用 Tablelayout。但我必须检测用户是否已滚动到最后一行。如果用户滚动到最后一个视图,则会向用户显示一条 Toast 消息。 但是如何知道用户已滚动到表格布局的最后一行。我已经引用了 ScrollVie 内的 TableLayoutw 中的代码。

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

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/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

套路撩心 2024-12-14 03:17:57

如果新的滚动 y 位置 + 滚动视图高度 >= 表视图高度,则意味着您已到达列表末尾。

要实现这一点,您必须编写自定义滚动视图。

Step-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 创建一个侦听器来检测scrollchanged

包 com.sunil;
导入LDObservableScrollView;

public interface LDObservableScrollViewListener {

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

}

Step-3 在布局 xml 中而不是 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 在您的活动类或要检测滚动事件的位置使用以下代码

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

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 ::");
        }
    }
纵情客 2024-12-14 03:17:57

这也对我有用,但 onScrollChanged 上的 y 值永远不会高于我的三星 Galaxy Tab 2 7" 上的高度。
但它适用于 Galaxy Tab 2 10.1"
有什么想法吗?

[编辑]
我现在发现,当屏幕小于一定高度(例如 1000px)时,就会发生这种情况。因为当我在 10.1" Galaxy tab 2 上将滚动内容设置为小于 1200 时,它停止了最大高度检测。

[编辑]
我找到了解决方案,它没有正确检测滚动的大小,为了做到这一点,我所做的如下:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文