OSDN Git Service

New fragment sample code for ListFragment and context menus.
authorDianne Hackborn <hackbod@google.com>
Sat, 12 Jun 2010 17:17:58 +0000 (10:17 -0700)
committerDianne Hackborn <hackbod@google.com>
Sat, 12 Jun 2010 17:17:58 +0000 (10:17 -0700)
Change-Id: I97c64fa4c4140c81c7d0147026ba558ba1a76d2e

samples/ApiDemos/AndroidManifest.xml
samples/ApiDemos/res/layout/fragment_context_menu.xml [new file with mode: 0644]
samples/ApiDemos/res/values/strings.xml
samples/ApiDemos/src/com/example/android/apis/app/FragmentContextMenu.java [new file with mode: 0644]
samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
samples/ApiDemos/src/com/example/android/apis/app/FragmentList.java [new file with mode: 0644]
samples/ApiDemos/src/com/example/android/apis/app/FragmentMenu.java

index 614ccfe..3e22eb5 100644 (file)
 
         <!-- Fragment Samples -->
 
-        <activity android:name=".app.FragmentStack" android:label="@string/fragment_stack">
+        <activity android:name=".app.FragmentAnim" android:label="@string/fragment_anim">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
+        <activity android:name=".app.FragmentContextMenu"
+                android:label="@string/fragment_context_menu">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.SAMPLE_CODE" />
 
         <activity android:name=".app.FragmentLayout$DialogActivity" />
 
-        <activity android:name=".app.FragmentAnim" android:label="@string/fragment_anim">
+        <activity android:name=".app.FragmentList" android:label="@string/fragment_list">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
+        <activity android:name=".app.FragmentMenu" android:label="@string/fragment_menu">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.SAMPLE_CODE" />
             </intent-filter>
         </activity>
 
-        <activity android:name=".app.FragmentMenu" android:label="@string/fragment_menu">
+        <activity android:name=".app.FragmentStack" android:label="@string/fragment_stack">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.SAMPLE_CODE" />
diff --git a/samples/ApiDemos/res/layout/fragment_context_menu.xml b/samples/ApiDemos/res/layout/fragment_context_menu.xml
new file mode 100644 (file)
index 0000000..8f2c044
--- /dev/null
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+  
+          http://www.apache.org/licenses/LICENSE-2.0
+  
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<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: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>
index 9d09846..016ca18 100644 (file)
     <string name="redirect_getter">Enter the text that will be used by the main activity.  Press back to cancel.</string>
     <string name="apply">Apply</string>
 
-    <string name="fragment_stack">App/Fragment/Stack</string>
-    <string name="next">Next</string>
-
+    <string name="fragment_anim">App/Fragment/Anim</string>
+    
+    <string name="fragment_context_menu">App/Fragment/Context Menu</string>
+    <string name="fragment_context_menu_msg">Fragment populating a context
+            menu; long press the button to see.</string>
+    <string name="long_press">Long press me</string>
+    
     <string name="fragment_layout">App/Fragment/Layout</string>
     
-    <string name="fragment_anim">App/Fragment/Anim</string>
+    <string name="fragment_list">App/Fragment/List</string>
+    
+    <string name="fragment_menu">App/Fragment/Menu</string>
+    <string name="fragment_menu_msg">Build menus from two fragments, allowing
+        you to hide them to remove them..</string>
+    <string name="fragment1menu">Show fragment 1 menu</string>
+    <string name="fragment2menu">Show fragment 2 menu</string>
     
     <string name="fragment_retain_instance">App/Fragment/Retain Instance</string>
     <string name="fragment_retain_instance_msg">Current progress of retained fragment;
 
     <string name="fragment_receive_result">App/Fragment/Receive Result</string>
     
-    <string name="fragment_menu">App/Fragment/Menu</string>
-    <string name="fragment_menu_msg">Build menus from two fragments, allowing
-        you to hide them to remove them..</string>
-    <string name="fragment1menu">Show fragment 1 menu</string>
-    <string name="fragment2menu">Show fragment 2 menu</string>
-    
+    <string name="fragment_stack">App/Fragment/Stack</string>
+    <string name="next">Next</string>
+
     <string name="activity_menu">App/Activity/Menu</string>
     <string name="open_menu">Open menu</string>
     <string name="close_menu">Close menu</string>
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentContextMenu.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentContextMenu.java
new file mode 100644 (file)
index 0000000..3587dba
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.apis.app;
+
+import com.example.android.apis.R;
+
+import android.app.Activity;
+import android.app.Fragment;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.ContextMenu;
+import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.ContextMenu.ContextMenuInfo;
+
+/**
+ * Demonstration of displaying a context menu from a fragment.
+ */
+public class FragmentContextMenu extends Activity {
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        
+        // Create the list fragment and add it as our sole content.
+        ContextMenuFragment content = new ContextMenuFragment();
+        openFragmentTransaction().add(android.R.id.content, content).commit();
+    }
+    
+    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, "Menu A");
+            menu.add(Menu.NONE, R.id.b_item, Menu.NONE, "Menu B");
+        }
+        
+        @Override
+        public boolean onContextItemSelected(MenuItem item) {
+            switch (item.getItemId()) {
+                case R.id.a_item:
+                    Log.i("ContextMenu", "Item 1a was chosen");
+                    return true;
+                case R.id.b_item:
+                    Log.i("ContextMenu", "Item 1b was chosen");
+                    return true;
+            }
+            return super.onContextItemSelected(item);
+        }
+    }
+}
index 71cae8a..c802310 100644 (file)
@@ -46,9 +46,15 @@ public class FragmentLayout extends Activity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        
+        // This layout varies depending on the screen size.
         setContentView(R.layout.fragment_layout);
     }
     
+    /**
+     * This is a secondary activity, to show what the user has selected
+     * when the screen is not large enough to show it all in one activity.
+     */
     public static class DialogActivity extends Activity {
         @Override
         protected void onCreate(Bundle savedInstanceState) {
@@ -63,7 +69,7 @@ public class FragmentLayout extends Activity {
             }
             
             DialogFragment dialog = new DialogFragment();
-            this.openFragmentTransaction().add(android.R.id.content, dialog).commit();
+            openFragmentTransaction().add(android.R.id.content, dialog).commit();
             dialog.setText(getIntent().getIntExtra("text", -1));
         }
     }
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentList.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentList.java
new file mode 100644 (file)
index 0000000..976bcc1
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.apis.app;
+
+import com.example.android.apis.R;
+import com.example.android.apis.Shakespeare;
+
+import android.app.Activity;
+import android.app.ListFragment;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
+
+/**
+ * Demonstration of using ListFragment to show a list of items.
+ */
+public class FragmentList extends Activity {
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        
+        // Create the list fragment and add it as our sole content.
+        ExampleListFragment list = new ExampleListFragment();
+        openFragmentTransaction().add(android.R.id.content, list).commit();
+    }
+    
+    public static class ExampleListFragment extends ListFragment {
+        @Override
+        public void onReady(Bundle savedInstanceState) {
+            super.onReady(savedInstanceState);
+            setListAdapter(new ArrayAdapter<String>(getActivity(),
+                    android.R.layout.simple_list_item_1, Shakespeare.TITLES));
+        }
+        
+        @Override
+        public void onListItemClick(ListView l, View v, int position, long id) {
+            Log.i("FragmentList", "Item clicked: " + id);
+        }
+    }
+}
index bed1517..6116953 100644 (file)
@@ -41,7 +41,7 @@ public class FragmentMenu extends Activity {
         FragmentTransaction ft = openFragmentTransaction();
         mFragment1 = findFragmentByTag("f1");
         if (mFragment1 == null) {
-            mFragment1 = new Menu1Fragment();
+            mFragment1 = new MenuFragment();
             ft.add(mFragment1, "f1");
         }
         mFragment2 = findFragmentByTag("f2");
@@ -83,7 +83,7 @@ public class FragmentMenu extends Activity {
      * have a UI (it does not implement onCreateView), but it could also
      * have one if it wanted.
      */
-    public static class Menu1Fragment extends Fragment {
+    public static class MenuFragment extends Fragment {
         @Override
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);