Android – HorizontalScrollView – 获取焦点视图(或其索引)
作者:互联网
我有一个包含LinearLayout的HorizontalScrollView,而LinearLayout又包含几个FrameLayout.每个FrameLayout都包含片段的布局.现在我正在尝试获取视图,即FrameLayout,它在HorizontalScrollView中当前可见或焦点.只要我有焦点视图,我就可以在LinearLayout中得到它的索引.
我尝试了以下,但没有任何作用:
> HorizontalScrollView.findFocus() – 返回null
> LinearLayout.findFocus() – 返回null
> LinearLayout.getFocusedChild() – 返回null
> FrameLayout.hasFocus() – 为所有FrameLayouts返回false
我还可以尝试通过基于每个孩子的当前X位置进行计算来确定LinearLayout的哪个孩子有焦点.但是,在每个子节点上调用getX()始终返回“0.0”.
有没有人知道如何获得焦点的视图(甚至更好的是它在LinearLayout中的索引)?
解决方法:
使用这个问题的答案: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,android-layout,android-scrollview,android-scroll 来源: https://codeday.me/bug/20190530/1183697.html