gpt4 book ai didi

java - 确定要更新的 fragment

转载 作者:行者123 更新时间:2023-11-30 10:48:15 25 4
gpt4 key购买 nike

我正在更新当前显示的 View 并隐藏/显示两个 fragment 。

FragmentTransaction fragmentTransaction =  getSupportFragmentManager().beginTransaction();

if (mTaxLocationListViewFrag.isVisible()) {
if (mGoogleMapFrag == null) {
mGoogleMapFrag = new GoogleMapFragment();
fragmentTransaction.add(R.id.fragment_container, mGoogleMapFrag);
}
fragmentTransaction.hide(mListViewFrag);
fragmentTransaction.show(mGoogleMapFrag).commit();

} else {fragmentTransaction.hide(mGoogleMapFrag).show(mListViewFrag).commit();
}

updateDisplay();

updateDisplay 方法-

if (mListViewFrag != null && mListViewFrag.isVisible()) {
Log.d(TAG, "Update Location ListView");
mListViewFrag.setLocations(mLocations);
}

if (mGoogleMapFrag != null && mGoogleMapFrag.isVisible()) {
Log.d(TAG, "Update Google Maps Markers");
//Convert locations to markers to display.

}

问题是我猜测 fragmentTransaction 在执行 .show.hide 时是异步的,并且 updateDisplay 没有像它认为的那样正常工作要隐藏的 fragment 仍在显示。有没有更可靠的方法来检查要更新哪个 fragment ?

最佳答案

添加 fragment 后添加fragmentTransaction.executePendingTransactions()。来自 executePendingTransactions() 的文档:

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.

关于java - 确定要更新的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35873306/

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