方向更改时不可见的片段
Posted
技术标签:
【中文标题】方向更改时不可见的片段【英文标题】:Fragments not visible on orientation change 【发布时间】:2015-06-09 03:13:33 【问题描述】:在以下代码中,片段是从布局 xml 动态扩展的。我面临的问题是我在旋转设备时看不到碎片。
如果我查看片段管理器,我确实看到片段存在。它们被添加和附加,但在屏幕上没有。
MenuCard extends Activity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
leftContainer = (FrameLayout) findViewById(R.id.leftContainer);
rightContainer = (FrameLayout) findViewById(R.id.rightContainer);
Fragment fragment = mFragmentManager.findFragmentById(R.id.leftFragment);
if (fragment == null)
LayoutInflater.from(this).inflate(R.layout.left_fragment, leftContainer);
fragment = mFragmentManager.findFragmentById(R.id.rightFragment);
if (fragment == null)
LayoutInflater.from(this).inflate(R.layout.right_fragment, rightContainer);
R.layout.left_fragment
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:id="@+id/leftFragment"
android:name="com.example.LeftFragment"
tools:layout="@layout/left_fragment">
</fragment>
R.layout.right_fragment
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:id="@+id/rightFragment"
android:name="com.example.LeftFragment"
tools:layout="@layout/left_fragment">
</fragment>
为 R.layout.content 添加代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spl"
android:layout_
android:layout_>
<FrameLayout
android:id="@+id/leftContainer"
android:layout_
android:layout_
android:background="#00AFF0"
/>
<FrameLayout
android:id="@+id/rightContainer"
android:layout_
android:layout_
/>
</android.support.v4.widget.SlidingPaneLayout>
【问题讨论】:
你能分享你的 content.xml 吗? 看起来不错,虽然附加片段的方式有点有趣,但它应该可以工作。我注意到您在 left_fragment.xml 和 right_fragment.xml 中都指定了 LeftFragment。您确定 Fragments 本身实际上在屏幕上显示任何内容吗?你能只用带有彩色背景的片段做一个简单的测试,并确保它完全显示出来吗? 它们在加载时完美显示内容。旋转时的问题。 mContainerId、mView、mInnerView 都为空。但是片段是可见的、附加的和添加的:o 左右两边的LeftFragment只是为了简化示例 看看底部的帖子。我已经发布了答案。 【参考方案1】:您可以尝试在片段活动声明中的清单中添加以下行:
android:configChanges="screenSize|orientation"
这将阻止 onCreate 在屏幕方向更改。为了处理方向更改,您可以覆盖 onConfigurationChanges。
【讨论】:
这绕过了这个问题,当我看到这个问题时,我就有了这个解决方案。我更感兴趣的是弄清楚我们所看到的内容和原因。【参考方案2】:所以在大量阅读片段管理器代码后,我发现了这些神奇的线条:
// For fragments that are created from a layout, when restoring from
// state we don't want to allow them to be created until they are
// being reloaded from the layout.
这意味着我的案例的正确代码是
@覆盖 protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);
setContentView(R.layout.content);
leftContainer = (FrameLayout) findViewById(R.id.leftContainer);
rightContainer = (FrameLayout) findViewById(R.id.rightContainer);
LayoutInflater.from(this).inflate(R.layout.left_fragment, leftContainer);
LayoutInflater.from(this).inflate(R.layout.right_fragment, rightContainer);
SupportFragmentManager 将在方向改变的情况下重用片段,并且只会膨胀布局。
【讨论】:
【参考方案3】:我从未尝试过您的设计,但我觉得它很有趣。虽然我不推荐。
由于方向已更改,因此未重新创建 UI。这是因为没有重新创建 Activity,因此更改方向时不会触发 onCreate
。当屏幕因不同的因素发生变化时,触发onCreateView。这是我从未在 Activity 子类中尝试过的。它通常在 Fragment 子类中完成。实际上,onCreate
仅在 App 因用户操作或崩溃而停止时触发。
如果/当我在 Activity 中找到一个很好的 onCreateView 方法示例,我会发布它。
【讨论】:
以上是关于方向更改时不可见的片段的主要内容,如果未能解决你的问题,请参考以下文章