30

私のアプリには、4つのフラグメントを持つFragmentPagerAdapterを持つ単一のアクティビティがあります(ViewPagerIndicatorライブラリを使用)。これらのフラグメントの1つには、個別の縦向きと横向きの両方のレイアウトのデザインがありますが、他の3つにはないため、縦向きに固定する必要があります。

私の考えは、マニフェストに設定し、すべてのフラグメントをandroid:configChanges="orientation"呼び出しgetActivity().setRequestedScreenOrientation()て、そのうちの3つにロックし、ローテーションを許可する必要がある1つにロックすることでしたが、これは機能しません。アプリはポートレートモードのままです。onResume()SCREEN_ORIENTATION_PORTRAITSCREEN_ORIENTATION_UNSPECIFIED

これを達成する方法はありますか?

とにかくフラグメントがそのアクティビティなしで方向を変更できるようにする場合、実際のアクティビティが回転する必要はありませんが、これが可能であることに言及するものは何も見つかりませんでした。横向きの場合はタブバーが非表示になるため、アクティビティが回転しても同様に問題ありません。

4

7 に答える 7

36

Override setUserVisibleHint() in each fragment.

In the portrait only fragments:

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

in the the portrait/landscape fragment:

@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.

于 2012-11-06T14:11:07.520 に答える
10

One thing that worked for me was to just put

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

At the top of the OnCreateView() methods for each of the fragments. You would want to replace ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR with the appropriate SCREEN_ORIENTATION constant.

Once I did this it worked perfectly on ICS. You don't event have to adjust the manifest for the specific activity.

于 2012-12-22T03:07:34.953 に答える
4

Issue is if you enable configChanges you then need to handle onConfigurationChanged() method in your activity/fragments.

Meaning that if you did fire the getActivity().setRequestedScreenOrientation() you would have to manually call setContentView() again to reinflate the landscape layout.

Also setting `UNSPECIFIED' will not change to landscape, it will just remain where it is.

I would use getActivity().setRequestedScreenOrientation(SCREEN_ORIENTATION_PORTRAIT) for the portrait fragments. And getActivity().setRequestedScreenOrientation(SCREEN_ORIENTATION_LANDSCAPE) for the landscape fragments.

This will reinflate the activity layout, which means you will then need to keep track of the last ViewPager page you where on, to make sure you show that after the layout has been recreated as to default back to that fragment before they are shown to the user and fragment onResume() is called.

Its going to be fiddly but, it is possible.

于 2012-11-06T13:33:57.387 に答える
1

Write some code in AndroidManifest.xml on the particular activity tag.

android:configChanges="orientation|screenSize".

And Write code in Fragment on onCreateView method,
for Portrait:

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
setRetainInstance(true);

for Landscape:

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRetainInstance(true);
于 2018-02-06T10:43:21.213 に答える
0

in the fragment you want to set just add to your onCreate/onCreateView

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

in the other fragments you want to allow multi orientation add

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
于 2017-06-03T10:50:43.530 に答える
0

If you want to change it to landscape for specific fragment, consider this. Remember to change it back to stopsActivityInfo.SCREEN_ORIENTATION_PORTRAIT on fragment's lifecycle stop

@Nullable
@Override
public View onCreateView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    return inflater.inflate(R.layout.YOUR_LAYOUT, container, false);
}
于 2020-11-16T03:42:33.793 に答える
0

in the the portrait/landscape fragment:

@Override
    public void onResume() {
        super.onResume();
        requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
    }

and you can change it to "ActivityInfo.SCREEN_ORIENTATION_PORTRAIT" or "ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE"

于 2021-02-16T13:05:37.297 に答える