Android RecyclerView Edittext 问题

编程入门 行业动态 更新时间:2024-10-28 02:36:36
本文介绍了Android RecyclerView Edittext 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建一个可编辑项目列表(从后端接收).为此,我正在使用新"回收器视图.我的回收站视图中有几个可能的视图类型:

I'm creating a list of editable items (received from backend). I'm using the "new" recyclerview for this. There a couple of possible viewTypes in my recyclerview:

  • 复选框
  • 微调器
  • 编辑文本

我遇到的问题是 EditText 获得焦点.AdjustResize 正常工作,并显示了我的键盘.但是获得焦点的 EditText 在列表中不再可见.(现在调整大小部分的列表中的位置低于可见位置).在这种情况下,我不想使用 AdjustPan,因为在顶部有一个寻呼机 &我想在那里固定的东西..

The problem I'm having is with the EditText gaining focus. AdjustResize kicks in fine and my keyboard is shown. But the EditText that has gained focus isn't visible anymore in the list. (position in the list in the now resized portion is below the visible positions). I don't want to be using AdjustPan in this case because on top there is a pager & stuff I would like to keep fixed there..

推荐答案

发生这种情况是因为在显示键盘时,您的 EditText 已不存在.我曾多次尝试寻找解决此问题的干净解决方案,以保持 adjustResize 输入模式和良好的用户体验.这是我最后的结果:

This is happening because, at the time the keyboard is shown, your EditText does not exist anymore. I have made several attempts to find a clean solution to this problem maintaining adjustResize input mode and good user experience. Here's what I ended up with:

要确保在键盘显示之前 EditText 可见,您必须手动滚动 RecyclerView,然后聚焦您的 EditText.您可以通过在代码中覆盖焦点和点击处理来实现.

To ensure that EditText is visible before the keyboard is shown, you will have to scroll the RecyclerView manually and then focus your EditText. You can do so by overriding focus and click handling in your code.

首先,通过将 android:focusableInTouchMode 属性设置为 false 来禁用对 EditText 的关注:

Firstly, disable focus on the EditText by setting android:focusableInTouchMode property to false:

<EditText android:id="@+id/et_review" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusableInTouchMode="false"/>

然后您必须为 EditText 设置一个点击侦听器,您将在其中手动滚动 RecyclerView,然后显示键盘.您必须知道包含 EditText 的 ViewHolder 的位置:

Then you have to set a click listener for the EditText where you will manually scroll the RecyclerView and then show the keyboard. You will have to know the position of your ViewHolder which contains the 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 在自然失去焦点时不会再次获得焦点:

And finally, you must make sure that the EditText is made not focusable again when it naturally looses focus:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { editText.setFocusableInTouchMode(false); UiUtils.hideKeyboard(); } } });

这远不是一个简单而干净的解决方案,但希望它能帮助我.

This is far from a simple and clean solution, but hope it will help the way it helped me.

更多推荐

Android RecyclerView Edittext 问题

本文发布于:2023-10-08 08:45:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472108.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   RecyclerView   Edittext

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!