Ferran Maylinch
Ferran Maylinch

Reputation: 11519

Filter using EditText inside a ListView

I would like to filter a ListView while typing into an EditText (searchInput) that is the first item of the list. When the list is scrolled up the searchInput should "hide" (just because it scrolls up together with the other rows).

I have a piece of code similar to this:

    searchInput.addTextChangedListener(new SimpleTextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            adapter.filter(s.toString());
        }
    });

The problem I have is that when I start typing into the searchInput and the list starts to filter, the searchInput loses the focus. I guess this is because the list is being refreshed (and so is the searchInput, that is inside).

I know I can put a search view in the action bar. I actually had that component before, but I would like to move to this new design.

I've seen this design is used in Instagram, for example. I wonder if they have done some custom component or they have found a way to solve this issue.

Upvotes: 1

Views: 19

Answers (1)

diegoveloper
diegoveloper

Reputation: 103401

Try using this on your ListView:

android:descendantFocusability="afterDescendants"

Upvotes: 1

Related Questions