Android RecyclerView Edittext issue

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:

  • checkbox
  • spinner
  • edittext
  • 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..


    Using this in layout worked : 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"
            />
    

    Manifest file : Add this to the activity section 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>
    

    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:

    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.

    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"/>
    

    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);
            });
        }
    });
    

    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.

    链接地址: http://www.djcxy.com/p/24236.html

    上一篇: 输入焦点的Android软键盘问题

    下一篇: Android RecyclerView Edittext问题