Fragment

// 创建一个Fragment->   PlaceholderFragment

public class PlaceholderFragment extends Fragment { // android.support.v4.app.Fragment;

    public PlaceholderFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragement_main, container, false);

        rootView.findViewById(R.id.btnShowAnotherFrament).setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
         // 启动另外一个 Fragment getFragmentManager().beginTransaction() .addToBackStack(
null) // 添加后退栈 不然的后退的话没有效果 .replace(R.id.container, new AnotherFragment()) // 在这个容器里面替换 新的Fragment .commit(); } }); rootView.findViewById(R.id.btnStartSliderActivity).setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { getFragmentManager().popBackStack(); } }); return rootView; } }

PlaceholderFragment对应的视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity$PlaceholderFragment">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="呈现另一个Fragement"
        android:id="@+id/btnShowAnotherFrament" />
</LinearLayout>

 

 

// 2. 从主 MainActivity中加载PlaceholderFragment

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        System.out.println("onCreate");
    }

 

posted on 2016-09-13 00:02  邵邵  阅读(221)  评论(0编辑  收藏  举报

淘宝美工兼职招聘