3

我创建了一个可编辑项目列表(从后端接收)。我为此使用了“新”回收站。就在我recyclerview一对夫妇可能viewTypes的:Android RecyclerView编辑文本问题

  • 复选框
  • 微调
  • 的EditText

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

+0

我还没有完全熟悉'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

+0

任何解决方案? –

+0

您是否找到解决方案? – Andrea

回答

3

布局使用此工作: 机器人: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" 
     /> 

清单文件:这个添加到活动部分机器人: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> 
1

发生这种情况的原因是,在显示键盘时,您的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(); 
     } 
    } 
}); 

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

+1

这是完美的解决我的问题! – KuTear

+0

无法解析符号'UiUtils' – temirbek

+0

@temirbek这只是一种隐藏键盘的静态工具方法。您应该能够使用简单的Google搜索来查找适合您的案例的实现。 – Bolein95