2016-06-13 2 views
2

FragmentLayoutLinearLayout과 여러 개의 하위보기 (TextView, CardView)가 있습니다. 나는 모든 LinearLayout 뷰의 상단 오프셋을 찾고 싶지만 항상 0이됩니다.
이 내 코드입니다 :getLocationOnScreen (location)이 항상 0을 반환하는 이유는 무엇입니까?

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

그리고 이것은 내 레이아웃 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> 

답변

1

onWindowFocusChanged(boolean hasFocus) 방법으로 위치를 찾거나 100 밀리 초 지연 처리기에 작업을 게시하는 기능을 작성합니다.

+0

고맙습니다. –

+0

나는 액티비티 또는 프래그먼트보다 크기가 큽니다. 어떻게해야합니까? – suulisin

+0

새로운 Handler() .post (... v.getLocationOnScreen (location); .....)의 코드를 사용해보십시오. – Neo

관련 문제