13

I have a Listview with EditTexts in every cell. Whenever I add text to the point that the EditText grows 1 line, the ListView scrolls to the top (and usually removes the EditText from view by doing so).

I found a few questions about this but they only seem to have "hacks" as answers and nothing that works for me.

ListView scrolls to top when typing in multi-line EditText

Listview with edittext - auto scroll on "next"

EDIT: After a lot of research, I have found that it is because the editText is resizing, causing the ListView layout its subviews and then resetting to the top. I want to find a way for the EditText to grow while staying at the same place.

I tried this because of a suggestion I found online but, even though it keeps the ListView at the right height, it clears the focus from the EditText and I cannot seem to get it back with requestFocus.

private Stirng getTextWatcher(){
return new TextWatcher(){
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override public void afterTextChanged(Editable s) { 
listView.setSelection(index);
  }
 }
}

So what I would need is either:

  1. Have a way for the ListView to not scroll automatically to the top when the subviews are being layed out again or

  2. A way to focus my EditText after having called ListView.setSelected() or 3. Have another way to scroll the ListView that will not clear focus from the EditText.

For option

  1. I have tried scrollTo() and smoothScrollToPosition(). scrollTo for some reason works but hides certain items from the ListView (they become invisible, not loaded). And smoothScrollToPosition() creates an animation where the ListView jumps to the top and then it brings the user back to the Item (and does it 2 or 3 times).
Community
  • 1
  • 1
NickProvost
  • 367
  • 5
  • 22
  • Can you post your code? Why don't these answers work for you? – legendof Apr 18 '17 at 17:21
  • @legendof post my code for what? the listview? It seems pretty standard to me. Nothing special. I don't do anything to it in java besides fill it, just in the XML. What I meant is that all of these are hacks and I want something elegant and efficient... not something like "scroll the listview every time a letter is added" – NickProvost Apr 18 '17 at 21:12
  • Can you show us the XML layout? The code for the listview, as well as it's parents. Is it inside a ScrollView by an chance? – Kostas Andrianos Apr 22 '17 at 20:55

3 Answers3

4

As par my view if you want to take input from ListView item by using EditText then you should use TextView in place EdiText and by click of TextView you should open and Input Dialog with EditText that is more simple and good solution. handling of EditText focus in ListView is vary difficult and you need to put extra Listeners for this.

OR

And if you have limited number of input box in your UI/UX then you can implement EditText with CustomListView by using ScrollViews.

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • 1
    I just tested your method. The second one worked flawlessly for me. What I did was use a linear layout inside of a scroll view and insert my views programatically in java. It works great and the scroll view stays at it's place when the edittext grows. Thank you. – NickProvost May 02 '17 at 02:03
0

ListView Scrolls to Top when EditText Grows

ListView has some known limitations related to child focus.

Solution

Why don't you try to use RecyclerView instead of ListView? Google introduced RecyclerView as the default recommended way of such UI. So custom ListView or hacks ore no more needed.

Darish
  • 11,032
  • 5
  • 50
  • 70
  • The same case also happens on RecyclerView. https://stackoverflow.com/questions/36765265/android-edittexts-container-recyclerview-auto-scroll-when-edittext-is-focused?rq=1 – ziLk Mar 15 '18 at 09:11
0

You can try adding this attribute to your ListView:

android:transcriptMode="normal"

This makes list automatically scroll to the bottom when a data set change notification is received and only if the last item is already visible on screen.

Read more here: attr_android:transcriptMode | Android Developers

Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55