出现键盘时,水平滚动视图始终滚动到结束

时间:2018-10-23 06:42:34

标签: android android-edittext android-softkeyboard horizontalscrollview autoscroll

我有3个HorizontalScrollview,对于每个他们我都有customView包含editText,我根据网络服务响应动态添加。问题是,当我想更改editText中的值并且出现键盘时,HorizontalScrollView会自动滚动到末尾,因此我无法正确地将值输入到我的编辑文本中。我的问题是如何防止在HorizontalScrollView中自动滚动,以便我可以在editText上输入值?并且HorizontalScrollView仅在用户滚动时滚动。

我已经搜索过,并且我的问题与this thread类似,但是建议无效。这是我已经尝试过的解决方法:

  1. 添加android:windowSoftInputMode="stateHidden|adjustNothing"android:windowSoftInputMode="stateHidden|adjustResize"已经尝试 adjustPan和其他组合,但没有任何效果。
  2. android:descendantFocusability="beforeDescendants" android:focusable="true" android:focusableInTouchMode="true"中的LinearLayout中添加HorizontalScrollView,也没有影响。

  3. 添加android:descendantFocusability="blockDescendants HorizontalScrollView不会自动滚动,但是我无法更改编辑文本的值,这与在android:focusable = "false"中添加editText的情况类似

  4. 在下面添加代码,但没有任何影响。

    scrollLayout.descendantFocusability = ViewGroup.FOCUS_BEFORE_DESCENDANTS scrollLayout.isFocusable = true scrollLayout.isFocusableInTouchMode = true scrollLayout.setOnTouchListener {v,event-> v.requestFocusFromTouch() false}

这是我的代码

private fun setUpList(rootLayout: LinearLayout, scrollLayout: HorizontalScrollView, items: List<ProductItem>) {
    //config horizontal scroll view
    scrollLayout.descendantFocusability = ViewGroup.FOCUS_BEFORE_DESCENDANTS
    scrollLayout.isFocusable = true
    scrollLayout.isFocusableInTouchMode = true
    scrollLayout.setOnTouchListener { v, event ->
        v.requestFocusFromTouch()
        false
    }
    // add customView to linear layout
    rootLayout.removeAllViews()
    for (item in items) {
        val view = EditableThumbnailProduct(context)
        view.setProductInfo(item)
        view.setOnProductChangeListener(onChangeListener())
        rootLayout.setBackgroundColor(Color.TRANSPARENT)
        rootLayout.addView(view)
    }
}

这是我的XML

<HorizontalScrollView
            android:id="@+id/scrollMed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none">

            <LinearLayout
                android:id="@+id/contentMed"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@android:color/transparent"
                android:descendantFocusability="beforeDescendants"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:orientation="horizontal" />
        </HorizontalScrollView>

这是我的自定义视图

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@android:color/transparent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/img_thumbnail"
        android:layout_width="150dp"
        android:layout_height="120dp"
        android:background="@color/gray_border"
        android:scaleType="fitXY" />

    <TextView
        android:id="@+id/txt_product_name"
        style="@style/DefaultTextTitle"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_margin="5dp"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="2"
        android:text="@string/product_name_label" />

    <TextView
        android:id="@+id/txt_product_stock"
        style="@style/DefaultTextSubTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:gravity="center"
        android:text="@string/available_stock_label" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/gray_border" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="vertical">

        <Button
            android:id="@+id/btn_add_to_cart"
            style="@style/WhiteTitle"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_margin="10dp"
            android:background="@drawable/round_button_orange"
            android:gravity="center"
            android:text="@string/buy_button_label"
            android:visibility="invisible" />

        <RelativeLayout
            android:id="@+id/layout_plus_minus"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_margin="10dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/btn_minus"
                style="@style/WhiteTitle"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentLeft="true"
                android:background="@drawable/round_button_blue"
                android:paddingLeft="5dp"
                android:paddingTop="11dp"
                android:paddingRight="5dp"
                android:paddingBottom="11dp"
                android:scaleType="fitXY"
                app:srcCompat="@drawable/ic_minus" />

            <ImageView
                android:id="@+id/btn_delete"
                style="@style/WhiteTitle"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentLeft="true"
                android:background="@drawable/round_button_blue"
                android:padding="3dp"
                android:scaleType="fitXY"
                android:visibility="invisible"
                app:srcCompat="@drawable/ic_delete" />

            <EditText
                android:id="@+id/txt_cart_amount"
                style="@style/LightTextTitle"
                android:layout_width="50dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="40dp"
                android:layout_marginRight="10dp"
                android:backgroundTint="@color/blue_text_color"
                android:inputType="number"
                android:maxLength="3"
                android:paddingBottom="5dp" />

            <ImageView
                android:id="@+id/btn_plus"
                style="@style/WhiteTitle"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentRight="true"
                android:background="@drawable/round_button_blue"
                android:gravity="center"
                android:padding="5dp"
                android:scaleType="fitXY"
                android:textSize="16sp"
                app:srcCompat="@drawable/ic_plus" />

        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>

所有建议将不胜感激。预先感谢。

0 个答案:

没有答案
相关问题