码迷,mamicode.com
首页 > 其他好文 > 详细

Fragment

时间:2016-09-13 01:38:04      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

// 创建一个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");
    }

 

Fragment

标签:

原文地址:http://www.cnblogs.com/shaoshao/p/5866928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!