所以View.getTop()
是docs:
此视图相对于其父级的顶部位置。
我有以下布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.scrollviewtest1.MainActivity">
<LinearLayout
android:id="@+id/scroll_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.example.scrollviewtest1.MyTextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/large_text" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
</ScrollView>
我正在检查getTop()
LinearLayout
,因为我向上和向下滚动getTop()
始终返回0.不应该随着 移动而改变相对于其父母而言上下? 。
我是如何检查的?
我存储了对子LinearLayout
内的TextView
的引用,并在每次滚动时调用invalidate()
:
ScrollView root = ((ScrollView) findViewById(R.id.root));
root.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
textView.invalidate();
}
});
并在孩子onDraw()
TextView
来电中查看
答案 0 :(得分:0)
尝试这种方式:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int top = view.getTop();
}
}, 300);// delay times in milliseconds
您可以根据您的要求设置延迟时间。
答案 1 :(得分:0)
要获取LinearLayout滚动位置调用myScrollView.getScrollY();
getTop();
会返回您在xml布局中输入为marginTop的内容,并且在滚动内容时不会发生变化,以了解您需要在帐户中getScrollY();
采取的实际当前位置