我怎么完全prevent从滚动一个TextView? [英] How do I completely prevent a TextView from scrolling?

查看:249
本文介绍了我怎么完全prevent从滚动一个TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的文字一个TextView。这TextView中有 MAXLINES 集,因此它只显示第8行左右。我也有一个更多按钮,这样我处理扩大我自己TextView的。

I have a TextView with a lot of text. This TextView has maxLines set, so it only shows the first 8 or so lines. I also have a "Read More" button so I handle expanding the TextView on my own.

我的问题是,有时TextView的滚动一点(只有半在一个时间线),即使我没有指定的滚动条。这个问题变得更加严重,因为TextView的是一个ListView里面,所以当用户滚动为主的ListView,TextView的滚动有时一点点,像这样的:

My problem is that sometimes the TextView scrolls a little (just half a line at a time), even though I never specified any scroll bars. This issue is made worse because the TextView is inside a ListView, so when the user scrolls the main ListView, the TextView sometimes scrolls a little, like this:

我如何prevent TextView的滚动距离?

How do I prevent the TextView from scrolling?

推荐答案

所以,我做了一些研究,我不认为这是像刚才禁用滚动那样简单,但也有一些事情你可以做/尝试。

So I did a little research and I don't think it's as simple as just disabling scrolling, but there are a few things you can do/try.

第一个是的setEnabled(假),但这将禁用链接并改变文字颜色。

The first is setEnabled(false) but this will disable links and alter the text color.

第二,我建议尝试,使用scrollTo(INT X,int y)对方法。只是scrollTo(0,0)设置的TextView的文本之后,我的猜测是大文本是造成滚动所以这应该能照顾它的唯一的事情。

The second, which I suggest trying, is using the scrollTo(int x, int y) method. Just scrollTo(0,0) after setting the text of the TextView, my guess is the large text is the only thing causing the scrolling so this should be able to take care of it.

第三个答案,我发现,你可以尝试有点更复杂,不完全是你的问题,但它可能为你工作可以发现<一个href=\"http://stackoverflow.com/questions/14579785/can-i-disable-the-scrolling-in-textview-when-using-linkmovementmethod\">here.

The third answer I found that you can try is a bit more complicated and not exactly your question but it may work for you can be found here.

public class LinkMovementMethodOverride implements View.OnTouchListener{

@Override
public boolean onTouch(View v, MotionEvent event) {
    TextView widget = (TextView) v;
    Object text = widget.getText();
    if (text instanceof Spanned) {
        Spanned buffer = (Spanned) text;

        int action = event.getAction();

        if (action == MotionEvent.ACTION_UP
                || action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            ClickableSpan[] link = buffer.getSpans(off, off,
                    ClickableSpan.class);

            if (link.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    link[0].onClick(widget);
                } else if (action == MotionEvent.ACTION_DOWN) {                             
                    // Selection only works on Spannable text. In our case setSelection doesn't work on spanned text
                    //Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
                }
                return true;
            }
        }

    }

    return false;
}

}

之后,它应用于目标TextView的触摸监听器: -

"After that apply it to the target textview as touch listener: -

textview.setOnTouchListener(新LinkMovementMethodOverride());

textview.setOnTouchListener(new LinkMovementMethodOverride());"

这篇关于我怎么完全prevent从滚动一个TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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