gpt4 book ai didi

java - `commitNow()` 和 `postDelayed` 如何使这段代码起作用?

转载 作者:行者123 更新时间:2023-12-01 23:25:05 26 4
gpt4 key购买 nike

我尝试过做一些 fragment 工作。我将 recyclerview 布局放入布局中,并在 fragment 的 onCreateView 中初始化 recyclerview。最后,我设置了 recyclerview 的 pos 动态及其项目。

我有这样的代码,即使延迟为 0 毫秒,也可以与 commitNowpostDelayed 正常工作,但我不知道为什么:

        getSupportFragmentManager()
.beginTransaction()
.replace(R.id.parentLayout, popupFragment)
.commitNow();
new Handler().postDelayed(()->popupFragment.show(), 0);

但我尝试过:

        getSupportFragmentManager()
.beginTransaction()
.replace(R.id.parentLayout, popupFragment)
.commitNow();
popupFragment.show();
        getSupportFragmentManager()
.beginTransaction()
.replace(R.id.parentLayout, popupFragment)
.commitNowAllowingStateLoss();
popupFragment.show();
        getSupportFragmentManager()
.beginTransaction()
.replace(R.id.parentLayout, popupFragment)
.runOnCommit(()->popupFragment.show())
.commitNowAllowingStateLoss();
        getSupportFragmentManager()
.beginTransaction()
.replace(R.id.parentLayout, popupFragment)
.runOnCommit(()->popupFragment.show())
.commitNow();

它们都有一个未初始化的高度。

根据我的猜测,这是关于 postDelayed 使代码在下一个循环上运行,但是, runOnCommit 似乎也是先提交然后运行可运行的,为什么这是不起作用?

最佳答案

如果您想要一个“弹出” fragment ,建议是像这样使用 AlertDialog 类(并从 onCreateView 重构您的代码,因为不能保证您能够引用 onCreateView 中的 Activity ):

@Override
public void onViewCreated(View view, Bundle onSaveInstanceState) {
super.onViewCreated(view, onSaveInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getActivity());
View popupFragmentsResourceView = inflater.inflate(R.layout.fragment_popup_view, null);
builder.setView(popupFragmentsResourceView);
//grab controls here
//RecyclerView rv = popupFragmentsResourceView.findViewById(R.id.some_id);
//rv.setAdapter(new CustomAdapter(getActivity));
//set a click button for your dialog builder
builder.show();
}

关于java - `commitNow()` 和 `postDelayed` 如何使这段代码起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58309788/

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