Android RecyclerView Edittext问题 [英] Android RecyclerView Edittext issue

查看:617
本文介绍了Android RecyclerView Edittext问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个可编辑项的列表(从后端接收).我正在为此使用新" recyclerview.我的recyclerview中有几种可能的viewType:

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

我遇到的问题是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然后显示键盘.您将必须知道包含EditTextViewHolder的位置:

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问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆