While Bruce's answer solves the problem, he does it very cruelly, which harms the UX, as it will clear the focus of each point of view as soon as we do the scroll.
It refers to the symptom of a problem, but does not solve the real cause.
How to reproduce the problem:
Your EditText has focus, and the keyboard opens, then scrolls until the EditText is disconnected from the screen and it has not been redesigned for the new EditText, which is now displayed.
Let me first understand why this problem occurs:
The ListView recycles its views and uses them again, as you all know, but sometimes it doesn’t need to use a view that immediately goes off the screen to save it for future use, and because it doesn’t need to be shown anymore, it will separate it , causing view.mParent to be null. however, the keyboard needs to know how to pass the input, and it does this by choosing the exact focused view or EditText.
Thus, the problem is that we have an EditText that has focus but suddenly does not have a parent, so we get the error "parameter must be a descendant of this view". has the meaning.
Using the scroll listener we cause more problems.
Decision:
We need to listen to an event that tells us when the view has moved to the heap and is no longer connected, fortunately ListView provides this event.
listView.setRecyclerListener(new AbsListView.RecyclerListener() { @Override public void onMovedToScrapHeap(View view) { if ( view.hasFocus()){ view.clearFocus(); //we can put it inside the second if as well, but it makes sense to do it to all scraped views //Optional: also hide keyboard in that case if ( view instanceof EditText) { InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } } } });
ndori Nov 17 '16 at 16:05 2016-11-17 16:05
source share