{"id":985,"date":"2015-08-18T17:40:56","date_gmt":"2015-08-18T09:40:56","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=985"},"modified":"2015-08-18T17:42:15","modified_gmt":"2015-08-18T09:42:15","slug":"fragment%e6%a0%88","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=985","title":{"rendered":"Fragment\u6808"},"content":{"rendered":"<p>\u6211\u4eec\u77e5\u9053Android\u7cfb\u7edf\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4efb\u52a1\u6808\u7528\u4e8e\u4fdd\u5b58Activity\uff0c\u540c\u6837\u4e5f\u4e3aFragment\u63d0\u4f9b\u4e86\u4e00\u4e2a\u56de\u9000\u6808\uff0c\u5982\u679c\u6211\u4eec\u628aFragment\u538b\u5165\u56de\u9000\u6808\u540e\u70b9\u51fb\u8fd4\u56de\u53ef\u4ee5\u663e\u793a\u4e0a\u4e00\u4e2aFragment.<br \/>\n<!--more--><br \/>\n\u770b\u4e0bAPIDemo\u4e0b\u7684\u5b9e\u4f8b<br \/>\n<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/08\/fragment_stack.gif\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-986\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/08\/fragment_stack.gif\" alt=\"fragment_stack\" width=\"322\" height=\"533\" \/><\/a><\/p>\n<p>\u5e03\u5c40fragment_stack.xml<\/p>\n<p>[java]&lt;LinearLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;<br \/>\n    android:layout_width=&quot;match_parent&quot;<br \/>\n    android:layout_height=&quot;match_parent&quot;<br \/>\n    android:gravity=&quot;center_horizontal&quot;<br \/>\n    android:orientation=&quot;vertical&quot;<br \/>\n    android:padding=&quot;4dip&quot; &gt;<\/p>\n<p>    &lt;LinearLayout<br \/>\n        android:layout_width=&quot;wrap_content&quot;<br \/>\n        android:layout_height=&quot;wrap_content&quot;<br \/>\n        android:layout_weight=&quot;0&quot;<br \/>\n        android:gravity=&quot;center_horizontal&quot;<br \/>\n        android:measureWithLargestChild=&quot;true&quot;<br \/>\n        android:orientation=&quot;horizontal&quot;<br \/>\n        android:padding=&quot;4dip&quot; &gt;<\/p>\n<p>        &lt;Button<br \/>\n            android:id=&quot;@+id\/new_fragment&quot;<br \/>\n            android:layout_width=&quot;wrap_content&quot;<br \/>\n            android:layout_height=&quot;wrap_content&quot;<br \/>\n            android:text=&quot;Push&quot; &gt;<\/p>\n<p>            &lt;requestFocus \/&gt;<br \/>\n        &lt;\/Button&gt;<\/p>\n<p>        &lt;Button<br \/>\n            android:id=&quot;@+id\/delete_fragment&quot;<br \/>\n            android:layout_width=&quot;wrap_content&quot;<br \/>\n            android:layout_height=&quot;wrap_content&quot;<br \/>\n            android:layout_weight=&quot;0&quot;<br \/>\n            android:text=&quot;Pop&quot; &gt;<br \/>\n        &lt;\/Button&gt;<br \/>\n    &lt;\/LinearLayout&gt;<\/p>\n<p>    &lt;FrameLayout<br \/>\n        android:id=&quot;@+id\/simple_fragment&quot;<br \/>\n        android:layout_width=&quot;match_parent&quot;<br \/>\n        android:layout_height=&quot;0px&quot;<br \/>\n        android:layout_weight=&quot;1&quot; &gt;<br \/>\n    &lt;\/FrameLayout&gt;<\/p>\n<p>&lt;\/LinearLayout&gt;[\/java]<\/p>\n<p>\u4ee3\u7801:<\/p>\n<p>[java]public class FragmentStack extends Activity {<br \/>\n\tint mStackLevel = 0;<\/p>\n<p>\t@Override<br \/>\n\tprotected void onCreate(Bundle savedInstanceState) {<br \/>\n\t\tsuper.onCreate(savedInstanceState);<br \/>\n\t\tsetContentView(R.layout.fragment_stack);<\/p>\n<p>\t\tButton button = (Button) findViewById(R.id.new_fragment);<br \/>\n\t\tbutton.setOnClickListener(new OnClickListener() {<br \/>\n\t\t\tpublic void onClick(View v) {<br \/>\n\t\t\t\tmStackLevel++;<br \/>\n\t\t\t\taddFragmentToStack();<br \/>\n\t\t\t}<br \/>\n\t\t});<br \/>\n\t\tbutton = (Button) findViewById(R.id.delete_fragment);<br \/>\n\t\tbutton.setOnClickListener(new OnClickListener() {<br \/>\n\t\t\tpublic void onClick(View v) {<br \/>\n\t\t\t\tmStackLevel&#8211;;<br \/>\n\t\t\t\tgetFragmentManager().popBackStack(); \/\/\u5f39\u6808<br \/>\n\t\t\t}<br \/>\n\t\t});<br \/>\n\t}<\/p>\n<p>\tvoid addFragmentToStack() {<br \/>\n        Fragment newFragment = CountingFragment.newInstance(mStackLevel);<br \/>\n        FragmentTransaction ft = getFragmentManager().beginTransaction();<br \/>\n        ft.replace(R.id.simple_fragment, newFragment);<br \/>\n        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);<br \/>\n        ft.addToBackStack(null);<br \/>\n        ft.commit();<br \/>\n\t}<\/p>\n<p>\tpublic static class CountingFragment extends Fragment {<br \/>\n\t\tint mNum;<br \/>\n\t\tprivate static int[] sColorValue = new int[] {<br \/>\n\t\t\t\tandroid.R.color.holo_blue_light,<br \/>\n\t\t\t\tandroid.R.color.holo_green_light,<br \/>\n\t\t\t\tandroid.R.color.holo_orange_light,<br \/>\n\t\t\t\tandroid.R.color.holo_red_light };<\/p>\n<p>\t\t\/**<br \/>\n\t\t * Create a new instance of CountingFragment, providing &quot;num&quot; as an<br \/>\n\t\t * argument.<br \/>\n\t\t *\/<br \/>\n\t\tstatic CountingFragment newInstance(int num) {<br \/>\n\t\t\tCountingFragment f = new CountingFragment();<\/p>\n<p>\t\t\t\/\/ Supply num input as an argument.<br \/>\n\t\t\tBundle args = new Bundle();<br \/>\n\t\t\targs.putInt(&quot;num&quot;, num);<br \/>\n\t\t\tf.setArguments(args);<br \/>\n\t\t\treturn f;<br \/>\n\t\t}<\/p>\n<p>\t\t\/**<br \/>\n\t\t * When creating, retrieve this instance&#8217;s number from its arguments.<br \/>\n\t\t *\/<br \/>\n\t\t@Override<br \/>\n\t\tpublic void onCreate(Bundle savedInstanceState) {<br \/>\n\t\t\tsuper.onCreate(savedInstanceState);<br \/>\n\t\t\tmNum = getArguments() != null ? getArguments().getInt(&quot;num&quot;) : 1;<br \/>\n\t\t}<\/p>\n<p>\t\t\/**<br \/>\n\t\t * The Fragment&#8217;s UI is just a simple text view showing its instance<br \/>\n\t\t * number.<br \/>\n\t\t *\/<br \/>\n\t\t@Override<br \/>\n\t\tpublic View onCreateView(LayoutInflater inflater, ViewGroup container,<br \/>\n\t\t\t\tBundle savedInstanceState) {<br \/>\n\t\t\tView v = inflater.inflate(R.layout.fragment_stack_layout, container, false);<br \/>\n\t\t\tView tv = v.findViewById(R.id.text);<br \/>\n\t\t\t((TextView) tv).setText(&quot;Fragment #&quot; + mNum);<br \/>\n\t\t\ttv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));<br \/>\n\t\t\tLog.v(&quot;TAG&quot;, &quot;mNum % 3 &quot; + mNum % 3);<br \/>\n\t\t\ttv.setBackgroundColor(getActivity().getResources().getColor(sColorValue[mNum % 3]));<br \/>\n\t\t\treturn v;<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p>\u538b\u6808\u4ee3\u7801<\/p>\n<p>[java]FragmentTransaction ft = getFragmentManager().beginTransaction();<br \/>\nft.addToBackStack(null);[\/java]<\/p>\n<p>\u5f39\u6808\u4ee3\u7801<\/p>\n<p>[java]getFragmentManager().popBackStack();[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u77e5\u9053Android\u7cfb\u7edf\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4efb\u52a1\u6808\u7528\u4e8e\u4fdd\u5b58Activity\uff0c\u540c\u6837\u4e5f\u4e3aFragment\u63d0\u4f9b\u4e86\u4e00\u4e2a\u56de\u9000\u6808\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[67],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/985"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=985"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/985\/revisions"}],"predecessor-version":[{"id":989,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/985\/revisions\/989"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}