简体   繁体   中英

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).

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 .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM