2012-04-15 111 views
4

我有一個HorizontalScrollView包含LinearLayout,其中又包含一對FrameLayout s。每個FrameLayout包含Fragment的佈局。現在我試圖獲得視圖,即FrameLayout,該視圖當前可見或焦點在HorizontalScrollView之內。只要我把焦點放在視圖上,我可以得到它在LinearLayout之內的索引。Android - Horizo​​ntalScrollView - 獲取焦點(或索引)

我試過以下,但沒有任何工程:

  • Horizo​​ntalScrollView.findFocus() - 返回null
  • LinearLayout.findFocus() - 返回null
  • LinearLayout.getFocusedChild() - 返回空
  • FrameLayout.hasFocus() - 返回所有FrameLayout小號

我可以假通過根據每個孩子的當前X位置進行計算,試圖找出LinearLayout中哪個孩子有焦點。但是,每個孩子撥打getX()總是返回「0.0」。

有沒有人有一個想法如何獲得焦點(或甚至更好它的指數內的LinearLayout)?

+0

你不介意與我們分享您爲什麼會想要做這樣的事情?我只是好奇。 – 2012-04-15 18:07:17

+0

我需要跟蹤哪些片段是可見的用於日誌目的 – Schnodahipfe 2012-04-15 19:01:29

回答

6

使用這個問題的答案:android-how-to-check-if-a-view-inside-of-scrollview-is-visible我想出了以下解決方案:

Rect scrollBounds = new Rect(); 
MyHorizontalScrollView.getDrawingRect(scrollBounds); 
Rect childBounds = new Rect();  
for (int i = 0; i < MyLinearLayout.getChildCount(); i++) { 
    MyLinearLayout.getChildAt(i).getHitRect(childBounds); 
    if(scrollBounds.contains(childBounds)) { 
     IndexOfVisibleChild = i; 
     return; 
    } 
} 
+0

如何獲得滾動條視圖當前可見的子視圖的數量? – CoDe 2012-10-03 10:28:40

+0

在這裏的任何解決方案/建議? – CoDe 2013-01-30 10:00:22

+0

在年齡方面沒有對Android做任何事情,但是如果你在上面的if語句中增加了一個計數器應該可以做到這一點 – Schnodahipfe 2013-06-20 13:02:17

相關問題