New API demo showing arguments to fragments.
Change-Id: I5bc41bb23b7ac6a6d7cbbd08cec67879dc4ae2fe
This commit is contained in:
@@ -256,6 +256,15 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity android:name=".app.FragmentArguments"
|
||||
android:label="@string/fragment_arguments"
|
||||
android:enabled="@bool/atLeastHoneycomb">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.SAMPLE_CODE" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity android:name=".app.FragmentHideShow"
|
||||
android:label="@string/fragment_hide_show"
|
||||
android:windowSoftInputMode="stateUnchanged"
|
||||
|
||||
57
samples/ApiDemos/res/layout-land/fragment_arguments.xml
Normal file
57
samples/ApiDemos/res/layout-land/fragment_arguments.xml
Normal file
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (C) 2011 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.
|
||||
-->
|
||||
|
||||
<!-- Top-level content view for the simple fragment sample. -->
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical" android:padding="4dip"
|
||||
android:gravity="center_horizontal"
|
||||
android:layout_width="match_parent" android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="0"
|
||||
android:padding="4dip"
|
||||
android:layout_gravity="center_vertical|center_horizontal"
|
||||
android:gravity="top|center_horizontal"
|
||||
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||
android:text="@string/fragment_arguments_msg" />
|
||||
|
||||
<LinearLayout android:orientation="horizontal" android:padding="4dip"
|
||||
android:layout_width="match_parent" android:layout_height="wrap_content">
|
||||
|
||||
<fragment class="com.example.android.apis.app.FragmentArguments$MyFragment"
|
||||
android:id="@+id/embedded"
|
||||
android:layout_width="0px" android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:label="@string/fragment_arguments_embedded" />
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/created"
|
||||
android:layout_width="0px"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<fragment class="com.example.android.apis.app.FragmentArguments$MyFragment"
|
||||
android:id="@+id/embedded_land"
|
||||
android:layout_width="match_parent" android:layout_height="wrap_content"
|
||||
android:label="@string/fragment_arguments_embedded_land" />
|
||||
|
||||
</LinearLayout>
|
||||
54
samples/ApiDemos/res/layout/fragment_arguments.xml
Normal file
54
samples/ApiDemos/res/layout/fragment_arguments.xml
Normal file
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (C) 2011 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.
|
||||
-->
|
||||
|
||||
<!-- Top-level content view for the simple fragment sample. -->
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical" android:padding="4dip"
|
||||
android:gravity="center_horizontal"
|
||||
android:layout_width="match_parent" android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="0"
|
||||
android:padding="4dip"
|
||||
android:layout_gravity="center_vertical|center_horizontal"
|
||||
android:gravity="top|center_horizontal"
|
||||
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||
android:text="@string/fragment_arguments_msg" />
|
||||
|
||||
<LinearLayout android:orientation="horizontal" android:padding="4dip"
|
||||
android:layout_width="match_parent" android:layout_height="wrap_content">
|
||||
|
||||
<!-- BEGIN_INCLUDE(from_attributes) -->
|
||||
<fragment class="com.example.android.apis.app.FragmentArguments$MyFragment"
|
||||
android:id="@+id/embedded"
|
||||
android:layout_width="0px" android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:label="@string/fragment_arguments_embedded" />
|
||||
<!-- END_INCLUDE(from_attributes) -->
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/created"
|
||||
android:layout_width="0px"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -44,4 +44,12 @@
|
||||
<enum name="drop" value="2" />
|
||||
</attr>
|
||||
</declare-styleable>
|
||||
|
||||
<!-- These are the attributes that we want to retrieve for
|
||||
app/FragmentArguments.java -->
|
||||
<!-- BEGIN_INCLUDE(fragment_arguments) -->
|
||||
<declare-styleable name="FragmentArguments">
|
||||
<attr name="android:label" />
|
||||
</declare-styleable>
|
||||
<!-- END_INCLUDE(fragment_arguments) -->
|
||||
</resources>
|
||||
|
||||
@@ -108,6 +108,12 @@
|
||||
|
||||
<string name="fragment_alert_dialog">App/Fragment/Alert Dialog</string>
|
||||
|
||||
<string name="fragment_arguments">App/Fragment/Arguments</string>
|
||||
<string name="fragment_arguments_msg">Demonstrates a fragment that takes arguments
|
||||
as a Bundle at runtime (on the right) or from attributes in a layout (on the left).</string>
|
||||
<string name="fragment_arguments_embedded">From Attributes</string>
|
||||
<string name="fragment_arguments_embedded_land">Landscape Only</string>
|
||||
|
||||
<string name="fragment_hide_show">App/Fragment/Hide and Show</string>
|
||||
|
||||
<string name="fragment_context_menu">App/Fragment/Context Menu</string>
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* 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.app.FragmentTransaction;
|
||||
import android.content.res.TypedArray;
|
||||
import android.os.Bundle;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* Demonstrates a fragment that can be configured through both Bundle arguments
|
||||
* and layout attributes.
|
||||
*/
|
||||
public class FragmentArguments extends Activity {
|
||||
//BEGIN_INCLUDE(create)
|
||||
@Override protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.fragment_arguments);
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
// First-time init; create fragment to embed in activity.
|
||||
FragmentTransaction ft = getFragmentManager().beginTransaction();
|
||||
Fragment newFragment = MyFragment.newInstance("From Arguments");
|
||||
ft.add(R.id.created, newFragment);
|
||||
ft.commit();
|
||||
}
|
||||
}
|
||||
//END_INCLUDE(create)
|
||||
|
||||
//BEGIN_INCLUDE(fragment)
|
||||
public static class MyFragment extends Fragment {
|
||||
CharSequence mLabel;
|
||||
|
||||
/**
|
||||
* Create a new instance of MyFragment that will be initialized
|
||||
* with the given arguments.
|
||||
*/
|
||||
static MyFragment newInstance(CharSequence label) {
|
||||
MyFragment f = new MyFragment();
|
||||
Bundle b = new Bundle();
|
||||
b.putCharSequence("label", label);
|
||||
f.setArguments(b);
|
||||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse attributes during inflation from a view hierarchy into the
|
||||
* arguments we handle.
|
||||
*/
|
||||
@Override public void onInflate(Activity activity, AttributeSet attrs,
|
||||
Bundle savedInstanceState) {
|
||||
super.onInflate(activity, attrs, savedInstanceState);
|
||||
|
||||
TypedArray a = activity.obtainStyledAttributes(attrs,
|
||||
R.styleable.FragmentArguments);
|
||||
mLabel = a.getText(R.styleable.FragmentArguments_android_label);
|
||||
a.recycle();
|
||||
}
|
||||
|
||||
/**
|
||||
* During creation, if arguments have been supplied to the fragment
|
||||
* then parse those out.
|
||||
*/
|
||||
@Override public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
Bundle args = getArguments();
|
||||
if (args != null) {
|
||||
mLabel = args.getCharSequence("label", mLabel);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the view for this fragment, using the arguments given to it.
|
||||
*/
|
||||
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View v = inflater.inflate(R.layout.hello_world, container, false);
|
||||
View tv = v.findViewById(R.id.text);
|
||||
((TextView)tv).setText(mLabel != null ? mLabel : "(no label)");
|
||||
tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
|
||||
return v;
|
||||
}
|
||||
}
|
||||
//END_INCLUDE(fragment)
|
||||
}
|
||||
Reference in New Issue
Block a user