相关疑难解决方法(0)

将Fragment动态附加到<FrameLayout>?

好吧,我有一个简单的<FrameLayout>:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FragmentContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
Run Code Online (Sandbox Code Playgroud)

然后在我的代码中,我添加了一个片段:

FragClass aFrag = new FragClass();
getSupportFragmentManager().beginTransaction()
        .replace(R.id.FragmentContainer, aFrag).commit();
Run Code Online (Sandbox Code Playgroud)

在我的代码中的其他地方,我想FragClass (extends Fragment)从ID中获取该对象R.id.FragmentContainer.

我试过了

((ViewGroup) findViewById(R.id.FragmentContainer)).getChildAt(0)
Run Code Online (Sandbox Code Playgroud)

要么

((FrameLayout) findViewById(R.id.FragmentContainer)).getChildAt(0)
Run Code Online (Sandbox Code Playgroud)

但他们正在返回View,而不是Fragment依附于它.

我知道我可以将变量保留在aFrag某处,所以我不需要再找到它.但我相信应该有办法来解决它.

android android-layout android-fragments android-framelayout

38
推荐指数
1
解决办法
3万
查看次数

试图将片段添加到我的片段容器FrameLayout

我创建了一个名为editor.xml的xml文件,其中包含一个FrameLayout.在我的主要活动中,我正在尝试将自定义片段添加到FrameLayout中.

我在尝试添加片段时收到的错误是:

FragmentTransaction类型中的方法add(int,Fragment)不适用于参数(int,editorFrag)

然而,我的editorFrag扩展了Fragment,所以我很困惑为什么会发生这种情况.下面是我提到的文件的代码.任何帮助表示赞赏.

Editor.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

editorFrag.java

public class editorFrag extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) 
    {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.newlevel, container, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

public class editorActivity extends FragmentActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.editor);

        // Check that the activity is using the layout version with the fragment_container FrameLayout
        if(findViewById(R.id.fragment_container) != null)
        {
            // if we …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-fragments android-framelayout

25
推荐指数
3
解决办法
6万
查看次数

Android Honeycomb:如何更改FrameLayout中的碎片,而无需重新创建它们?

是否可以在片段之间切换而无需一直重新创建片段?如果是这样,怎么样?

在文档中,我找到了如何替换Fragments的示例.

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
Run Code Online (Sandbox Code Playgroud)

但我不希望每次需要时都从头开始创建我的碎片.

我还发现了这个 隐藏/显示片段的例子:

// The content view embeds two fragments; now retrieve them and attach
// their "hide" button.
FragmentManager fm = getFragmentManager();
addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));
Run Code Online (Sandbox Code Playgroud)

但是,如何在XML文件外部创建一个带有ID的片段?

我认为这可能与这个问题有关,但没有答案.:/ …

android android-fragments android-3.0-honeycomb

14
推荐指数
1
解决办法
2万
查看次数