为什么 getLocationOnScreen(location) 总是返回 0?

dar*_*h f 3 android view nestedscrollview

在我的FragmentLayoutLinearLayout有多个子视图(TextViewCardView)。我想找到所有LinearLayout视图的顶部偏移量,但我总是得到零。
这是我的代码:

@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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的布局 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>
Run Code Online (Sandbox Code Playgroud)

Neo*_*Neo 5

编写函数以在onWindowFocusChanged(boolean hasFocus)方法中查找位置或以 100 毫秒的延迟将任务发布到处理程序上。