出现键盘时,带有EditText的滚动条的Horizo​​ntalScrollView [英] HorizontalScrollView with EditText's scrolls when keyboard appear

本文介绍了出现键盘时,带有EditText的滚动条的Horizo​​ntalScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功创建了一个包含多个动态元素/行的列表(LinearLayout).它充满了webservice接收到的内容. 每行元素之一是Horizo​​ntalScrollView,其中包含可变数量的EditText字段.

I have successfully created a list (LinearLayout) that contains multiple dynamic elements/rows. It is filled with content received by webservice. One of the elements per row is a HorizontalScrollView that contains a variable amount of EditText fields.

这意味着所有行(包括标题)中的所有edittext都可以使用该horizo​​ntalScrollView滚动.

That means that all edittexts from all rows (including a header) can scroll with that horizontalScrollView.

Scrollmanager将确保所有horizo​​ntalScrollviews同时移动. 因此,它基本上是列表中的可滚动列.

A Scrollmanager will make sure that all horizontalScrollviews move simultaneously. So it is basically a scrollable column within a list.

我遇到的问题如下.

当我选择EditText视图时,它将显示键盘,这是我想要的.但是scrollManager被触发,因此它将所有horizo​​ntalscrollviews滚动到末尾.与其将焦点突出的编辑文本保留在屏幕中,不如将其移到视线之外.

When i select a EditText view it will show the keyboard, which is what i want it to do. But the scrollManager is triggered so it will scroll all horizontalscrollviews to the end. Instead of keeping the focussed edittext in screen, it will move out sight.

我的ScrollManager OnScrollChanged

My ScrollManager OnScrollChanged

@Override
    public void onScrollChanged(View sender, int l, int t, int oldl, int oldt) {
        // avoid notifications while scroll bars are being synchronized
        if (isSyncing)
            return;

        isSyncing = true;

        // remember scroll type
        if (l != oldl)
            scrollType = SCROLL_HORIZONTAL;
        else if (t != oldt)
            scrollType = SCROLL_VERTICAL;
        else {
            // not sure why this should happen
            isSyncing = false;
            return;
        }

        // update clients
        for (ScrollNotifier client : clients) {
            View view = (View) client;

            if (view == sender) 
                continue; // don't update sender

            // scroll relevant views only
            // TODO Add support for horizontal ListViews - currently weird things happen when ListView is being scrolled horizontally
            if ((scrollType == SCROLL_HORIZONTAL && view instanceof HorizontalScrollView)
                    || (scrollType == SCROLL_VERTICAL && view instanceof ScrollView)
                    || (scrollType == SCROLL_VERTICAL && view instanceof ListView)) {
                view.scrollTo(l, t);
            }
        }

        isSyncing = false;
    }

最后,我希望键盘出现,并且scrollview能够滚动,但是我想防止键盘出现时的水平滚动事件.

I the end i want the keyboard to appear and the scrollview to be able to scroll, but i want to prevent the horizontal scroll event when the keyboard appears.

推荐答案

我尚未对此进行测试,但是您应该能够通过将OnTouchListener设置为EditText并覆盖这样的OnTouch方法:

I haven't tested this yet, but you should be able to stop propogation of the touch event to the HorizontalScrollView by setting an OnTouchListener to your EditText and overriding the OnTouch method like this:

@Override
public boolean onTouch(View v, MotionEvent event) 
{
    Log.i("OnTouch", "Fired On Touch in " + v.getId());

    // Do this on the down event too so it's not getting fired before up event
    if(event.getAction() == MotionEvent.ACTION_DOWN)
    {
       // Disallow ScrollView to intercept touch events.
       v.getParent().requestDisallowInterceptTouchEvent(true);
    }   

    //only do this on the up event so you're not doing it for down too
    if(event.getAction() == MotionEvent.ACTION_UP)
    {
       // Disallow ScrollView to intercept touch events.
       v.getParent().requestDisallowInterceptTouchEvent(true);
    }   

    //Keep going with the touch event to show the keyboard        
    v.onTouchEvent(event);
    return true;
}  

这篇关于出现键盘时,带有EditText的滚动条的Horizo​​ntalScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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