如何还原屏幕旋转后的TextView滚动位置? [英] How to restore textview scrolling position after screen rotation?

查看:154
本文介绍了如何还原屏幕旋转后的TextView滚动位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid的布局,我有一个TextView。此TextView中正在显示一个相当大spannable文本,它是能够滚动。现在,当手机旋转时,查看被破坏,创造,我要的setText()TextView的再次重置滚动位置的开头。

In my Android Layout, I have a TextView. This TextView is displaying a rather large spannable text and it is able to scroll. Now when the phone is rotated, the View is destroyed and created and I have to setText() the TextView again, resetting the scroll position to the beginning.

我知道我可以使用getScrolly()和scrollTo()滚动到像素位置,但是由于视图宽度的变化,线条变得更长,一条线是在像素POS机400现在可能在250。因此,这是不是非常有帮助。

I know I can use getScrolly() and scrollTo() to scroll to pixel positions, but due to the change in View widths, lines become longer and a line that was at pixel pos 400 might now be at 250. So this is not very helpful.

我需要一种方法来找到在的onDestroy(一个TextView第一个可见线),然后一个方法,使的TextView滚动到这个特定的一段文字后轮换。

I need a way to find the first visible line in a TextView in onDestroy() and then a way to make the TextView scroll to this specific piece of text after the rotation.

任何想法?

推荐答案

这是一个老问题,但我在这里登陆寻找解决同样的问题的时候,所以这里是我想出了。本人结合思想从回答以下三个问题:

This is an old question, but I landed here when searching for a solution to the same problem, so here is what I came up with. I combined ideas from answers to these three questions:

  • 滚动的TextView文本位置
  • <一个href="http://stackoverflow.com/questions/9096613/dynamically-modifying-contextual-long-$p$pss-menu-in-edittext-based-on-position-o">Dynamically基于长$ P $的位置的EditText修改上下文/庆龙preSS菜单PSS
  • <一个href="http://stackoverflow.com/questions/3263259/scrollview-scrollto-not-working-saving-scrollview-position-on-rotation">ScrollView .scrollTo不工作?保存在旋转滚动型位置
  • Scroll TextView to text position
  • Dynamically Modifying Contextual/Long-Press Menu in EditText Based on Position of Long Press
  • ScrollView .scrollTo not working? Saving ScrollView position on rotation

我想从我的应用程序中提取出相关的code,所以请原谅任何错误。还要注意的是,如果你旋转为横向和背部,它可能不会在你开始相同的位置结束。例如,说彼得是第一个可见的字画像。当旋转为横向,彼得是其行的最后一个字,第一个是拉里。当你旋转回来,拉里将是可见的。

I tried to extract only the relevant code from my app, so please forgive any errors. Also note that if you rotate to landscape and back, it may not end in the same position you started. For example, say "Peter" is the first visible word in portrait. When you rotate to landscape, "Peter" is the last word on its line, and the first is "Larry". When you rotate back, "Larry" will be visible.

private static float scrollSpot;

private ScrollView scrollView;
private TextView textView;

protected void onCreate(Bundle savedInstanceState) {
    textView = new TextView(this);
    textView.setText("Long text here...");
    scrollView = new ScrollView(this);
    scrollView.addView(textView);

    // You may want to wrap this in an if statement that prevents it from
    // running at certain times, such as the first time you launch the 
    // activity with a new intent.
    scrollView.post(new Runnable() {
        public void run() {
            setScrollSpot(scrollSpot);
        }
    });

    // more stuff here, including adding scrollView to your main layout
}

protected void onDestroy() {
    scrollSpot = getScrollSpot();
}

/**
 * @return an encoded float, where the integer portion is the offset of the
 *         first character of the first fully visible line, and the decimal
 *         portion is the percentage of a line that is visible above it.
 */
private float getScrollSpot() {
    int y = scrollView.getScrollY();
    Layout layout = textView.getLayout();
    int topPadding = -layout.getTopPadding();
    if (y <= topPadding) {
        return (float) (topPadding - y) / textView.getLineHeight();
    }

    int line = layout.getLineForVertical(y - 1) + 1;
    int offset = layout.getLineStart(line);
    int above = layout.getLineTop(line) - y;
    return offset + (float) above / textView.getLineHeight();
}

private void setScrollSpot(float spot) {
    int offset = (int) spot;
    int above = (int) ((spot - offset) * textView.getLineHeight());
    Layout layout = textView.getLayout();
    int line = layout.getLineForOffset(offset);
    int y = (line == 0 ? -layout.getTopPadding() : layout.getLineTop(line))
        - above;
    scrollView.scrollTo(0, y);
}

这篇关于如何还原屏幕旋转后的TextView滚动位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆