executePendingTransactions

executePendingTransactions

我正在尝试以编程方式添加一个片段,该片段有一个视图,我需要在该片段创建后立即初始化该视图。我试图在FragmentManager.executePendingTransactions()之后立即使用FragmentTransition.commit(),但它并没有按预期工作。我做了一个研究,发现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(),以便它存储的引用将引用创建的片段。这是一个工作代码:

@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);
}

我希望这对某人有帮助。

10-07 18:12