Android RecyclerView Edittext问题
我正在创建一个可编辑项目列表(从后端接收)。 我为此使用了“新”回收站。 在我的recyclerview中有几个可能的viewType:
我遇到的问题是EditText获得焦点。 AdjustResize正常踢,我的键盘显示。 但是获得焦点的EditText在列表中不再可见。 (在现在调整大小的部分列表中的位置低于可见位置)。 我不想在这种情况下使用AdjustPan,因为在顶部有一个寻呼机和我想保持固定的东西..
在布局中使用此工作: android:descendantFocusability =“beforeDescendants”
<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:descendantFocusability="beforeDescendants"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/extrasLayout"
        android:layout_below="@+id/anchorView"
        android:layout_marginTop="@dimen/margin5"
        android:fastScrollEnabled="false"
        />
清单文件:将其添加到活动部分android:windowSoftInputMode =“stateHidden | adjustPan”
<activity
            android:name=".activites.CartActivity"
            android:label="@string/title_activity_cart"
            android:exported="true"
            android:windowSoftInputMode="stateHidden|adjustPan"
            android:parentActivityName=".activites.HomeActivity"
            android:screenOrientation="portrait">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".activites.HomeActivity"/>
        </activity>
  发生这种情况的原因是,在显示键盘时, EditText不再存在。  我已经多次尝试找到一个干净的解决方案来维护adjustResize输入模式和良好的用户体验。  这就是我最终的结果: 
  为了确保在显示键盘之前EditText是可见的,你将不得不手动滚动RecyclerView ,然后集中EditText 。  您可以通过在代码中重写焦点并单击处理来完成此操作。 
  首先,通过将android:focusableInTouchMode属性设置为false来禁用EditText的android:focusableInTouchMode : 
<EditText
    android:id="@+id/et_review"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="false"/>
  然后,您必须为EditText设置一个点击侦听器,您将在其中手动滚动RecyclerView ,然后显示键盘。  您将必须知道包含EditText ViewHolder的位置: 
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();
        }
    }
});
这远不是一个简单而干净的解决方案,但希望它能帮助它帮助我。
链接地址: http://www.djcxy.com/p/24235.html