允许旋转/景观于一体片段片段、景观

2023-09-03 23:05:15 作者:深情不及久伴 *

我的应用程序有带4个片段(使用ViewPagerIndicator库)一个FragmentPagerAdapter单个活动。一这些片段的具有设计为一个单独的纵向和横向布置,其他三个不和需要被固定到纵向。

My app has a single Activity with a FragmentPagerAdapter with four fragments (Using the ViewPagerIndicator library). One of these fragments has designs for both a separate portrait and landscape layout, the other three do not and need to be fixed to portrait orientation.

我的想法是设置机器人:configChanges =定向在清单中,并调用 getActivity()setRequestedScreenOrientation() onResume()所有的碎片,锁定到 SCREEN_ORIENTATION_PORTRAIT 在他们三个人,而是 SCREEN_ORIENTATION_UNSPECIFIED 在一个需要允许旋转,但这并不工作。该应用程序仍处于纵向模式。

My thought was to set android:configChanges="orientation" in the manifest and call getActivity().setRequestedScreenOrientation() in the onResume() of all the fragments, locking to SCREEN_ORIENTATION_PORTRAIT in three of them but to SCREEN_ORIENTATION_UNSPECIFIED in the one that needs to allow rotation, but this doesn't work. The app remains in portrait mode.

有没有办法来实现这一目标?

Is there a way to achieve this?

这实际上不是必需的实际活动来旋转是否有无论如何,让一个片段来改变方向不其活性这样做,但我还没有发现任何提到这是可能的。这也将是同样确定,如果该活动自旋转标签栏会在横向被隐藏起来的时候。

It isn't actually necessary for the actual activity to rotate if there is anyway to allow a fragment to change orientation without its activity doing so, but I have not found anything mentioning this being possible. It would also be equally ok if the activity rotates since the tab bar will be hidden when in landscape orientation.

推荐答案

覆盖 setUserVisibleHint()中的每个片段。

在纵向唯一片段:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser) {
        Activity a = getActivity();
        if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

在纵向/横向片段:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser) {
        Activity a = getActivity();
        if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
    }
}

这将允许整个活动在一个片段旋转,但是它固定到肖像在其他

This will allow the whole activity to rotate in one fragment, but fix it to portrait in others.