[英]Why does Android ViewPager2 looses focus when switching between tabs?
I have a ViewPager2 on my Android App.我的 Android 应用程序上有一个 ViewPager2。 One of the pages contains an EditText field.其中一个页面包含一个 EditText 字段。
The scenario I encountered is as follows:我遇到的场景如下:
I prepared a small demo app to demonstrate this issue: https://github.com/hilaza/SwitchTabsBugDemo我准备了一个小的演示应用程序来演示这个问题: https://github.com/hilaza/SwitchTabsBugDemo
I debugged it and what I found was that the ViewPager clears the focus from my page, thinking that a page was selected.我对其进行了调试,发现 ViewPager 清除了我页面的焦点,认为页面已被选中。 Debugging it further I saw that it has something to do with the RecyclerView's didChildRangeChange method which wrongly assumes that something has changed.进一步调试它我发现它与 RecyclerView 的 didChildRangeChange 方法有关,该方法错误地假设某些东西已经改变。
Does anyone happen to know why it's happening and what can I do to work around this?有没有人碰巧知道为什么会这样,我该怎么做才能解决这个问题?
This is a strange bug happens because of Recycler behaviour.这是一个奇怪的错误,因为 Recycler 的行为。 Unfortunately, cannot explain why it happens, but know for sure that this row might help you:不幸的是,无法解释为什么会发生这种情况,但可以肯定地知道这一行可能对您有所帮助:
pager.offscreenPageLimit = 1
Or the equivalent in Java. This may help because it disables some of Recycler behaviour.或 Java 中的等效项。这可能会有所帮助,因为它会禁用某些 Recycler 行为。 Will be happy if someone explains it better.如果有人更好地解释它会很高兴。 Faced the same issue and resolved it with this one.面对同样的问题并用这个解决了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.