确定一个观点是在屏幕上 - 安卓 [英] Determine if a view is on screen - Android
问题描述
我有点坚持这一个 - 首先,下面的链接一直有用,但是我已经想出了一个有点问题的,能见度:
I'm a little bit stuck with this one - first and foremost, the following link has been useful however I've come up with a bit of an issue with visibility:
链接:<一href="http://stackoverflow.com/questions/4628800/android-how-to-check-if-a-view-inside-of-scrollview-is-visible"标题=检查视图visbilitiy>检查视图知名度
我有一个滚动视图(父)和若干子视图(的LinearLayout
- > TableLayout
)等有一些项目我设置为 View.GONE
在XML中(的安卓知名度=水涨船高
)
I have a scroll view (parent) and a number of sub-views (LinearLayout
-> TableLayout
) etc. There are a number of items I set to View.GONE
within the XML (android:visibility="gone"
).
我有一些简单的code,以确定它是否是可见或不使用 getVisibility()
但是当我设置项查看。可见
并立即尝试 getDrawingRect()
我得到一个矩形与全线零。任何进一步的点击得到正确的坐标。
I have some simple code to determine whether it is visible or not using getVisibility()
however when I set the item to View.VISIBLE
and try to immediately getDrawingRect()
I get a Rect with zeros across the board. Any further click gets the correct coordinates.
现在,这可能是因为观点从来没有被抽(如XML定义),使其返回没有坐标然而,我设置 View.VISIBLE
之前尝试确定屏幕的可视性。难道说我需要某种形式的回调,从说的OnDraw()?或者设定的范围内code隐藏的项目视图的知名度。有点讨厌;(
Now this could be because the view has never been drawn (as defined in the XML) causing it to return no coordinates however I do set View.VISIBLE
before trying to determine screen visibility. Could it be that I need to get some kind of callback from say the onDraw()? or perhaps set the view visibility of hidden items within code. A bit annoying ;(
有些code:
Rect scrollBounds = new Rect();
scroll.getHitRect(scrollBounds);
Rect viewBounds = new Rect();
if (view.getVisibility() == View.GONE) {
view.setVisibility(View.VISBLE)
viewBounds.getDrawingRect(viewBounds);
if (!Rect.intersects(scrollBounds, viewBounds) {
// do somthing
}
}
布局区域如下:
Layouts area as follows:
-
滚动型
-
的LinearLayout
-
TableLayout
-
按钮
-
HiddenView
ScrollView
LinearLayout
TableLayout
Button
HiddenView
当然,这是极有可能我会对此错误的方式干脆 - 基本上我只是想确保滚动视图自身定位,使已成为可见的视图可以在其中看到的全部
Of course, it's highly likely I'm going about this the wrong way altogether - basically I just want to make sure that the scrollview positions itself so the view that has become visible can be seen in it's entirety.
如果任何其他信息需要,让我知道!
If any other information is required, let me know!
推荐答案
确定,所以感谢OceanLife指着我在正确的方向!有确实需要一个回调和
ViewTreeObserver.OnGlobalLayoutListener()
的伎俩。我最终实现了听众对我的片段类,并把它捡起来,我需要它。谢谢你的提醒过关于多个电话,我解决了这个使用removeOnGlobalLayoutListener()
方法 - 作品魅力Ok so thanks to OceanLife for pointing me in the right direction! There was indeed a callback required and
ViewTreeObserver.OnGlobalLayoutListener()
did the trick. I ended up implementing the listener against my fragment class and picked it up where I needed it. Thanks for the warning too regarding the multiple calls, I resolved this using theremoveOnGlobalLayoutListener()
method - works a charm.code:
... // vto initialised in my onCreateView() method vto = getView().getViewTreeObserver(); vto.addOnGlobalLayoutListener(this); ... @Override public void onGlobalLayout() { final int i[] = new int[2]; final Rect scrollBounds = new Rect(); sView.getHitRect(scrollBounds); tempView.getLocationOnScreen(i); if (i[1] >= scrollBounds.bottom) { sView.post(new Runnable() { @Override public void run() { sView.smoothScrollTo(0, sView.getScrollY() + (i[1] - scrollBounds.bottom)); } }); } vto.removeOnGlobalLayoutListener(this); }
只需要做一些现在清理...
Just got to do some cleaning up now ...
这篇关于确定一个观点是在屏幕上 - 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-
-
-