Android只控制单个Fragment支持横屏竖屏

工具类

import android.app.Activity;
import android.app.Fragment;
import android.content.pm.ActivityInfo;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.OrientationEventListener;

import java.lang.ref.WeakReference;

/**
 * Created by Administrator on 2021/4/21 0021.
 */

public class SwitchDoIt{
    private OrientationEventListener landOrientationListener
    private OrientationEventListener portOrientationListener
    private WeakReference<Activity> weakReference
    private boolean portLock = false;
    private boolean landLock=false;


    public SwitchDoIt(final Activity activity) {
        this.weakReference = new WeakReference(activity);
        //横屏
        this.landOrientationListener = new OrientationEventListener(activity, 3) {
            public void onOrientationChanged(int orientation) {
                Log.d(MySensorHelper.TAG, "mLandOrientationListener");
                if(orientation < 100 && orientation > 80 || orientation < 280 && orientation > 260) {
                    if(!MySensorHelper.this.isLandLock) {
                        Activity mActivity = MySensorHelper.this.weakReference.get();
                        if(mActivity != null) {
                            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                            landLock=true;
                            portLock=false;
                        }
                    }
                }

            }
        };
        //竖屏
        this.portOrientationListener = new OrientationEventListener(activity, 3) {
            public void onOrientationChanged(int orientation) {
                Log.w(MySensorHelper.TAG, "mPortOrientationListener");
                if(orientation < 10 || orientation > 350 || orientation < 190 && orientation > 170) {
                    if(!MySensorHelper.this.isPortLock) {
                        Activity mActivity = MySensorHelper.this.weakReference.get();
                        if(mActivity != null) {
                            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                            portLock=true;
                            landLock=false;
                        }
                    }
                }
            }
        };
    }

    //关闭横竖屏切换
    public void disable(Activity activity) {
        Log.e(TAG, "disable");
        this.portOrientationListener.disable();
        this.landOrientationListener.disable();
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    //打开横竖屏切换
    public void enable(){
        this.portOrientationListener.enable();
        this.landOrientationListener.enable();
    }
}

使用方法:
在Activity开启
MySensorHelper mySensorHelper=new MySensorHelper(this);
在这里插入图片描述
点击事件 切换到需要横竖屏切换的fragment处开启其它fragment关闭


Manifest处设置为强制竖屏
theme表示不切换横竖屏不重新创建页面

在这里插入图片描述
这是写在碎片中横竖屏的监听

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值