0

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

这意味着所有行(包括标题)的所有编辑文本都可以使用该水平滚动视图滚动。

滚动管理器将确保所有水平滚动视图同时移动。所以它基本上是列表中的一个可滚动列。

我遇到的问题如下。

当我选择 EditText 视图时,它将显示键盘,这就是我想要它做的。但是scrollManager被触发了,所以它会将所有的horizo​​ntalscrollviews滚动到最后。它不会将焦点编辑文本保留在屏幕中,而是会移出视线。

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

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

4

1 回答 1

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;
}  
于 2015-03-05T16:10:07.587 回答