为什么 getLocationOnScreen(location) 总是返回 0? [英] Why getLocationOnScreen(location) always returns 0?
本文介绍了为什么 getLocationOnScreen(location) 总是返回 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的 FragmentLayout
中,我有一个带有多个子视图(TextView
、CardView
)的 LinearLayout
.我想找到所有 LinearLayout
视图的顶部偏移量,但我总是得到零.
这是我的代码:
@Override公共视图 onCreateView(LayoutInflater inflater,ViewGroup 容器,Bundle savedInstanceState) {view = inflater.inflate(R.layout.fragment_detail, container, false);NestedScrollView = (NestedScrollView) view.findViewById(R.id.nestedScrollView);linearLayout = (LinearLayout) view.findViewById(R.id.lll);int childCount = linearLayout.getChildCount();int[] location = new int[2];for (int i = 0; i < childCount; i++) {视图 v = linearLayout.getChildAt(i);v.getLocationOnScreen(location);Log.e("locX", location[0] + "");Log.e("locY", location[1] + "");}返回视图;}
这是我的布局 xml :
<android.support.design.widget.CoordinatorLayout><android.support.design.widget.AppBarLayout><android.support.design.widget.CollapsingToolbarLayout></android.support.design.widget.CollapsingToolbarLayout></android.support.design.widget.AppBarLayout><android.support.v4.widget.NestedScrollViewandroid:id="@+id/nestedScrollView"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior"><LinearLayout android:id="@+id/lll">//有视图</LinearLayout></android.support.v4.widget.NestedScrollView></android.support.design.widget.CoordinatorLayout></LinearLayout>
解决方案
在 onWindowFocusChanged(boolean hasFocus)
方法中编写查找位置的函数或将任务发布到处理程序上,延迟 100 毫秒.>
In my FragmentLayout
I have a LinearLayout
with multiple subviews (TextView
, CardView
). I want to find the top offset of all LinearLayout
views but I always get zero.
This is my code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_detail, container, false);
nestedScrollView = (NestedScrollView) view.findViewById(R.id.nestedScrollView);
linearLayout = (LinearLayout) view.findViewById(R.id.lll);
int childCount = linearLayout.getChildCount();
int[] location = new int[2];
for (int i = 0; i < childCount; i++) {
View v = linearLayout.getChildAt(i);
v.getLocationOnScreen(location);
Log.e("locX", location[0] + "");
Log.e("locY", location[1] + "");
}
return view;
}
And this is my layout xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout>
<android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout android:id="@+id/lll">
// views are there
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
解决方案
Write the function to find location in onWindowFocusChanged(boolean hasFocus)
method or post the task on handler with 100 millisecond delay.
这篇关于为什么 getLocationOnScreen(location) 总是返回 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文