仅显示一个片段

时间:2014-02-28 10:12:43

标签: android android-fragments android-fragmentactivity

我有一个微调器项。当我点击位置2.我想要执行以下代码。

if (pos == 2) {
    Log.e("TEST", "YOU CLICKED" + pos);
    for (int i = 0; i < 2; i++) {
        Log.e("TEST INSIDE FOR", "YOU CLICKED" + pos);
        if (i % 2 == 0) {
            // showFragOne();
            showFragOne();
            Log.e("TEST INSIDE FOR " + i, "YOU CLICKED" + pos);

            try {

                Thread.sleep(20000);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if (i %2 == 1) {
            // showFragTwo();
            showFragTwo();
            Log.e("TEST INSIDE FOR " + i, "YOU CLICKED" + pos);

            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    Toast.makeText(mCoreContext,"One Fragment added from feature app",Toast.LENGTH_LONG).show();


}

我希望这段代码能够在20秒内更改Frame布局。但只有我最后得到的第二个片段。这里 showFragTwo()功能: -

private void showFragTwo() {
                // TODO Auto-generated method stub
                FragmentTransaction ft = 
                        .getFragmentManager().beginTransaction();
                if (fragtwo.isAdded()) {
                    if (fragtwo.isHidden())
                        ft.show(fragtwo);
                    else
                        ft.hide(fragtwo);

                    ft.commit();
                } else {
                    ft.replace(com.example.coreapp.R.id.container, fragtwo,
                            "ADDFRAGTWO").commit();
                    ft.show(fragtwo);
                }
            }

此处 showFragOne()功能: -

private void showFragOne() {
        // TODO Auto-generated method stub
        FragmentTransaction ft =
                .getFragmentManager().beginTransaction();

        if (frag.isAdded()) {
            if (frag.isHidden())
                ft.show(frag);
            else
                ft.hide(frag);

            ft.commit();
        } else {

            ft.replace(com.example.coreapp.R.id.container, frag, "ADDFRAG")
                    .commit();
            ft.show(frag);
        }
    }

其中我在顶部初始化frag和fragtwo以跟随片段类: -

frag = new AddFragmentOne();
fragtwo = new AddFragmentTwo();

我将以下内容作为logcat o / p。

**02-28 04:56:34.250: E/TEST INSIDE FOR: YOU CLICKED2

02-28 04:56:34.290: E/TEST INSIDE FOR 0: YOU CLICKED2

02-28 04:56:54.350: E/TEST INSIDE FOR: YOU CLICKED2

02-28 04:56:54.350: E/TEST INSIDE FOR 1: YOU CLICKED2**

我收到o / p中的日志消息但是为什么片段没有显示?

我正在获得的第二个片段

如果我发表评论,如果我正在获得第一条评论

有谁能告诉我哪里出错了?

1 个答案:

答案 0 :(得分:2)

这是因为你让线程睡眠20秒,它可能是Ui线程。 您应该用显示相应片段的Thread.sleep()替换此Timer语句:

if(pos == 2) {
    showFragOne();
    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {
            showFragTwo();
        }
    }, 20000);
}

根据评论完成答案:我建议你制作一个方法

/** This method shows the fragment whose 'number' is {@code fragmentNumber}. */
public void showFrag(int fragmentNumber) {
    Fragment f = fragments.get(fragmentNumber); // fragments can be a List for example
    // Then use the code you already have to show fragment 'f'
}

然后,您可以像这样修改前面的代码:

if(pos == 2) {
    final AtomicInteger count = new AtomicInteger(0); // Because use inside TimerTask requires a final variable
    new Timer().scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            if(count.get() < MAX_FRAG_INDEX)
                showFrag(count.getAndIncrement);
            else
                cancel();
        }
    }, 0, 2000);
}