简体   繁体   English

如何获得滚动视图的滚动量

[英]how to get amount of scroll for a scrollview

How do I get the amount of pixels which a user has scrolled in a scroll view. 如何获得用户在滚动视图中滚动的像素量。 I have a scroll view which contains lot of texts. 我有一个滚动视图,其中包含很多文本。 I also have a button at bottom of the screen. 屏幕底部还有一个按钮。 What I want is to change text on the button to the amount of scrolling done in the scroll view. 我想要的是将按钮上的文本更改为在滚动视图中完成的滚动量。

this is my scroll view: 这是我的滚动视图:

 <ScrollView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scrollverbal"
    >

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <TextView 
         android:layout_width="match_parent"
    android:layout_height="wrap_content"
     android:id="@+id/verbalintro"
     android:textSize="15sp"
     />

    <View 
        android:layout_width="match_parent"
        android:layout_height="10dp"
    />

    <TextView 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:id="@+id/verqstns"
         android:textSize="15sp"
    />

  </LinearLayout>  

</ScrollView>

and my button is 我的按钮是

 <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="15" 
android:background="@android:color/transparent"
android:textSize="20sp"
android:textStyle="bold"
android:drawablePadding="-5sp"
android:paddingLeft="10sp"
android:drawableLeft="@drawable/tick"
/>

I read that it can be done using getScrollY() method. 我读到它可以使用getScrollY()方法完成。 but don't know how to use it programatically. 但不知道如何以编程方式使用它。

Try this, where scrollX is scrolled pix in x axis and scrollY in Y axis 试试这个,其中scrollX在x轴上滚动pix,在Y轴上滚动pix

scrollX = scrollView.getScrollX();
scrollY = scrollView.getScrollY();

Refer this for doc 参考此文档

this may help you 这可能对您有帮助

https://stackoverflow.com/a/5966375/472336 https://stackoverflow.com/a/5966375/472336

set click listener for your btn findview by id your scroolview and on that call this method and set it as buttons text 设置id scroolview为您的btn findview的点击监听器,并在该方法上调用此方法并将其设置为按钮文本

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

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