添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
奋斗的烤土司  ·  jQuery 事件(三) ...·  4 月前    · 
跑龙套的手链  ·  Connect to database ...·  9 月前    · 
光明磊落的镜子  ·  javascript - Why ...·  11 月前    · 
安静的李子  ·  php ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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>
                Try, the code within in new Handler ().post(...... v.getLocationOnScreen(location); .....);
– Neo
                Jun 7, 2017 at 16:19

Your screen is not ready when you call onCreate(). To get all the data of the screen you need to wait until it is fully finished. You can use:

your_main_layout_here.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
                        // Remove the Global observer once it is called
        your_main_layout_here.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        // Your code here, i.e. check the view's location

And then

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.