[英]Android - HorizontalScrollView scrolls to the right when keyboard is opened
[英]HorizontalScrollView with EditText's scrolls when keyboard appear
我已經成功創建了一個包含多個動態元素/行的列表(LinearLayout)。 它填充了Web服務接收的內容。 每行元素之一是HorizontalScrollView,其中包含可變數量的EditText字段。
這意味着所有行(包括標題)中的所有edittext都可以使用該horizontalScrollView滾動。
Scrollmanager將確保所有horizontalScrollviews同時移動。 因此,它基本上是列表中的可滾動列。
我遇到的問題如下。
當我選擇EditText視圖時,它將顯示鍵盤,這是我想要的。 但是scrollManager被觸發,因此它將所有horizontalscrollviews滾動到末尾。 與其將聚焦的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;
}
最后,我希望鍵盤出現,並且scrollview能夠滾動,但是我想防止鍵盤出現時的水平滾動事件。
我還沒有對此進行測試,但是您應該可以通過將OnTouchListener設置為EditText並重寫OnTouch方法來停止將touch事件傳播到HorizontalScrollView:
@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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.