一段时间后自动切换片段

时间:2014-12-29 16:48:59

标签: android fragment

我正在编写一个Android应用程序,它将nexus 7平板电脑转换为信息面板,该面板应该全屏显示某些信息 - 无需用户交互。

对于每种类型的信息,我使用相应的UI创建了一个片段。我想要实现的是主活动每5秒钟切换一次片段。

在每个片段中,我实现了一个简单的回调,告诉Activity它的AsyncTask完成了获取数据并构建(全屏)UI:

((MainActivity)getActivity()).fragmentFinishedLoading();

在我的活动中,该方法如下所示:

public void fragmentFinishedLoading(){
    finishedFragments++;

    while(finishedFragments == amountOfFragments){
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragment_A));
        fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragment_B));
        fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragment_C));
        fragmentTransaction.show(fragmentManager.findFragmentById(R.id.fragment_D));
        fragmentTransaction.commit();
        fragmentManager.executePendingTransactions();
        Thread.sleep(5000);
        }
    }

请注意,上面的代码尚未包含动态更改应隐藏或显示哪个片段的功能。

我的问题:在

之后
fragmentTransaction.commit();

甚至在

之后
fragmentManager.executePendingTransactions();
片段根本没有变化。我检查了commit()方法,发现它是异步的。我知道Thread.sleep()是不好的做法,因为它冻结了UI,但由于不需要UI交互,我可以忍受。但是,由于我将UI线程发送到睡眠状态,因此fragmentTransaction可能也会休眠(?)

我正在寻找一种在给定时间之后隐藏/显示一定数量的片段而无需任何用户交互的方法 - 非常感谢任何帮助。

编辑: rguerra的CountDownTimer建议就行了。为了透明起见,这是适合我的解决方案:

public void fragmentFinishedLoading(){
    finishedFragments++;

    if(finishedFragments == amountOfFragments){
        new FragmentTimer(5000, 1000, 1).start();
    }
}

private class FragmentTimer extends CountDownTimer{
    int fId;
    static final int amountOfFragments = 4;

    public FragmentTimer(long millisInFuture, long countDownInterval, int fragmentId) {
        super(millisInFuture, countDownInterval);
        fId = fragmentId;
    }

    @Override
    public void onTick(long millisUntilFinished) {
    }

    @Override
    public void onFinish() {

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        if(fId == 1){
            fragmentTransaction.show(fragmentManager.findFragmentById(R.id.A_fragment));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.B_fragment));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.C_fragment));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.D_fragment));
        }
        else if (fId == 2){
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.A_fragment));
            fragmentTransaction.show(fragmentManager.findFragmentById(R.id.B_fragment));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.C_fragment));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.D_fragment));
        }
        else if (fId == 3){
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.A_fragment));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.B_fragment));
            fragmentTransaction.show(fragmentManager.findFragmentById(R.id.C_fragment));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.D_fragment));
        }
        else if (fId == 4){
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.A_fragment));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.B_fragment));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.C_fragment));
            fragmentTransaction.show(fragmentManager.findFragmentById(R.id.D_fragment));
        }

        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        fragmentTransaction.commit();
        fragmentManager.executePendingTransactions();

        if(fId == amountOfFragments){
            fId = 1;
        }
        else{
            fId++;
        }
        new FragmentTimer(5000, 1000, fId).start();
    }
}

2 个答案:

答案 0 :(得分:1)

使用替换片段。 show R.id.fragment_D

的示例
new CountDownTimer(5000, 1000) {

         public void onTick(long millisUntilFinished) {

         }

         public void onFinish() {
             FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
             ft.replace(R.id.R.id.fragment_D, (Fragment) new FragmentAFragment(), FRAGMENTA_TAG);
             ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
             ft.commit();
         }
      }.start();

http://developer.android.com/reference/android/os/CountDownTimer.html

答案 1 :(得分:0)

有几种方法可以做到这一点。您可以将Handler或Alarm Manager与PendingIntent或Timer配合使用。

根据您的设计听起来,我建议ViewPager可能是管理片段之间转换的不错选择。

Handler mHandler;
public void useHandler() {
    mHandler = new Handler();
    mHandler.postDelayed(mRunnable, 5000);
}

private Runnable mRunnable = new Runnable() {
    @Override
    public void run() {
        mMyFragmentStatePagerAdapter.myViewPagerIncrementFunction();
        useHandler();
    }
};

取消:mHandler.removeCallbacks(mRunnable);