开发测试环境Android 7.0
一.配置测试
1.不配置任何东西
切换横竖屏调用了
2.配置只orientation
android:configChanges="orientation"
切换横竖屏调用了
3.配置orientation|keyboardHidden
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTask"
android:windowSoftInputMode="stateHidden|adjustResize"
以上切换横竖屏 Activty 调用了
- onConfigurationChanged:
- onSaveInstanceState:
- onDestroy:
- onCreat
- onRestoreInstanceState
4.配置orientation|screenSize|keyboardHidden
android:configChanges="orientation|screenSize|keyboardHidden"
android:launchMode="singleTask"
android:windowSoftInputMode="stateHidden|adjustResize"
切换横竖屏 Activty 调用了
- onConfigurationChanged:
其他均没调用
5.Fragment 设置setRetainInstance(true)后
切换横竖屏 Fragment 调用了
- onSaveInstanceState
- onDestroyView:
- onCreateView:
- onViewCreated:
- onActivityCreated:
- onRestoreInstanceState:
没有调用onCreat 和 onDeatory onConfigurechanged
二.适配
1. 重启Activity (manifest中1-3配置)
30NLMH~Y}A{NT{9@3BZ%RCG.png device-2017-09-30-095622.png device-2017-09-30-095633.png创建两个layout文件夹layout layout-land
activity会自动加载横竖屏layout,另外你可根据方向设置Gridlayout spancount等来适配。
在onCreat中恢复fragment,onSaveInstanceState保存fragment
把需要保持的fragment设置setRetainInstance(true),同样由于fragment会调用 onViewCreated: onActivityCreated: 也可加载不同方向layout
if (bundle != null) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
sickbedF = (SickbedF) supportFragmentManager.getFragment(bundle, "sickbedF");
communicateF = (CommunicateF) supportFragmentManager.getFragment(bundle, "communicateF");
hospitalF = (HospitalF) supportFragmentManager.getFragment(bundle, "hospitalF");
mineF = (MineF) supportFragmentManager.getFragment(bundle, "mineF");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("checkedID", checkedID);
getSupportFragmentManager().putFragment(outState, "sickbedF", sickbedF);
getSupportFragmentManager().putFragment(outState, "hospitalF", hospitalF);
getSupportFragmentManager().putFragment(outState, "communicateF", communicateF);
getSupportFragmentManager().putFragment(outState, "mineF", mineF);
if (Constance.DEBUGTAG)
Log.i(Constance.DEBUG + "--" + getClass().getSimpleName() + "--", "onSaveInstanceState: ");
}
2.不重启Activity
配置orientation|screenSize|keyboardHidden
重写 onconfigureChanged 根据不同方向调整不同布局,以recyclerview为例,可以定义不同方向布局创建不同Viewholder,方向改变时调用adapter.notifyDateChanged
![TN)H8MM6GJIY49ALN9S6B2.png
device-2017-09-30-095605.pngaa
如下图根据不同方向返回不同布局类型例子
.addType(R.layout.doctor_info, new SBaseMutilAdapter.ITEMHOLDER<DoctorInfo>() {
@Override
public boolean istype(DoctorInfo item, int position) {
return position == 0 && OrentionUtils.isPortrait(DoctorHome.this);}})
.addType(R.layout.doctor_info_land, new SBaseMutilAdapter.ITEMHOLDER<DoctorInfo>() {
@Override
public boolean istype(DoctorInfo item, int position) {
return position == 0 && !OrentionUtils.isPortrait(DoctorHome.this);}})
网友评论