简体   繁体   English

在Android上检测scrollview的位置

[英]Detect scrollview's position at android

I'm working on a code at android for applying a form and before completing it, user has to accept a policy. 我正在使用android上的代码来应用表单,并且在完成表单之前,用户必须接受政策。 If i put a TextView inside of ScrollView and a button for accepting, user can accept directly before read. 如果我在ScrollView内放置一个TextView和一个用于接受的按钮,则用户可以在阅读之前直接接受。 But i must force user to scroll to bottom and then make Accept button became enabled and user accepts it then continue. 但是我必须强迫用户滚动到底部,然后使“接受”按钮变为启用状态,然后用户接受它,然后继续。 First i tried this inside of Dialog Window . 首先,我在Dialog Window尝试了此操作。 It's getting much more complicating. 它变得越来越复杂。 Now I'm trying it to inside another activity. 现在,我正在尝试将其添加到另一个活动中。

Is there any way to detect Scroll View 's position has reached to end ? 有什么方法可以检测到Scroll View的位置是否已结束?

You can use the canScrollVertically method with a setOnScrollChangeListener, exemple : 您可以将canScrollVertically方法与setOnScrollChangeListener一起使用,例如:

 scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollView.canScrollVertically(1)) {
                    //user can't scroll more in bottom direction
                }
            }
        });

If the canScrollVertically is reach the user is in the bottom of the scrollView and can't scroll more. 如果可以到达canScrollVertically,则用户位于scrollView的底部,并且无法滚动更多内容。 If you put a negative value like -1 the method detect if the user can scrolling up 如果您输入负值(如-1该方法将检测用户是否可以向上滚动

You can detect it via below code 您可以通过以下代码检测到它

scrollView.getViewTreeObserver()
    .addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() 
{
        @Override
        public void onScrollChanged() {
            if (scrollView.getChildAt(0).getBottom()
                 <= (scrollView.getHeight() + scrollView.getScrollY())) {
                //scroll view is at bottom
            } else {
                //scroll view is not at bottom
            }
        }
    });
  1. Fragments automatically save and restore the states of their Views, as long they have IDs assigned to it. 片段自动分配和保存其视图的状态,只要为其分配了ID。 By Assiging an Id(android:id) to scroll view, do magic for you(scroll state restored) if you are using Fragment 通过分配一个Id(android:id)滚动视图,如果您正在使用Fragment,则可以为您做魔术(恢复滚动状态)

  2. For Activity 活动

    // save index and top position int index = mList.getFirstVisiblePosition(); //保存索引和最高位置int index = mList.getFirstVisiblePosition(); View v = mList.getChildAt(0); 查看v = mList.getChildAt(0); int top = (v == null) ? int top =(v == null)吗? 0 : (v.getTop() - mList.getPaddingTop()); 0:(v.getTop()-mList.getPaddingTop());

// ... // ...

// restore index and position mList.setSelectionFromTop(index, top); //恢复索引和位置mList.setSelectionFromTop(index,top);

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

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