Android 在 HorizontalScrollView 上滚动
我有一个 HorizontalScrollView ,里面有一个 LinearLayout 。 此 LinearLayout(动态)填充有大量 TextView。 我想根据某些索引将 HorizontalScrollView 滚动到指定的 TextView。 我尝试这样做,但不起作用
while (!this.stopsCursor.isAfterLast()) {
int index = this.stopsCursor.getInt(this.stopsCursor.getColumnIndex("index"));
if (index == session.getServiceStopIndex()) {
TextView view = (TextView) this.linearLayout.getChildAt(index);
StyleHelper.setStopHighlight(view);
Log.v("TEST", "Left : " + view.getLeft() + " Right : " + view.getRight());
int offsetX = ((view.getLeft() + view.getRight()) / 2);
this.horizontalScrollView.scrollTo(offsetX, 0);
}
this.stopsCursor.moveToNext();
}
我的记录器显示左:0 右:0 有人可以帮助我吗? 提前致谢!
I have a HorizontalScrollView with a LinearLayout inside.
This LinearLayout is populated (dynamically) with a lot of TextViews.
I want to scroll my HorizontalScrollView to a specified TextView depending on some index.
I try this but doesn't work
while (!this.stopsCursor.isAfterLast()) {
int index = this.stopsCursor.getInt(this.stopsCursor.getColumnIndex("index"));
if (index == session.getServiceStopIndex()) {
TextView view = (TextView) this.linearLayout.getChildAt(index);
StyleHelper.setStopHighlight(view);
Log.v("TEST", "Left : " + view.getLeft() + " Right : " + view.getRight());
int offsetX = ((view.getLeft() + view.getRight()) / 2);
this.horizontalScrollView.scrollTo(offsetX, 0);
}
this.stopsCursor.moveToNext();
}
My logger show me Left : 0 Right : 0
Can someone help me?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能是您试图在布局之前获取视图的左侧和右侧。您需要稍后在布局流程中移动此代码。如果没有更多关于何时滚动的上下文,很难准确说出将其移动到哪里,但是如果它是在创建后立即移动(并且我假设您正在 Activity 的 onCreate 周围的某个位置填充 LinearLayout)那么你可能想看看覆盖
View.onSizeChanged
然后就这样做了。The problem is probably that you are attempting to get the left and right of the view before layout. You need to move this code later in the layout flow. It's hard to say exactly where to move it without more context as to when you want to scroll, but if it's immediately after creation (and I assume that you're populating the LinearLayout somewhere around the Activity's
onCreate
) then you might want to look at overridingView.onSizeChanged
and doing it around then.