我是靠谱客的博主 激昂毛衣,这篇文章主要介绍Fragment Arguments,现在分享给大家,希望可以做个参考。

一个Fragment可以通过两种方式进行配置,一个是Bundle类型参数,一个是layout中的属性。请看下面的例子:

1.主activity的布局文件

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3.   
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:orientation="vertical" android:padding="4dip"  
  6.     android:gravity="center_horizontal"  
  7.     android:layout_width="match_parent" android:layout_height="match_parent">  
  8.   
  9.     <TextView  
  10.             android:id="@+id/text"  
  11.             android:layout_width="wrap_content"  
  12.             android:layout_height="wrap_content"  
  13.             android:layout_weight="0"  
  14.             android:padding="4dip"  
  15.             android:layout_gravity="center_vertical|center_horizontal"  
  16.             android:gravity="top|center_horizontal"  
  17.             android:textAppearance="?android:attr/textAppearanceMedium"  
  18.             android:text="@string/fragment_arguments_msg" />  
  19.   
  20.     <LinearLayout android:orientation="horizontal" android:padding="4dip"  
  21.         android:layout_width="match_parent" android:layout_height="wrap_content">  
  22.   
  23.   
  24.         <fragment class="com.example.android.apis.app.FragmentArguments$MyFragment"  
  25.                 android:id="@+id/embedded"  
  26.                 android:layout_width="0px" android:layout_height="wrap_content"  
  27.                 android:layout_weight="1"  
  28.                 android:label="From Layout" />  
  29.   
  30.   
  31.         <FrameLayout  
  32.                 android:id="@+id/created"  
  33.                 android:layout_width="0px"  
  34.                 android:layout_height="wrap_content"  
  35.                 android:layout_weight="1" />  
  36.   
  37.     </LinearLayout>  
  38.   
  39. </LinearLayout>  
  40. </span>  
 

 

2.主Activity:

 

Java代码   收藏代码
  1. **  
  2.  * Demonstrates a fragment that can be configured through both Bundle arguments  
  3.  * and layout attributes.  
  4.  */  
  5. public class FragmentArguments extends Activity {  
  6.   
  7.     @Override protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.fragment_arguments);  
  10.   
  11.         if (savedInstanceState == null) {  
  12.             // First-time init; create fragment to embed in activity.  
  13.             FragmentTransaction ft = getFragmentManager().beginTransaction();  
  14.             Fragment newFragment = MyFragment.newInstance("From Arguments");  
  15.             ft.add(R.id.created, newFragment);  
  16.             ft.commit();  
  17.         }  
  18.     }  
  19.   
  20.   
  21.   
  22.     public static class MyFragment extends Fragment {  
  23.         CharSequence mLabel; 
  24.   
  25.         /** 
  26.          * Create a new instance of MyFragment that will be initialized 
  27.          * with the given arguments. 
  28.          */  
  29.         static MyFragment newInstance(CharSequence label) {  
  30.             MyFragment f = new MyFragment();  
  31.             Bundle b = new Bundle();  
  32.             b.putCharSequence("label", label);  
  33.             f.setArguments(b);  
  34.             return f;  
  35.         }  
  36.   
  37.         /** 
  38.          * Parse attributes during inflation from a view hierarchy into the 
  39.          * arguments we handle. 
  40.          */  
  41.         @Override 
  42.         public void onInflate(Activity activity, AttributeSet attrs,  
  43.                 Bundle savedInstanceState) {  
  44.             super.onInflate(activity, attrs, savedInstanceState);  
  45.   
  46.             TypedArray a = activity.obtainStyledAttributes(R.styleable.FragmentArguments);  
  47.             mLabel = a.getText(R.styleable.FragmentArguments_android_label);  
  48.             a.recycle();//不能忘记
  49.         }  
  50.   
  51.         /** 
  52.          * During creation, if arguments have been supplied to the fragment 
  53.          * then parse those out. 
  54.          */  
  55.         @Override 
  56. public void onCreate(Bundle savedInstanceState) {  
  57.             super.onCreate(savedInstanceState);  
  58.   
  59.             Bundle args = getArguments();  
  60.             if (args != null) {  
  61.                 mLabel = args.getCharSequence("label", mLabel);  
  62.             }  
  63.         }  
  64.   
  65.         /** 
  66.          * Create the view for this fragment, using the arguments given to it. 
  67.          */  
  68.         @Override 
  69. public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  70.                 Bundle savedInstanceState) {  
  71.             View v = inflater.inflate(R.layout.hello_world, container, false);  
  72.             View tv = v.findViewById(R.id.text);  
  73.             ((TextView)tv).setText(mLabel != null ? mLabel : "(no label)");  
  74.              
  75.             return v;  
  76.         }  
  77.     }  
  78.   
  79. }  

 

 

 3.MyFragment中的布局文件hello_world.xml

 

Xml代码   收藏代码
  1. <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"  
  2.     android:layout_width="match_parent" android:layout_height="match_parent"  
  3.     android:gravity="center_vertical|center_horizontal"  
  4.     android:textAppearance="?android:attr/textAppearanceMedium"  
  5.     android:text="hello_world"/>  
 
4.资源文件attr.xml
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.    
  3.   
  4. <resources>  
  5.   
  6.     <!-- These are the attributes that we want to retrieve for  
  7.          app/FragmentArguments.java -->  
  8.   
  9.     <declare-styleable name="FragmentArguments">  
  10.         <attr name="android:label" />  
  11.     </declare-styleable>  
  12.   
  13. </resources>  
 

 

总结:

这个示例展示了如何configure 一个Fragment, 其中展示了2种方法:
1. setArguments()方法

   这里要记住Fragment的Life cycle:   onAttach --- onCreate --- onCreateView --- onActivityCreated.

      首先, 在new 一个Fragment的同时设置Argument;
     然后,在这个Fragment类的onCreate中取出Argument,在onCreateView中对Argument进行设置。

2. 在view hierarchy的 inflation过程中解析出相关的attributes

以下是Fragment类说明中的一段话:

 

 The attributes of the <fragment> tag are used to control the LayoutParams provided when attaching the fragment's view to the parent container. They can also be parsed by the fragment in onInflate(Activity, AttributeSet, Bundle) as parameters. 


本例中定义了一个新的属性:android:label.
定义新属性的方法--- 在res/values/attr.xml中添加

<declare-styleable name="FragmentArguments">
       <attr name="android:label" />
</declare-styleable>

之后在layout xml文件中使用这个属性, 

复制代码
1
2
3
4
5

<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" />


这样就可以在onInflate()方法中把属性值取出来,在onCreateView中进行处理了。

       

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

 

         
       /**
* 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();//不要忘记
        }

 

最后

以上就是激昂毛衣最近收集整理的关于Fragment Arguments的全部内容,更多相关Fragment内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部