Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 中級開發 >> Fragment示例代碼

Fragment示例代碼

編輯:中級開發

 Android 3.0平板系統的重要特性Fragment示例代碼今天android123給大家兩個例子,一起來看下Fragment的實戰代碼:

  一、通過Fragment實現簡單的上下文菜單

  public class FragmentContextMenu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         ContextMenuFragment content = new ContextMenuFragment();
         getFragmentManager().beginTransaction().add(android.R.id.content, content).commit(); //在Activity中通過這個與Fragment通訊
    }

   下面ContextMenuFragment是我們從Fragment派生的子類,裡面重寫了onCreateVIEw來布局自己的Fragment

    public static class ContextMenuFragment extends Fragment {

        @Override
        public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container,
                Bundle savedInstanceState) {
            VIEw root = inflater.inflate(R.layout.fragment_context_menu, container, false);
            registerForContextMenu(root.findVIEwById(R.id.long_press));
            return root;
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, VIEw v, ContextMenuInfo menuInfo) {
            super.onCreateContextMenu(menu, v, menuInfo);
            menu.add(Menu.NONE, R.id.a_item, Menu.NONE, "菜單1");
            menu.add(Menu.NONE, R.id.b_item, Menu.NONE, "菜單2");
        }

        @Override
        public boolean onContextItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.a_item:
                    Log.i("CWJ", "Item 1a was chosen");
                    return true;
                case R.id.b_item:
                    Log.i("CWJ", "Item 1b was chosen");
                    return true;
            }
            return super.onContextItemSelected(item);
        }
    }
}

 涉及到的布局文件fragment_context_menu.XML源碼

  <?XML version="1.0" encoding="utf-8"?>

<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
    android:orIEntation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <TextVIEw
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/fragment_context_menu_msg" />

    <Button android:id="@+id/long_press"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/long_press">
        <requestFocus />
    </Button>
   
</LinearLayout>

  二、控制Fragment的顯示或隱藏同時包含淡入淡出效果

  public class FragmentHideShow extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentVIEw(R.layout.fragment_hide_show);

        FragmentManager fm = getFragmentManager();
        addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
        addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));

        //上面的兩行代碼是在Activity中為Fragment中的按鈕添加監聽事件
    }

    void addShowHideListener(int buttonId, final Fragment fragment) {
        final Button button = (Button)findVIEwById(buttonId);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(VIEw v) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.setCustomAnimations(android.R.animator.fade_in,
                        android.R.animator.fade_out); //為Fragment設置淡入淡出效果,Android開發網提示這裡這兩個動畫資源是android內部資源無需我們手動定義。
                if (fragment.isHidden()) {
                    ft.show(fragment);
                    button.setText("隱藏");
                } else {
                    ft.hide(fragment);
                    button.setText("顯示");
                }
                ft.commit();
            }
        });
    }

    public static class FirstFragment extends Fragment {
        TextView mTextVIEw;

        @Override
        public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container,
                Bundle savedInstanceState) {
            VIEw v = inflater.inflate(R.layout.labeled_text_edit, container, false);
            View tv = v.findVIEwById(R.id.msg);
            ((TextVIEw)tv).setText("The fragment saves and restores this text.");

            mTextView = (TextView)v.findVIEwById(R.id.saved);
            if (savedInstanceState != null) {
                mTextVIEw.setText(savedInstanceState.getCharSequence("text"));
            }
            return v;
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);

            outState.putCharSequence("text", mTextVIEw.getText());
        }
    }

    public static class SecondFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, VIEwGroup container,
                Bundle savedInstanceState) {
            VIEw v = inflater.inflate(R.layout.labeled_text_edit, container, false);
            View tv = v.findVIEwById(R.id.msg);
            ((TextView)tv).setText("The TextVIEw saves and restores this text.");

            ((TextView)v.findVIEwById(R.id.saved)).setSaveEnabled(true);
            return v;
        }
    }
}

 涉及資源布局文件fragment_hide_show.XML代碼:

  <?XML version="1.0" encoding="utf-8"?>

<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
    android:orIEntation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextVIEw android:layout_width="match_parent" android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Demonstration of hiding and showing fragments." />

    <LinearLayout android:orIEntation="horizontal" android:padding="4dip"
        android:gravity="center_vertical" android:layout_weight="1"
        android:layout_width="match_parent" android:layout_height="wrap_content">

        <Button android:id="@+id/frag1hide"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="Hide" />

        <fragment android:name="com.android123.FragmentHideShow$FirstFragment"
                android:id="@+id/fragment1" android:layout_weight="1"
                android:layout_width="0px" android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout android:orIEntation="horizontal" android:padding="4dip"
        android:gravity="center_vertical" android:layout_weight="1"
        android:layout_width="match_parent" android:layout_height="wrap_content">

        <Button android:id="@+id/frag2hide"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="Hide" />

        <fragment android:name="com.android123.FragmentHideShow$SecondFragment"
                android:id="@+id/fragment2" android:layout_weight="1"
                android:layout_width="0px" android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

 有關布局文件labeled_text_edit.XML的代碼為

<?XML version="1.0" encoding="utf-8"?>

<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
    android:orIEntation="vertical" android:padding="4dip"
    android:layout_width="match_parent" android:layout_height="wrap_content">

    <TextVIEw android:id="@+id/msg"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip" />

    <EditText android:id="@+id/saved"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/green"
        android:text="@string/initial_text"
        android:freezesText="true">
        <requestFocus />
    </EditText>

</LinearLayout>

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved