简体   繁体   English

为什么 Android ViewPager2 在选项卡之间切换时会失去焦点?

[英]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:我遇到的场景如下:

  1. Set focus on the text field -> keyboard shows将焦点放在文本字段上 -> 键盘显示
  2. Switch to another page -> keyboard dismisses (the field lost focus)切换到另一个页面 -> 键盘关闭(该字段失去焦点)
  3. Go back to the previous tab and set focus on the text field again -> the field will gain focus but immediately the focus would clear. Go 回到上一个选项卡并再次将焦点设置在文本字段上 -> 该字段将获得焦点,但焦点会立即清除。 Tapping on the field again will get the focus back.再次点击该字段将重新获得焦点。

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.

相关问题 Android:在ActivityGroup中的Activity之间切换时,如何重新获得EditText的焦点 - Android: How to regain focus of EditText when switching between Activities in ActivityGroup 如何处理viewpager2中edittext的焦点 - How to handle focus of edittext inside viewpager2 Android-EditText会获得焦点,并且在打开抽屉时不会松开焦点 - Android - EditText receives focus and never looses it while drawer is opened Android操作栏标签和键盘焦点 - Android Actionbar Tabs and Keyboard Focus Android:为什么我的EditText字段失去焦点? - Android : Why does my EditText field lose focus? DialogFragment 中的 ViewPager2 内的 EditText 不会显示键盘 - Keyboard won't show for EditText inside a ViewPager2 in a DialogFragment 编辑框未在Android中获得焦点 - Edit box does not gain focus in android Android,TabLayout(tabhost),切换标签会禁用第二个标签上的edittext。 我该如何预防? - Android, TabLayout (tabhost), switching tabs disables edittext on second tab. How do I prevent this? 显示键盘时,Android EditText无法聚焦 - Android EditText cannot focus when the keyboard is showing Android:在活动开始时明确关注edittext - Android : clear focus on edittext when activity starts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM