首页 > 解决方案 > 键盘出现时水平滚动视图总是滚动到结束

问题描述

我有 3 个HorizontalScrollview,对于它们中的每一个,我都customView包含editText我根据 Web 服务响应动态添加的内容。问题是,当我想更改值editText并出现键盘时,会HorizontalScrollView自动滚动到最后,所以我无法正确输入值到我的编辑文本中。我的问题是如何防止我的自动滚动,HorizontalScrollView以便我可以在我的editText? 并且HorizontalScrollView当用户滚动它时唯一的滚动。

我已经搜索并且我的问题与此线程类似,但建议没有奏效。这是我已经尝试过的解决方法:

  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="blocksDescendants没有 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>

所有的建议将不胜感激。提前致谢。

标签: androidandroid-edittextandroid-softkeyboardhorizontalscrollviewautoscroll

解决方案


推荐阅读