android - 键盘出现时水平滚动视图总是滚动到结束
问题描述
我有 3 个HorizontalScrollview
,对于它们中的每一个,我都customView
包含editText
我根据 Web 服务响应动态添加的内容。问题是,当我想更改值editText
并出现键盘时,会HorizontalScrollView
自动滚动到最后,所以我无法正确输入值到我的编辑文本中。我的问题是如何防止我的自动滚动,HorizontalScrollView
以便我可以在我的editText
? 并且HorizontalScrollView
当用户滚动它时唯一的滚动。
我已经搜索并且我的问题与此线程类似,但建议没有奏效。这是我已经尝试过的解决方法:
- 添加
android:windowSoftInputMode="stateHidden|adjustNothing"
或android:windowSoftInputMode="stateHidden|adjustResize"
已经尝试adjustPan
和其他组合,但它没有任何影响。 添加到我
android:descendantFocusability="beforeDescendants" android:focusable="true" android:focusableInTouchMode="true"
的LinearLayout
里面HorizontalScrollView
,它也没什么影响。添加
android:descendantFocusability="blocksDescendants
没有HorizontalScrollView
自动滚动但我无法更改我的编辑文本的值,当我添加android:focusable = "false"
我的editText
在下面添加代码但没有任何影响。
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>
所有的建议将不胜感激。提前致谢。
解决方案
推荐阅读
- c++ - 如何使用 STL 获取结构向量并将其转换为结构元素的向量
- javascript - 以最小值为条件的mongodb查询
- swift - 取消 OperationQueue Swift
- asp.net-core - 如果我在 Controller 中使用 IService 并按 IService->Service->IRepository->Repository 的顺序连接 Iservice,如何添加范围
- python-3.x - 在 for 循环中添加熊猫附加问题
- html - 导航栏左侧的间隙
- javascript - 获取数据后设置状态的问题(React 挂钩)
- regex - 在正则表达式上需要帮助以进行多次替换
- corpus - 我在哪里可以获得 Quanteda 网站上的示例中使用的 data_corpus_irishbudget2010?
- netbeans - Netbeans 项目部署到 TomEE 间歇性 FileNotFoundException