当键盘出现时,带有EditText的Horizo​​ntalScrollView会滚动

时间:2015-03-05 13:36:39

标签: android android-softkeyboard horizontalscrollview onscrollchanged synchronized-scrolling

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

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

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

我遇到的问题如下。

当我选择EditText视图时,它将显示键盘,这是我想要它做的。但是scrollManager被触发,因此它会将所有水平滚动视图滚动到最后。它不会将焦点化的edittext保留在屏幕上,而是会移出视线。

我的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;
    }

我最后想要键盘出现并且滚动视图能够滚动,但我想在键盘出现时阻止水平滚动事件。

1 个答案:

答案 0 :(得分:1)

我还没有对此进行过测试,但您应该能够通过将OnTouchListener设置为EditText并覆盖OnTouch方法来阻止触摸事件传播到Horizo​​ntalScrollView:

@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;
}