gpt4 book ai didi

android - FragmentManager.executePendingTransactions 不会立即调用 onCreateView()

转载 作者:可可西里 更新时间:2023-11-01 11:40:57 26 4
gpt4 key购买 nike

我目前正在尝试以编程方式添加一个 fragment ,该 fragment 具有一个我需要在 fragment 创建后立即初始化的 View 。我尝试在 FragmentTransition.commit() 之后立即使用 FragmentManager.executePendingTransactions(),但它无法正常工作。我做了一个研究,发现 this ,所以我意识到 FragmentManager.executePendingTransactions()onCreate() 中不起作用,然后将代码放入 onStart(),但它仍然不起作用。虽然在我的例子中它是从 onItemClickListener() 调用的,但我认为它没有任何区别。这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vk_music_list);
musicProgressBar = new MusicProgressBar();
listView = (ListView) findViewById(R.id.listView);
}

@Override
protected void onStart() {
super.onStart();
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String audioUrl = ((VKApiAudio) parent.getAdapter().getItem(position)).url;
serviceIntent.putExtra("audioUrl", audioUrl);

//Inflate music progress bar
fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = fragmentManager.findFragmentById(R.id.musicBarContainer);
if(fragment == null) {
fragmentTransaction.add(R.id.musicBarContainer, musicProgressBar)
.setTransition(R.anim.move_from_the_bottom)
.commit();
}

//This guy doesn't work
fragmentManager.executePendingTransactions();

//Never get into this, because fragment is always == null
if(fragment != null) {
SeekBar seekBar = (SeekBar) fragment.getView().findViewById(R.id.progressBar);
seekBar.setMax(((VKApiAudio) parent.getAdapter().getItem(position)).duration);
}
startService(serviceIntent);
}

最佳答案

好吧,我终于找到问题所在了。问题的原因是 fragmentTransaction.commit()fragmentManager.executePendingTransactions() 都没有更改存储在 fragment 变量中的引用。因此,在 executePendingTransactions() 之后,我需要再次调用 findFragmentById(),因此它存储的引用将引用创建的 fragment 。这是一个工作代码:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String audioUrl = ((VKApiAudio) parent.getAdapter().getItem(position)).url;
serviceIntent.putExtra("audioUrl", audioUrl);

//Inflate music progress bar
fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = fragmentManager.findFragmentById(R.id.musicBarContainer);
if(fragment == null) {
fragmentTransaction.add(R.id.musicBarContainer, musicProgressBar)
.commit();
}

//Force to create fragment without scheduling so it's not necessarily to wait until it get scheduled
fragmentManager.executePendingTransactions();

//This is what I needed
fragment = fragmentManager.findFragmentById(R.id.musicBarContainer);

//Set song duration as a seekBar's maximum
if (fragment != null) {
SeekBar seekBar = (SeekBar) fragment.getView().findViewById(R.id.progressBar);
seekBar.setMax(((VKApiAudio) parent.getAdapter().getItem(position)).duration);
}
startService(serviceIntent);
}

希望对大家有所帮助。

关于android - FragmentManager.executePendingTransactions 不会立即调用 onCreateView(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36112666/

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