06Android learning from zero single row of fragment
This is the last part of Android Basic learning, after this part of the learning is completed, will not be updated in the "Android learning from zero single row" series blog, in this section after learning to complete, I will also start learning Android project writing.
Fragment Getting Started
- Add 2 fragment layouts to the layout file
<fragment android:name= " Com.example.xwf.hsia06fragmentdemo.LeftFragment " android:id = "@+id/left" //must add ID, otherwise error Android:layout_width= "0DP" android:layout_weight= "1" android:layout_he Ight= "match_parent" /> <fragment android:name= "C Om.example.xwf.hsia06fragmentdemo.RightFragment " android:id = "@+id/right" android:layout_width= "0DP" android:layou T_weight= "1" android:layout_height= "match_parent"
/>
- Then write the 2 fragment class files specified in the 2 layout types.
publicclass LeftFragment extends Fragment { @Nullable @Override publiconCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { null); return view; }}
Fragment Dynamic Additions
- Write 2 fragment files.
- Get fragmenttransaction transactions through Getfragmentmanager in activity.
- Replacing fragment layouts with fragmenttransaction
- Commits the transaction.
protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_main);Dynamically loading the fragment, dynamically loading the WindowManager WM = (WindowManager) getsystemservice (window_service) via the screen's portrait screen;Point point = new Point ();Wm. Getdefaultdisplay(). GetSize(point);Intx= Point. x;Inty= Point. Y;Fragmentmanager fm = Getfragmentmanager ();Fragmenttransaction fragmenttransaction = FM. BeginTransaction();if (x<y) {Fragmenttransaction. Replace(Android. R. ID. Content, new Verticalfragment ());}else{fragmenttransaction. Replace(Android. R. ID. Content, new Horizontalfragment ());}//COMMIT TRANSACTION Fragmenttransaction. Commit();}
Fragment Imitation page
It is important to note that the transaction needs to be re-acquired in the onclick.
protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_main);Butterknife. Butterknife. Bind(this);Btnwechat. Setonclicklistener(this);Btncontact. Setonclicklistener(this);Btnfriend. Setonclicklistener(this);Btninformation. Setonclicklistener(this);fm = Getfragmentmanager ();The default is Page FM. BeginTransaction(). Replace(R. ID. ll_replace,new wechatfragment ()). Commit();} @Override public void OnClick (View v) {//requires a re-acquisition of transaction in the onclick fragmenttransaction FT = FM. BeginTransaction();Switch (v. GetId()) {case R. ID. BTN_wechat:ft. Replace(R. ID. ll_replace,new wechatfragment ()); Break;Case R. ID. BTN_contact:ft. Replace(R. ID. ll_replace,new contactfragment ()); Break;Case R. ID. BTN_friend:ft. Replace(R. ID. ll_replace,new friendfragment ()); Break;Case R. ID. BTN_information:ft. Replace(R. ID. ll_replace,new informationfragment ()); Break;} ft. Commit();}
Communication between fragment
The
is used primarily by Getfragmentmanager (). Findfragmentbytag ("Leftfragment") to get to another fragment object and invoke the Inside method.
public View oncreateview (Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {LinearLayout layout = (linearlayout) inflater.inflate (r.layout.fragment_right, null); Button btn = (button) Layout.findviewbyid (R.ID.BTN); Btn.setonclicklistener (new View.onclicklistener () { @Override public void onclick (View v) {leftfragment lf = (leftfragment) Getfragmentmanager (). findfragment Bytag ( "leftfragment" ); Lf.settext ( "data changed! "); } }); return layout; }
Android Animation Collection
-Create a new Anim folder under Res and place some animation files under the file.
-Load various animation resource files through Animationutils in activity.
public void OnClick (view view) {switch (view. GetId()) {case R. ID. BTN1:animation A1 = Animationutils. Loadanimation(Getapplicationcontext (), R. Anim. Alpha);iv. Startanimation(A1); Break;Case R. ID. BTN2:animation A2 = Animationutils. Loadanimation(Getapplicationcontext (), R. Anim. Rotate);iv. Startanimation(A2); Break;Case R. ID. BTN3:animation A3 = Animationutils. Loadanimation(Getapplicationcontext (), R. Anim. scale);iv. Startanimation(A3); Break;Case R. ID. BTN4:animation A4 = animationutils. Loadanimation(Getapplicationcontext (), R. Anim. Translate);iv. Startanimation(A4); Break;Case R. ID. BTN_set:animationSet= Animationutils. Loadanimation(Getapplicationcontext (), R. Anim. Set);iv. Startanimation(Set); Break;} }
Learning to use Autocompletetextview controls
The control is primarily a blur hint, and according to your initials, it will automatically prompt you for what you may need to enter. According to the development document, COP one copy.
Private Static Finalstring[] Countries =NewString[] {"Belgium","France","Italy","Germany","Spain"};@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Autocompletetextview ACTV = (autocompletetextview) Findviewbyid (R.ID.ACTV); arrayadapter<string> adapter =NewArrayadapter<string> ( ThisAndroid. R.layout.simple_dropdown_item_1line, countries); Actv.setadapter (adapter); }
about the author
-Personal website: Beijing Internet Technology
-Email:[email protected]
-Project Address: Https://github.com/swordman20/Hsia06FragmentDemo.git
06Android learning from zero single row of fragment