HorizontalScrollView with EditText's scrolls when keyboard appear

▼魔方 西西 提交于 2019-12-23 04:58:16

问题


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.

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

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

The problem that i am experiencing is as follows.

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.

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

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.


回答1:


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


来源:https://stackoverflow.com/questions/28879111/horizontalscrollview-with-edittexts-scrolls-when-keyboard-appear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!