【问题标题】:LayoutTransition causes Fragment to not show up the second time is createdLayoutTransition 导致 Fragment 第二次创建时不显示
【发布时间】:2016-04-19 08:54:06
【问题描述】:

我有一个实现此方法的活动:

public void pushFragment(MyFragment fragment) {
    stack.add(fragment);
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.main_frame_layout, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
    setTitle(fragment.getTitle(this));
}

在一个片段中,我有一个带有这个 onItemClickListener 的列表视图:

getAppActivity().pushFragment(anotherFragment);

AnotherFragment onCreateView 方法:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    setHasOptionsMenu(true);

    LayoutTransition t = new LayoutTransition();
    container.setLayoutTransition(t);

    View rootView = inflater.inflate(R.layout.another_layout, container, false);

    final ViewGroup vg = (ViewGroup)rootView.findViewById(R.id.avatar_image_layout);
    vg.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);

    // Stuff

    return rootView;
}

在 API 级别 >= 19 上一切正常。 在 API 18 和 17 上(我没有在 API 16 上测试),我第一次点击它的列表视图项目。然后我返回并再次单击该项目。这次创建了fragment,调用了onActivityCreated和onCreateView等,但是没有显示fragment。

编辑 1: 评论随机的东西,我发现问题是:

LayoutTransition t = new LayoutTransition();
container.setLayoutTransition(t);

评论这两行解决了问题,但我不知道为什么,我失去了动画......

【问题讨论】:

  • 按下后按时你的代码是什么?您是否覆盖它并使用 fragment.popbackstack()?
  • 是的,但正如您从我的编辑 1 中看到的那样,问题出在其他地方
  • 我避免在我的片段上使用动画,但如果你愿意,我认为这可能会对你有所帮助stackoverflow.com/questions/9194311/…

标签: android android-fragments android-activity android-transitions


【解决方案1】:

这可能因 API 而异,但在调用 commit() (info) 后,FragmentTransaction 可能不会立即应用。

根据FragmentTransaction.javacommit() 方法声明:

 * Schedules a commit of this transaction.  The commit does
 * not happen immediately; it will be scheduled as work on the main thread
 * to be done the next time that thread is ready.

为了使这立即发生,在commit()方法之后,我们调用FragmentManager.javaexecutePendingTransactions()more info):

 * After a {@link FragmentTransaction} is committed with
 * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it
 * is scheduled to be executed asynchronously on the process's main thread.
 * If you want to immediately executing any such pending operations, you
 * can call this function (only from the main thread) to do so.  Note that
 * all callbacks and other related behavior will be done from within this
 * call, so be careful about where this is called from.

【讨论】:

    猜你喜欢
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 2018-06-26
    相关资源
    最近更新 更多