【问题标题】:Android - HorizontalScrollView - get view in focus (or index of it)Android - Horizo​​ntalScrollView - 获得焦点视图(或它的索引)
【发布时间】:2012-04-27 05:20:16
【问题描述】:

我有一个HorizontalScrollView,其中包含一个LinearLayout,而后者又包含几个FrameLayouts。每个FrameLayout 都包含Fragment 的布局。现在我正在尝试获取视图,即FrameLayout,它当前在HorizontalScrollView 内可见或处于焦点位置。一旦我获得焦点视图,我就可以在LinearLayout 中获得它的索引。

我尝试了以下方法,但没有任何效果:

  • Horizo​​ntalScrollView.findFocus() - 返回 null
  • LinearLayout.findFocus() - 返回 null
  • LinearLayout.getFocusedChild() - 返回 null
  • FrameLayout.hasFocus() - 为所有 FrameLayouts 返回 false

我还可以通过基于每个孩子的当前 X 位置进行计算来尝试找出 LinearLayout 的哪个孩子有焦点。但是,对每个孩子调用 getX() 总是返回“0.0”。

有没有人知道如何让视图成为焦点(或者更好的是它在LinearLayout 中的索引)?

【问题讨论】:

  • 您介意与我们分享您为什么要这样做吗?我只是好奇。
  • 我需要跟踪哪些片段是可见的以用于记录目的

标签: android android-layout android-scrollview android-scroll


【解决方案1】:

使用这个问题的答案: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;
    }
}

【讨论】:

  • 如何获取当前对滚动条视图可见的子视图计数?
  • 这里有什么解决方案/建议吗?
  • 已经很久没有在 Android 上做任何事情了,但是如果你在上面的 if 语句中增加一个计数器应该可以解决问题
  • 很好的解决方案。它对我有帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-30
相关资源
最近更新 更多