2016-06-13 219 views
2

在我的FragmentLayout我有一個LinearLayout與多個子視圖(TextViewCardView)。我想查找所有LinearLayout的最大偏移量,但我總是得到零。
這是我的代碼:爲什麼getLocationOnScreen(位置)總是返回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

相關問題