[英]Issue of horizontal scrollview automatically scrolls on Edittext focus
我正在使用包含Relative Layout
Horizontal Scroll View
其中包含6個edittext
。所有edittext
都在視圖中水平顯示
MyXml.xml
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/horizontalView">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<!--android:weightSum="6"-->
<android.support.v7.widget.CardView
android:id="@+id/sv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:imeOptions="actionNext"
android:ems="3"
android:inputType="number"
android:maxLength="2"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv1"
android:id="@+id/sv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="4"
android:imeOptions="actionNext"
android:inputType="textCapCharacters"
android:maxLength="1"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv2"
android:id="@+id/sv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="4"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="2"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv3"
android:id="@+id/sv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="3"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="4"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv4"
android:id="@+id/sv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="5"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="1"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv5"
android:id="@+id/sv6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="5"
android:inputType="number"
android:maxLength="3"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</HorizontalScrollView>
問題 :現在每當我點擊第一個Edittext
(或任何edittext)時。 horizontal scrollview
自動滾動到結束位置。 我不知道這種行為。 為什么水平滾動視圖會自動滾動?
我試過了什么
1)我試圖在第一次編輯文本焦點更改時將smoothscroll
調用到0,0位置。
edt1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if(b){
hv1.smoothScrollTo(0,0)
}
}
});
2)我還嘗試創建一個custom class
擴展horizontal scrollview
並在onLayout
覆蓋方法中將onLayout
更改為0
public class HorizontalCustomScrollView extends android.widget.HorizontalScrollView {
private boolean enableScrolling = true;
public boolean isEnableScrolling() {
return enableScrolling;
}
public void setEnableScrolling(boolean enableScrolling) {
this.enableScrolling = enableScrolling;
}
public HorizontalCustomScrollView(Context context) {
super(context);
}
public HorizontalCustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HorizontalCustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public HorizontalCustomScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onLayout (boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
this.scrollTo(0,0);
}
}
3)我也嘗試在onLayout
覆蓋方法中onLayout
fullScroll
到FOCUS_LEFT
以及FOCUS_RIGHT
, onLayout
也沒有用。
@Override
protected void onLayout (boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
//this.scrollTo(0,0);
this.fullScroll(HorizontalScrollView.FOCUS_LEFT);
//and also try with Focus right
//this.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
聚焦之前
聚焦編輯文本1(或2,3或任何其他編輯文本后...水平滾動移動到自動上次。)
但仍然水平滾動視圖自動滾動到最后位置。 我怎么能讓它停下來?
我能在這里解決我的問題,但仍然不知道為什么會這樣。
我刪除了定義的editext的所有屬性,並逐個添加了所有屬性,並檢查導致問題的屬性, 我發現當我再次將gravity="center"
屬性添加到edittext時,水平滾動視圖滾動到結束。
所以它正在發生因為gravitytext = edittext的“center”屬性。
但我仍然不知道為什么edittext的引力直接影響水平滾動視圖奇怪的行為。 重力是否與父容器的內容視圖有某種關系?
如果有人對此行為有回答,請分享您的觀點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.