Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Focus issue with EditTexts in Fragments when using ViewPager2 #107

Open
raranov opened this issue Jan 18, 2020 · 17 comments
Open

Focus issue with EditTexts in Fragments when using ViewPager2 #107

raranov opened this issue Jan 18, 2020 · 17 comments

Comments

@raranov
Copy link

raranov commented Jan 18, 2020

Build: AI-191.8026.42.35.6010548, 201911150702,

AI-191.8026.42.35.6010548, JRE 1.8.0_202-release-1483-b03x64 JetBrains s.r.o, OS Windows 10(amd64) v10.0 , screens 1536x864

AS: 3.5.3; Android Gradle Plugin: 3.5.3; Gradle: 5.4.1; NDK: from local.properties: (not specified), latest from SDK: (not found); LLDB: pinned revision 3.1 not found, latest from SDK: (package not found); CMake: from local.properties: (not specified), latest from SDK: (not found), from PATH: (not found)

I have an activity with a ViewPager2 container that swaps between three fragments. Each fragment contains two EditTexts. I have disabled manual scrolling, and the user can only traverse the fragments via a continue button. The goal is for the app to automatically focus the first EditText on the screen when the user opens that screen. The issue is that instead of the EditText getting focus, the ViewPager itself is getting the focus, even though it is not focusable. To try to fix this issue, I tried having the edittext explicitly requestFocus() in the fragment itself, but this doesn't help (after the user presses continue on the first screen, he gets to the second screen and the first edittext has no cursor or focus) and leads to some interesting scrolling issues. When I debug, I see that the ViewPager container itself has the focus.

Here is the repo, it is the ViewPager2 bug branch: https://github.com/raranov/MyExperienceNew/tree/ViewPager2
ViewPager (1) does not have this issue.

Also, I tried to do a similar thing using straight recyclerview, and that also had similar focus issues. The details are here:
https://stackoverflow.com/questions/59489756/issue-with-focus-behavior-when-using-edittexts-in-a-recyclerview

Thank you.

@CakeWalker1337
Copy link

I have also struggled with this issue! I used viewpager 1 to avoid that bug in my project.
Here is a test project which reproduces this bug: https://github.com/CakeWalker1337/ViewPager2-bugreport
It would be great, if you fix it:)

@shadabunique
Copy link

I have to use ViewPager2 in order to support Arabic locale as well. Is somebody looking into this issue?

@VyacheslavMartynenko
Copy link

VyacheslavMartynenko commented Jun 1, 2020

I have a similar problem when I use ViewPager2 with FragmentStateAdapter.
When I do navigation first time on a fragment by setCurrentItem, I can focus on an EditText. But reselecting the fragment causes the issue. You need to tap on the EditText twice because the first tap won't focus on the field, but you can still input message from the keyboard.

In this situation, as a temporary solution, I can set offscreen page limit to 1.
Still expecting a normal fix.

@HarunJr
Copy link

HarunJr commented Jun 19, 2020

Experiencing the same issue moving from fragment to fragment with edittext

@FEvgenSON
Copy link

Same on view holder

@samgithiaka
Copy link

I have the same issue and i have to use viewpager2 for vertical scroll which is not supported in viewpager1

@Nikunj2505
Copy link

Same issue when using edit text with fragments with viewpager2. Is there any work around?

@FEvgenSON
Copy link

Increase offscreen page limit

@schlagi123
Copy link

I have the same issue.

Thanks to @FEvgenSON for the workaround, but I hope it will be fixed

hussainmohd-a added a commit to celzero/rethink-app that referenced this issue Jan 25, 2021
The Viewpager in the Firewall Activity is migrated from Viewpager2 to
Viewpager1. There is an open focus issue in android.
material-components/material-components-android#500
android/views-widgets-samples#107

UI changes for the Home screen fragment.

DNS logs screen now will have the value of blocked and lifetime
queries in top layout.
@Ifans007
Copy link

Unfortunately increase offscreen page limit don't work for me

@Ifans007
Copy link

In my case the number of offscreenPageLimit should be not smaller the number of pages

@FeatherSeven
Copy link

There are 3 workarounds for this issue, all worked in my case:

  1. increase offsetScreenPageLimit, 1 is enough no matter how many pages i got;
  2. vp2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
  3. ((RecyclerView)vp2.getChildAt(0)).getLayoutManager().setItemPrefetchEnabled(false);

@PembaTamang
Copy link

Same issue here, is there a fix available ?

@PembaTamang
Copy link

There are 3 workarounds for this issue, all worked in my case:

  1. increase offsetScreenPageLimit, 1 is enough no matter how many pages i got;
  2. vp2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
  3. ((RecyclerView)vp2.getChildAt(0)).getLayoutManager().setItemPrefetchEnabled(false);

this didn't work for me

@KitsuneFolk
Copy link

Same issue here, is there a fix available ?

Hey, we solved this bug in the library called Fragula. I found out that transferring the ViewPager2 code to the project and commenting out the dispatchSelected method in the ScrollEventAdapter class solves it. If you're okay with transferring the library to your app, you might use this solution to fix the bug. Here's the commit: massivemadness/Fragula@61aca39"

@OoadaioO
Copy link

OoadaioO commented Aug 3, 2023

  1. when editText is child of viewpager2,may try this
fun ViewPager2.setDescendantFocusabilityKtx(focusability:Int) {
    if (childCount > 0) {
        val rl = this.getChildAt(0)
        if (rl is RecyclerView) {
            rl.descendantFocusability = focusability
        }
    }
}

binding.viewPager.setDescendantFocusabilityKtx(ViewGroup.FOCUS_AFTER_DESCENDANTS)

2. when edittext is slibing node of viewpager2

fun ViewPager2.setIsFocusableInTouchModeKtx(isFocusableInTouchMode:Boolean){
    if (childCount > 0) {
        val rl = this.getChildAt(0)
        if (rl is RecyclerView) {
            rl.isFocusableInTouchMode = isFocusableInTouchMode
        }
    }
}
binding.viewPager. setIsFocusableInTouchModeKtx(false)



@zhuhuan0122
Copy link

Has ViewPager2 called the setPageTransformer() method? Please check if there are any issues with the overridden ViewPager2.PageTransformer. For example, like in the code below, does it require setVisibility(View.GONE)?

private const val MAX_ROTATION = 10.0f

class RotatePageTransformer : ViewPager2.PageTransformer {

override fun transformPage(page: View, position: Float) {
    page.apply {
        when {
            position <= 0 -> {
                setViewVisible()
                alpha = 1f
                pivotX = width / 2f
                pivotY = height * 1f
                rotation = position * MAX_ROTATION
                translationX = 0f
            }
            position <= 1 -> {
                alpha = if (position == 1f) {
                    //need gone
                    setViewGone()
                    0f
                } else {
                    setViewVisible()
                    (1 - position).coerceAtLeast(0.35f)
                }
                rotation = 0f
                translationX = -position * page.width
            }
            else -> {
                setViewVisible()
                alpha = 0f
                rotation = 0f
                translationX = 0f
            }
        }
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests