我怎样才能实现FragmentManager和FragmentTransaction更换只是一个单一的片段? [英] How can I implement FragmentManager and FragmentTransaction to replace just a single fragment?

查看:211
本文介绍了我怎样才能实现FragmentManager和FragmentTransaction更换只是一个单一的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个片段的活动;一个头;身体;页脚(同一点在HTML)。该bodyfragment包含三个按钮,每个应更换中间片段(身体;本身)与一个又一个,但我无法弄清楚如何工作FragmentManager和FragmentTransition在这里了。我似乎无法找到其他民族问题的一致性在这里的问候他人实施其片段的方式。似乎每个人都有自己的方法,或者只是不包括完整的code在自己的线程。

MainActivity.java

 公共类MainActivity延伸活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    的setContentView(R.layout.test_frag);
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}

}

TestFragment.java

 公共类TestFragment扩展片段{@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){    返回inflater.inflate(R.layout.test_frag,集装箱,FALSE);
}

}

BodyFragment.java

 公共类BodyFragment扩展片段{@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){    返回inflater.inflate(R.layout.body_frag,集装箱,FALSE);
}

}

片段XML

 <片段
    机器人:ID =@ + ID / bodyfragment
    机器人:名字=com.example.test.BodyFragment
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_weight =1
    工具:布局=@布局/ body_frag/>

在XML BodyFragment布局(按钮X3)

 <按钮
    机器人:ID =@ + ID / bsettings
    机器人:layout_width =130dp
    机器人:layout_height =40dp
    机器人:layout_alignBaseline =@ + ID / bgames
    机器人:layout_alignBottom =@ + ID / bgames
    机器人:layout_toRightOf =@ + ID / bgames
    机器人:文字=设置/>


解决方案

您使用FragmentManager这样的:

  FragmentManager经理= getFragmentManager();
FragmentTransaction交易= manager.beginTransaction();
transaction.replace(R.id.bodyfragment,AnotherFragment.newInstance()); //的newInstance()是一个静态的工厂方法。
器transaction.commit();

这code将取代它坐落在观与该ID R.id.bodyfragment与另一个片段上的一个新的实例片段。

编辑:

要创建另一个片段的静态工厂方法应该是使用的新实例。你会实现它们在你的片段是这样的:

 公共类AnotherFragment扩展片段{    公共静态的newInstance AnotherFragment(){
        AnotherFragment片段=新AnotherFragment();
        ...
        //做一些初始设置如果需要的话,例如监听器等
        ...
        返回片段;
    }
    ...
}

I have an activity with 3 fragments; a header; a body; a footer (same point as in HTML). The bodyfragment contains three buttons which each should replace the middle fragment (body; itself) with another one, but I can't figure out how to work FragmentManager and FragmentTransition in here. I can't seem to find any coherency in other peoples questions on here with regards to the way others implement their fragments. It seems everyone has their own methods, or just doesn't include the full code in their threads.

MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.test_frag);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

TestFragment.java

public class TestFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.test_frag, container, false);
}

}

BodyFragment.java

public class BodyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.body_frag, container, false);
}

}

Fragment in XML

<fragment
    android:id="@+id/bodyfragment"
    android:name="com.example.test.BodyFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    tools:layout="@layout/body_frag" />

BodyFragment layout in XML (button x3)

    <Button
    android:id="@+id/bsettings"
    android:layout_width="130dp"
    android:layout_height="40dp"
    android:layout_alignBaseline="@+id/bgames"
    android:layout_alignBottom="@+id/bgames"
    android:layout_toRightOf="@+id/bgames"
    android:text="SETTINGS" />

解决方案

You use the FragmentManager like this:

FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.bodyfragment, AnotherFragment.newInstance()); // newInstance() is a static factory method.
transaction.commit();

This code would replace the Fragment which sits in the View with the id R.id.bodyfragment with a new Instance of another Fragment.

EDIT:

To create a new instance of another Fragment a static factory method is supposed to be used. You would implement them in your Fragment like this:

public class AnotherFragment extends Fragment {

    public static AnotherFragment newInstance() {
        AnotherFragment fragment = new AnotherFragment();
        ...
        // do some initial setup if needed, for example Listener etc
        ...
        return fragment;
    }
    ...
}

这篇关于我怎样才能实现FragmentManager和FragmentTransaction更换只是一个单一的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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