发生这种情况的原因是,在显示键盘时,您的EditText
不再存在。我已经做了几次尝试,以找到一个干净的解决方案,维护adjustResize
输入模式和良好的用户体验。这是我结束了:
为了确保EditText
可见显示键盘之前,你必须手动滚动RecyclerView
,然后集中你EditText
。 您可以通过在代码中重写焦点和点击处理来实现。
首先,通过android:focusableInTouchMode
属性设置为false
禁用重点EditText
:
<EditText
android:id="@+id/et_review"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"/>
然后,你必须设定一个点击监听器为EditText
,您将手动滚动RecyclerView
,然后显示键盘。你必须知道你的ViewHolder
的含有EditText
位置:
editText.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
// you may want to play with the offset parameter
layoutManager.scrollToPositionWithOffset(position, 0);
editText.setFocusableInTouchMode(true);
editText.post(() -> {
editText.requestFocus();
UiUtils.showKeyboard(editText);
});
}
});
最后,你必须确保在EditText
由不可作为焦点时再次就自然失去重点:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
editText.setFocusableInTouchMode(false);
UiUtils.hideKeyboard();
}
}
});
这远不是一个简单而干净的解决方案,但希望它能帮助它帮助我。
我还没有完全熟悉'LinearLayoutManager',所以我把它作为评论,而不是回答。不确定这是否奏效。查看[源代码](https://github.com/android/platform_frameworks_support/blob/46f5ea64c398ae336c85f3aa3a3d9f36398c3576/v7/recyclerview/src/android/support/v7/widget/LinearLayoutManager.java),LLM选择一个子视图作为锚定其滚动位置。如果您阅读'updateAnchorFromChildren(..)'方法,那么将焦点设置在EditText的父视图(无论视图是ViewHolder的实际'itemView')应该使该项目成为滚动锚点。 – Barend
任何解决方案? –
您是否找到解决方案? – Andrea