Android Api Demos登顶之路(四十一)Fragment-->Menu

来源:互联网 发布:pp2000软件 编辑:程序博客网 时间:2024/05/22 02:17

这个demo演示了如何在Fragment中设置菜单项
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="5dp" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <CheckBox         android:id="@+id/checkbox1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="show fragment1 menu"/>    <CheckBox         android:id="@+id/checkbox2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="show fragment2 menu"/>    <FrameLayout         android:id="@+id/frameLayout"        android:layout_width="match_parent"        android:layout_height="match_parent">    </FrameLayout></LinearLayout>

MainActivity

public class MainActivity extends Activity implements OnClickListener {    private CheckBox checkBox1;    private CheckBox checkBox2;    private MenuFragment1 fragment1;    private MenuFragment2 fragment2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        checkBox1=(CheckBox) findViewById(R.id.checkbox1);        checkBox2=(CheckBox) findViewById(R.id.checkbox2);        checkBox1.setOnClickListener(this);        checkBox2.setOnClickListener(this);        //准备fragment,将fragment添加进事务,等待处理        FragmentTransaction ft=getFragmentManager().beginTransaction();        fragment1=(MenuFragment1) getFragmentManager().findFragmentByTag("tag1");        if(fragment1==null){            fragment1=new MenuFragment1();            ft.add(fragment1, "tag1");        }        fragment2=(MenuFragment2) getFragmentManager().findFragmentByTag("tag2");        if(fragment2==null){            fragment2=new MenuFragment2();            ft.add(R.id.frameLayout,fragment2, "tag2");        }        ft.commit();        //更新复选框状态及显示        updateMenuVisiblity();    }    private void updateMenuVisiblity() {        FragmentTransaction ft=getFragmentManager().beginTransaction();        if(checkBox1.isChecked()){            ft.show(fragment1);        }else{            ft.hide(fragment1);        }        if(checkBox2.isChecked()){            ft.show(fragment2);        }else{            ft.hide(fragment2);        }        ft.commit();    }    //处理需要保存时的状态    @Override    protected void onRestoreInstanceState(Bundle savedInstanceState) {        super.onRestoreInstanceState(savedInstanceState);        updateMenuVisiblity();    }    @Override    public void onClick(View v) {        updateMenuVisiblity();    }    /*     * 定义用作菜单的fragment,这里没有实现onCreatView方法,所以这个     * fragment没有UI,只有两个菜单项。如果需要UI可以实现onCreatView方法     */    public class MenuFragment1 extends Fragment{        @Override        public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            //设置该fragment拥有操作菜单项            setHasOptionsMenu(true);        }        @Override        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {            super.onCreateOptionsMenu(menu, inflater);            //添加菜单,并设置显示模式            menu.add("Menu 1A").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);            menu.add("Menu 1B").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);        }    }    public class MenuFragment2 extends Fragment{        @Override        public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            //设置该fragment拥有操作菜单项            setHasOptionsMenu(true);        }        @Override        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {            super.onCreateOptionsMenu(menu, inflater);            //添加菜单,并设置显示模式            menu.add("Menu 2").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);        }        @Override        public View onCreateView(LayoutInflater inflater, ViewGroup container,                Bundle savedInstanceState) {            TextView tv=new TextView(getActivity());            tv.setText("这是Fragment2的UI");            tv.setTextColor(Color.WHITE);            tv.setBackgroundColor(Color.BLACK);            return tv;        }    }}
0 0