简体   繁体   English

Android ViewPager片段

[英]Android ViewPager fragments

I would like to ask if someone now's, is it possible to force ViewPager to instantiate one item at a time, not two as it does on default? 我想问一下是否有人,是否可以强迫ViewPager实例化一个项目,而不是像默认情况下那样实例化两个? Because I have one ViewPager with three fragments and one of them contains EditText which must force soft keyboard to show. 因为我有一个包含三个片段的ViewPager ,并且其中一个包含EditText ,必须强制软键盘显示。 The problem is that ViewPager instantiate two fragments at a time an when I come from third fragment to second, first is ceated and it forces soft keyboard to show on fragment with no EditText ... Thanks in advance. 问题是ViewPager实例化两个片段,当我从第三个片段到第二个片段时,第一个被终止,并且它强制软键盘显示在没有EditText片段上……谢谢。

The short answer is that you can't force the ViewPager not to create at least one offscreen fragment. 简短的答案是,您不能强制ViewPager不创建至少一个屏幕外片段。 That's simply necessary for the animation to work. 这是动画正常工作所必需的。

As a workaround, you might check if the fragment is currently displayed and depending on that show the keyboard. 解决方法是,您可能需要检查片段是否当前显示以及取决于显示的键盘。 You can check this by overriding setMenuVisibility 您可以通过覆盖setMenuVisibility检查

@Override
public void setMenuVisibility(final boolean visible) {
    super.setMenuVisibility(visible);
    if (visible) {
        // show keyboard
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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