简体   繁体   English

在 ListView 或 RecyclerView 中聚焦 EditText 时,键盘显示但滚动不起作用

[英]When focused EditText inside ListView or RecyclerView, the keyboard showing but scrolling not working

When I touched the EditText inside ListView or RecyclerView the soft keyboard showing.当我触摸 ListView 或 RecyclerView 中的 EditText 时,软键盘显示。 Then i clicked the next button on keyboard and the focus changed to next EditText.然后我单击键盘上的下一个按钮,焦点更改为下一个 EditText。 After last visible EditText, the focus changing to next EditText but ListView or RecyclerView not scrolling inside and all the screen going under the status bar every keyboard next Button clicked.在最后一个可见的 EditText 之后,焦点更改为下一个 EditText 但 ListView 或 RecyclerView 没有在内部滚动,并且所有屏幕都在状态栏下方,每个键盘下一个 Button 单击。

The following xml which is using for this screen:用于此屏幕的以下 xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include
            android:id="@+id/MainToolbar"
            layout="@layout/toolbar" />
        <include
            android:id="@+id/llHeaderItem"
            layout="@layout/TaskShelfShareHeaderItem" />
        <ListView
            android:id="@+id/lwShelfShare"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:stackFromBottom="true"
            android:transcriptMode="normal" />
    </LinearLayout>

投屏

I figured it out this way.我是这样想出来的。 Hope it helps.希望能帮助到你。

mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        RecyclerView recyclerView = getRecyclerView();
        if (recyclerView != null) {
            int position = getLayoutPosition();
            RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForLayoutPosition(position + 1);
            if (viewHolder == null) {
                recyclerView.smoothScrollToPosition(position + 1);
                return true;
            }
        }
        return false;
    }
});

@Ivan Vazhnov @伊万·瓦日诺夫

you need to pass the reference of the recyclerView to the adapter, something like您需要将 recyclerView 的引用传递给适配器,例如

CustomAdapter(data:Object, recyclerView: RecyclerView)

and then inside the adapter onBindViewHolder{}然后在适配器内部 onBindViewHolder{}

    try {
        val _recyclerView = recyclerView
        val _VH = _recyclerView.findViewHolderForLayoutPosition(position + 
        if(_VH != null){
        text =_VH.itemView.findViewById<EditText>(R.id.Cantidad)
        text.requestFocus()
        }
        } catch (e: Exception) {
        Log.e("RecyclerView", e.localizedMessage)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM