Android 在 Horizo​​ntalScrollView 上滚动

发布于 2024-12-19 19:47:04 字数 797 浏览 0 评论 0原文

我有一个 Horizo​​ntalScrollView ,里面有一个 LinearLayout 。 此 LinearLayout(动态)填充有大量 TextView。 我想根据某些索引将 Horizo​​ntalScrollView 滚动到指定的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅人 2024-12-26 19:47:04

问题可能是您试图在布局之前获取视图的左侧和右侧。您需要稍后在布局流程中移动此代码。如果没有更多关于何时滚动的上下文,很难准确说出将其移动到哪里,但是如果它是在创建后立即移动(并且我假设您正在 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 overriding View.onSizeChanged and doing it around then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文