Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView Scrolls to Top when EditText Grows

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).
like image 487
NickProvost Avatar asked Apr 18 '17 16:04

NickProvost


1 Answers

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.

like image 139
Chetan Joshi Avatar answered Oct 02 '22 08:10

Chetan Joshi