2016-05-16 2 views
4

Я использую Horizontal Scroll View, содержащий Relative Layout с 6 edittext в нем .Все edittext отображаются горизонтально в видуВыпуск горизонтальной Scrollview автоматически прокручивается на 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 и изменения постионом 0 в onLayout перекрытого метода

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) Я также попытаться дать направление 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); 


     } 

Перед Фокус

enter image description here

После фокусировки на редактирования текста 1 (или 2,3 или любой другой EditText .. Горизонтальная прокрутка перемещается к последнему автоматически.)

enter image description here

Но все же горизонтальная прокрутка в режиме автоматического прокрутки до последней позиции. Как я могу заставить его остановиться?

+0

Попробуйте http://developer.android.com/intl/ru/guide/topics/resources/layout-resource.html#requestfocus-element на первый EditText –

+0

я извиняюсь @PetrovDmitrii. Я не понимаю. Что я должен попробовать? включить его из antoher view или requestfocus tag? а также мой вопрос не связан только с одним редактором, но для всех редакторов. какой бы ни был edittext, вы нажали горизонтальные прокрутки прокрутки до конечной позиции. – KDeogharkar

+0

'android: descendantFocusability =" blocksDescendants "' Попробуйте добавить это в относительный макет. – Beena

ответ

5

Я могу решить мою проблему здесь, но все еще не знаю, почему это происходит.

Я удалить все свойства editext, которые определены и добавил, что все свойства по одному и проверить, какие свойства вызывают проблемы и я узнал, что, когда я добавить gravity="center" свойства EditText снова горизонтальный вид прокрутки прокручивается до конца.

Так что это происходит из-за силы тяжести = "center" свойства edittext.

Но все же я не знаю, почему гравитация, которая имеет edittext, прямо влияет на странное поведение горизонтальной прокрутки. гравитация каким-то образом связана с представлением содержимого родительского контейнера?

Если у кого-либо есть ответ на этот вопрос, пожалуйста, поделитесь своими взглядами.

+1

Не только у вас есть много людей, включая меня, которые пострадали от этой проблемы ** gravity = "center" **. Мои 'adjustPan' и' adjustResize' работали ** только один раз ** из-за этого .. !! –

+0

О, это странно. Может быть, это ошибка андроида, или, может быть, есть еще одна вещь, которую нам не хватает с этим свойством. @jankigadhiya – KDeogharkar

+0

возможно .. Но пока у нас нет другого решения. Мы можем просто прекратить использовать это. !! –