片段:删除所有片段的视图 [英] Fragments: Remove all fragments in a view

查看:190
本文介绍了片段:删除所有片段的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面对这个场景,是在我的应用我有一个的单窗格双窗格的风格布局。而不是单独处理每一个导航操作可以在屏幕之间,对于每一个不同的风格布局,我使用的是函数,它正确地设置了布局时给出所需的画面。

The scenario I am faced with, is in my application I have a single pane and dual pane style layout. Rather than individually handle every single navigation operation possible between the screens, for every different style of layout, I am using a function which sets up the layout correctly when given the desired screen.

它基本上是一个开关语句在应用程序的每个屏幕上,一个嵌套的开关每个屏幕声明来处理每个布局样式。这就是我在code念叨着:

It is basically a switch statement for each screen in the app, with a nested switch statement in each screen to handle each layout style. This is what I'm talking about in code:

protected void setupScreen() {
    switch(currentScreen) {
    case SCREEN_ONE:
        switch(currentLayout) {
        case SINGLE_PANE:
            // Perform actions to setup the screen
            break;
        case DUAL_PANE:
            // Perform actions to setup the screen
            break;
        }
        break;
    case SCREEN_TWO:
        switch(currentLayout) {
        case SINGLE_PANE:
            // Perform actions to setup the screen
            break;
        case DUAL_PANE:
            // Perform actions to setup the screen
            break;
        }
        break
    // ... etc ....
    }
}

在这里我想的执行的操作设置屏幕的部分,这包括以下三个基本操作:

In the section where I want to perform the actions to setup the screen, this consists of the following basic three operations:

// Create the fragments if necessary
if (screenFragment == null) {
    screenFragment = new myFragment();
}

// Remove the existing fragments from the layout views
// HOW???

// Add the fragments for this screen to the view
getSupportFragmentManager().beginTransaction().add(pane1.getId(), myFragment, "myFragment").commit();

正如你所看到的,我所挣扎的是如何做第二步。 如何你删除所有片段 - 从给定的查看不知道到底哪些是你是想去除?我发现的最接近的是 FragmentTransaction.replace()这也成功地做到这一点的每一种情况下的,但当它变成了要更换片段用相同的片段。在这种情况下,它不会删除所有,然后加入(如文档建议),它只是似乎删除。这是一个问题与使用兼容性库,或者是没有办法的办法 FragmentTransaction.replace()应使用?

As you can see, what I am struggling with is how to do the second step. How do you remove all Fragments from a given View without knowing exactly which ones you are wanting to remove? The closest I have found is FragmentTransaction.replace() which does successfully do this for every case but when it turns out you are replacing a Fragment with the same fragment. In this case, it does not remove all, then add (like the documentation suggests), it just seems to remove. Is this an issue with using the compatibility libraries or is it not the way FragmentTransaction.replace() should be used?

在任何情况下,我应该怎么去这样做?我一定要codeA removeAllFragments()功能要通过每一个片段并将其分离或者是有没有办法做的第一件什么'在一个两个半 FragmentTransaction.replace()功能声称办?

In any case, how should I go about doing this? Do I have to code a removeAllFragments() function to go through every fragment and detach it or is there a way to do the first half of what the 'two in one' FragmentTransaction.replace() function claims to do?

推荐答案

典型的机制是使用<一个href="http://developer.android.com/reference/android/app/FragmentManager.html#findFragmentByTag%28java.lang.String%29"><$c$c>FragmentManager.findFragmentByTag() 。您可以使用这一点,并添加标签到您的片段(或替代的ID)。这样,您就可以判断目前正在管理的是什么片段。然后,一旦你有一个句柄present片段(findFragmentByTag返回非空),你可以使用<一个href="http://developer.android.com/reference/android/app/FragmentManager.html#beginTransaction%28%29"><$c$c>FragmentManager.beginTransaction()开始FrgamentTransaction和删除/添加必要的片段。在这种工作方式可以让你避免重新添加过程中要保留的片段。

The typical mechanism is to use FragmentManager.findFragmentByTag() . You use this and add tags to your fragments (or the alternative for id's). This way you can determine what fragments are currently being managed. Then, once you have a handle to a present fragment (findFragmentByTag returns non-null), you can use FragmentManager.beginTransaction() to start a FrgamentTransaction and remove / add the necessary fragments. Working in this way will allow you to avoid the 're-adding' process for the fragment you want to keep.

什么我可能做的是有code像这样:(警告伪code)

What I'd probably do is have code like so: (warning psuedo code)

Frgament pane1 = FragmentManager.findFragmentByTag("myFragmentPane1");
Frgament pane2 = FragmentManager.findFragmentByTag("myFragmentPane2");

setupScreen(pane1, pane2);

您也应该考虑,而不必一切都在一个班类的子类。你有Martin Fowler的更换条件有子类。否则,我担心这将是非常努力,经理当你添加另一个屏幕。

You should also consider subclasses of your class instead of having 'everything in one class'. You have a fairly obvious case of Martin Fowler's Replace Conditional with Subclass. Otherwise, i fear this is going to be incredibly hard to manager when you add another screen.

这篇关于片段:删除所有片段的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆