简体   繁体   English

如何基于同级ViewGroup的孩子之一的位置来定位视图?

[英]How to position view based on position of one of sibling ViewGroup's child?

Ok, the question may sound confusing. 好的,这个问题听起来令人困惑。 Let me explain the scenario: 让我解释一下这种情况:

Refer the image at following link, (-I wasn't able to use imgur as it is blocked in my network): http://www.flickr.com/photos/93397745@N05/8494742266/ 请参阅以下链接中的图像,(-我无法使用imgur,因为它已在我的网络中被阻止): http ://www.flickr.com/photos/93397745@N05/8494742266/

  • I have a CustomView that extends RelativeLayout (entire black part in image) 我有一个CustomView扩展了RelativeLayout(图像中整个黑色部分)
  • This CustomView contains a component at top ViewTopBar (indicated in Red in image), which extends ViewGroup & is horizontally scrollable 此CustomView在顶部的ViewTopBar包含一个组件(图像中用红色表示),该组件扩展了ViewGroup,并且可以水平滚动
  • The ViewTopBar contains many child views. ViewTopBar包含许多子视图。 One of 'em is an IndicatorView (orange one in image), which is positioned in ViewTopBar after doing some calculations & calling layout(l,t,r,b) in ViewTopBar's onLayout() method. 其中之一是一个IndicatorView(图像中为橙色),在进行一些计算并在ViewTopBar的onLayout()方法中调用layout(l,t,r,b)之后,将其放置在ViewTopBar中。
  • I want a vertical 1 pixel line to be displayed below the IndicatorView. 我希望在IndicatorView下显示一条垂直的1像素线。 It should start just below IndicatorView & cover the entire height of CustomView. 它应从IndicatorView的正下方开始,并覆盖CustomView的整个高度。 (See the green colored line in image). (请参阅图像中的绿色线)。
  • This part doesn't matter, but just for information, the blue colored part in image is another Custom ViewGroup which is pretty complex to describe. 这部分无关紧要,但仅作参考之用,图像中的蓝色部分是另一个Custom ViewGroup,描述起来非常复杂。 (Also, there are still a few more components that are part of CustomView but not shown in the image as its not needed for my question.) (此外,CustomView中还有其他一些组件,但未在图像中显示,因为我的问题不需要使用它。)

There, I hope I have the view explained. 在那里,我希望我的观点得到解释。 Now what I need is that the green 1px line should move horizontally so as to stay aligned below IndicatorView whenever horizontal scrolling is done. 现在,我需要的是绿色的1px线应水平移动,以便在完成水平滚动时始终保持在IndicatorView下方。 I've currently implemented it as follows: 我目前已按以下方式实施:

  • Placed the 1px line as child of CustomView. 将1px的行放置为CustomView的子级。
  • Whenever ViewTopBar's onLayout() is called, I update the GreenLine's LayoutParams & just modify the left margin depending on IndicatorView's position. 每当调用ViewTopBar的onLayout()时,我都会更新GreenLine的LayoutParams并仅根据IndicatorView的位置修改左侧边距。

This works fine most of the time. 在大多数情况下,这都可以正常工作。 But sometimes, when I do left-right-left-right scroll continuously, the 1pxLine does not stay in sync with the scrolling IndicatorView (--However, once scrolling stops, it comes back into sync!) So what changes do I need to make so that the line stays in sync even during scroll operations? 但是有时候,当我连续左右滚动时,1pxLine不会与滚动的IndicatorView保持同步(但是,一旦停止滚动,它就会恢复同步!)所以我需要做些什么更改使行即使在滚动操作期间也保持同步?

Please let me know if someone has been able to do this before. 请让我知道之前是否有人可以这样做。

Thanks, 谢谢,

-Kiran -基兰

I'd take the IndicatorView out of ViewTopBar , and together with 1px-line-view I'd include it within some container layout, which I'd put inside CustomView instead. ViewTopBar IndicatorView取出ViewTopBar ,并与1px-line-view一起将其包含在某些容器布局中,该容器布局应放在CustomView That way, you could control position of this element as a whole, instead of making one 'catching-up' with another. 这样,您可以控制整个元素的位置,而不必与另一个元素“追赶”。 Not sure if it's applicable in your case though. 但是不确定是否适用于您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取View在ViewGroup中的位置? - How to get View's position inside ViewGroup? 如何在同级ScrollView中根据滚动位置同步一个视图的内容? - How to synchronize content of one view depending on scroll position in sibling ScrollView? RelativeLayout:将视图与兄弟视图组的子视图对齐 - RelativeLayout: align view to child of sibling viewgroup 从 ViewGroup 中删除视图后如何维护其他视图的位置? - How to maintain Position of other views after removing a view from the ViewGroup? RecyclerView滚动到子视图的子位置 - RecyclerView scroll to position of child view's child 更改同级位置时如何刷新android view的高度? - How to refresh android view height when sibling position changed? 如何在Android View getView中添加条件(int位置,View convertView,ViewGroup父级) - How to add condition in Android View getView (int position, View convertView, ViewGroup parent) 子视图在Horizo​​ntalScrollView中的位置 - Position of a Child View in a HorizontalScrollView 在此示例中,convertView如何更改getView(int位置,View convertView,ViewGroup父级) - how convertView change in getView (int position, View convertView, ViewGroup parent) in this example 如何相对于屏幕放置视图(即不是父视图组)? - How can I position a view relative to the screen (i.e. not the parent ViewGroup)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM