gpt4 book ai didi

android - API-21 上的 Fragment 事务替换处于落后状态

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:24:45 25 4
gpt4 key购买 nike

我正在开发一个使用 fragment 的应用程序,上周我的测试设备进行了 lolipop 更新。当我在 Lollipop 设备上测试我的应用程序时,我发现 Fragment Transaction 的替换方法无法正常工作。

虽然在 Kitkat 版本上一切正常,但它在 Lolipop 版本中令人困惑。

为了解释我的情况,我添加了一些图片。

--第一个屏幕----------------------------KitKat---------------- ---------------------- Lollipop ----------------

enter image description here enter image description here enter image description here

如您所见,当我使用 kitkat 时,一切都很好,但是当我使用 lolipop 时, fragment 事务替换就开始困惑了。

这是我的按钮代码;

mButtonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

FeedbackFragment mFragmentFeedBack = new FeedbackFragment();
android.app.FragmentManager fm = getFragmentManager();
fm.executePendingTransactions();
android.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (mFragmentFeedBack.isVisible()) {
fragmentTransaction.hide(mFragmentFeedBack);
} else {

if (!mFragmentFeedBack.isAdded()) {
fragmentTransaction.replace(R.id.containerfragment, mFragmentFeedBack);
}


fragmentTransaction.show(mFragmentFeedBack);
}
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

}
});

这是我的xml;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp" />

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerfragment">

</FrameLayout>

编辑: Kitkat 版本在平板电脑上运行,但我在手机(Kitkat 版本)上尝试了我的应用程序,结果是一样的。没有变化。

谢谢。

最佳答案

可能的问题可能是代码:

if (mFragmentFeedBack.isVisible())

我不建议使用这种方法来检查可见性。根据文档 @ Fragment isVisible() , 它说

...This means it: (1) has been added, (2) has its view attached to the window, and (3) is not hidden.

这部分句子不是很清楚。我怀疑 KitKat 说它不可见,但 Lollipop 说它是,我同意 Lollipop 的实现。 KitKat 说添加了 fragment (是的),附加了 View (是的),隐藏了(不是真的!)。不管你信不信,这实际上是其他 GUI 库的 GUI 问题!

可能的解决方案,目前:

  1. 创建一个 bool 标志,并在 2 个 fragment 之间维护该标志。如果这很容易做到,这是最好的。
  2. 检查按钮或 View 是否可点击,不知道是哪一个。这比检查 isVisible() 更可靠。
  3. 我认为代码设计比应有的要复杂一些。目前,这是我的建议。当用户点击“新建按钮”时,只需调用 replace() 方法,不要使用隐藏/显示方法。当用户点击反馈 fragment 中的 SEND 按钮时,调用 popBackStack() 或 replace() 方法。怎么样?

关于android - API-21 上的 Fragment 事务替换处于落后状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30731107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com