2016-06-13 12 views
2

In meinem FragmentLayout habe ich eine LinearLayout mit mehreren Unteransichten (TextView, CardView). Ich möchte den oberen Offset aller LinearLayout Ansichten finden, aber ich bekomme immer Null.
Dies ist mein Code:Warum gibt getLocationOnScreen (location) immer 0 zurück?

@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; 
} 

Und das ist mein 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> 

Antwort

1

die Funktion schreiben Lage in onWindowFocusChanged(boolean hasFocus) Verfahren oder per Post die Aufgabe auf Handler mit Verzögerung von 100 Millisekunden zu finden.

+0

vielen dank –

+0

ich bin über eine Aktivität oder ein Fragment. Wie mache ich das – suulisin

+0

Versuchen Sie, den Code innerhalb in neuen Handler() .post (...... v.getLocationOnScreen (Standort); .....); – Neo