Focusable EditText inside ListView

后端 未结 13 2374
无人共我
无人共我 2020-11-21 23:56

I\'ve spent about 6 hours on this so far, and been hitting nothing but roadblocks. The general premise is that there is some row in a ListView (whether it\'s g

相关标签:
13条回答
  • 2020-11-22 00:05

    this post was matching exactly my keywords. I have a ListView header with a search EditText and a search Button.

    In order to give focus to the EditText after loosing the initial focus the only HACK that i found is:

        searchText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // LOTS OF HACKS TO MAKE THIS WORK.. UFF...
                searchButton.requestFocusFromTouch();
                searchText.requestFocus();
            }
        });
    

    Lost lots of hours and it's not a real fix. Hope it helps someone tough.

    0 讨论(0)
  • 2020-11-22 00:05

    Just try this

    android:windowSoftInputMode="adjustNothing"
    

    in the

    activity

    section of your manifest. Yes, it adjusts nothings, which means the editText will stay where it is when IME is opening. But that's just an little inconvenience that still completely solves the problem of losing focus.

    0 讨论(0)
  • 2020-11-22 00:07

    If the list is dynamic and contains focusable widgets, then the right option is to use RecyclerView instead of ListView IMO.

    The workarounds that set adjustPan, FOCUS_AFTER_DESCENDANTS, or manually remember focused position, are indeed just workarounds. They have corner cases (scrolling + soft keyboard issues, caret changing position in EditText). They don't change the fact that ListView creates/destroys views en masse during notifyDataSetChanged.

    With RecyclerView, you notify about individual inserts, updates, and deletes. The focused view is not being recreated so no issues with form controls losing focus. As an added bonus, RecyclerView animates the list item insertions and removals.

    Here's an example from official docs on how to get started with RecyclerView: Developer guide - Create a List with RecyclerView

    0 讨论(0)
  • 2020-11-22 00:09

    My task was to implement ListView which expands when clicked. The additional space shows EditText where you can input some text. App should be functional on 2.2+ (up to 4.2.2 at time of writing this)

    I tried numerous solutions from this post and others I could find; tested them on 2.2 up to 4.2.2 devices. None of solutions was satisfactionary on all devices 2.2+, each solution presented with different problems.

    I wanted to share my final solution :

    1. set listview to android:descendantFocusability="afterDescendants"
    2. set listview to setItemsCanFocus(true);
    3. set your activity to android:windowSoftInputMode="adjustResize" Many people suggest adjustPan but adjustResize gives much better ux imho, just test this in your case. With adjustPan you will get bottom listitems obscured for instance. Docs suggest that ("This is generally less desirable than resizing"). Also on 4.0.4 after user starts typing on soft keyboard the screen pans to the top.
    4. on 4.2.2 with adjustResize there are some problems with EditText focus. The solution is to apply rjrjr solution from this thread. It looks scarry but it is not. And it works. Just try it.

    Additional 5. Due to adapter being refreshed (because of view resize) when EditText gains focus on pre HoneyComb versions I found an issue with reversed views: getting View for ListView item / reverse order on 2.2; works on 4.0.3

    If you are doing some animations you might want to change behaviour to adjustPan for pre-honeycomb versions so that resize doesnt fire and adapter doesn't refresh the views. You just need to add something like this

    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    

    All this gives acceptable ux on 2.2 - 4.2.2 devices. Hope it will save people some time as it took me at least several hours to come to this conclusion.

    0 讨论(0)
  • 2020-11-22 00:09

    some times when you use android:windowSoftInputMode="stateAlwaysHidden"in manifest activity or xml, that time it will lose keyboard focus. So first check for that property in your xml and manifest,if it is there just remove it. After add these option to manifest file in side activity android:windowSoftInputMode="adjustPan"and add this property to listview in xml android:descendantFocusability="beforeDescendants"

    0 讨论(0)
  • 2020-11-22 00:09

    The most important part is to get the focus working for the list cell. Especially for list on Google TV this is essential:

    setItemsCanFocus method of the list view does the trick:

    ...
    mPuzzleList = (ListView) mGameprogressView.findViewById(R.id.gameprogress_puzzlelist);
    mPuzzleList.setItemsCanFocus(true);
    mPuzzleList.setAdapter(new PuzzleListAdapter(ctx,PuzzleGenerator.getPuzzles(ctx, getResources(), version_lite)));
    ...
    

    My list cell xml starts like follows:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:id="@+id/puzzleDetailFrame"
                 android:focusable="true"
                 android:nextFocusLeft="@+id/gameprogress_lessDetails"
                 android:nextFocusRight="@+id/gameprogress_reset"
    ...
    

    nextFocusLeft/Right are also important for D-Pad navigation.

    For more details check out the great other answers.

    0 讨论(0)
提交回复
热议问题