Before the mobile phone after the 2 days of summary, but also from the new grasp a lot of knowledge before forgotten, deepened the impression of knowledge, the following is a new project, mall app, useful to a lot of old knowledge, of course, there are a lot of new knowledge waiting for me to challenge and learn.
Code hosting to the code cloud, interested can go to download to see
Https://git.oschina.net/joy_yuan/ShoppingMall
1. Create Loading interface
Loading interface is a simple layout of activity, in the activity of the OnCreate method, the use of handler send a delay of 2 seconds of intent, jump to the main activity, note after the jump, finish () Drop the activity.
Note that there is a Ontouchevent callback event, which is the function of our previous project phone audio and video, in the loading interface touch the screen, immediately jump to the main page.
Package com.yuanlp.shoppingmall.activity;import android.content.intent;import android.os.bundle ;import android.os.handler;import android.support.v7.app.appcompatactivity;import android.view.motionevent;import com.yuanlp.shoppingmall.r;public class splashactivity extends appcompatactivity {; @Override protected Void oncreate (bundle savedinstancestate) { Super.oncreate (savedinstancestate); setcontentview ( R.layout.activity_splash); new handler (). postDelayed (new Runnable () { @Override public void run () { &nBsp; starttoactivity (); } }, 2000); } private void starttoactivity () { intent intent=new intent (Splashactivity.this,mainactivity.class); this.startactivity (Intent); Finish (); } @Override public boolean ontouchevent (motionevent event) { Starttoactivity (); return super.ontouchevent (event); } @Override protected void ondestroy () { super.ondestroy (); }}
The activity that needs to be registered in Androidmanifext.xml at this time is splashactivity
<activity android:name= ". Activity. Splashactivity "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <c Ategory android:name= "Android.intent.category.LAUNCHER"/> </intent-filter></activity>
And we touch the screen, immediately jump, if not to do processing, then the handler of the 2-second delay will also jump to mainactivity, equivalent to open 2. In this case, the startup mode for Mainactivity is Singletask, that is, in the task stack, there will only be one activity.
<activity android:name= ". Activity. Mainactivity "android:launchmode=" Singletask "></activity>
2, Mainactivity
A layout file
This layout is similar to the previous project mobile phone audio and video, are the outermost is a linearlayout, vertical direction of typesetting.
There are 2 sub-layouts in the LinearLayout, the above is Framelayout, and the following is a radiogroup.
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= " Http://schemas.android.com/apk/res/android " xmlns:app=" http://schemas.android.com/apk/ Res-auto " xmlns:tools=" Http://schemas.android.com/tools " android : layout_width= "match_parent" android:layout_height= "Match_parent" android:background= "#ffffff" android:orientation= "vertical" tools:context= "Com.yuanlp.shoppingmall.activity.MainActivity" > <framelayout android:id= "@+id/framelayout" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android: layout_weight= "1"/>&nbsP; <radiogroup android:id= "@+id/rg_main" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" Android:layout_alignparentbottom= "true" android:background= "@ DRAWABLE/HOME_BOTTOM_PARENT_BG " android:orientation=" Horizontal "> <RadioButton android:id= "@+id/rb_home" android:text= "Home" android:drawabletop= "@ Drawable/home_button_selector " style=" @style/mainbuttonstyle " &nbsP; android:layout_width= "Match_parent" android : layout_height= "Match_parent"/> <radiobutton android:id= "@+id/rb_type" android:text= "category" android:drawabletop= "@drawable/type_button_selector" style= "@style/mainbuttonstyle" android:layout_width= "Match_parent" android:layout_height= "Match_parent"/> <RadioButton android:id= "@+id/rb_community " android:text=" "Discovery" android:drawabletop= "@drawable/community_button_ Selector " style=" @style/mainbuttonstyle " android:layout_width=" Match_parent " android:layout_height= "Match_parent"/ > <RadioButton android:id= "@+id/rb_cart" android:text= "Shopping Cart" android:drawabletop= "@drawable/cart_button_selector" style= "@stYle/mainbuttonstyle " android:layout_ Width= "Match_parent" android:layout_ height= "Match_parent"/> <radiobutton android:id= "@+id/rb_user" android:text= "Shopping Cart" android:drawabletop= "@drawable/user_button_selector" style= "@style/mainbuttonstyle" android:layout_width= "Match_parent" android:layout_height= "Match_parent"/></radiogroup></ Linearlayout>
b, in the activity, if you want a manual to instantiate the control, more trouble, if there is more complex layout, then manual writing is very tired, you need to use the Butterknife plug-in.
In Build.gradle, add the following, AYSN can join
Compile ' com.jakewharton:butterknife:8.7.0 ' annotationprocessor ' com.jakewharton:butterknife-compiler:8.7.0 '
Then you can use it in mainactivity, and by default the home page is selected first RadioButton
package com.yuanlp.shoppingmall.activity;import android.os.bundle;import android.support.v4.app.fragmentactivity;import android.widget.framelayout;import Android.widget.radiobutton;import android.widget.radiogroup;import com.yuanlp.shoppingmall.r;import butterknife. Bindview;import butterknife. butterknife;public class mainactivity extends fragmentactivity { @BindView (r.id.framelayout) FrameLayout mFramelayout; @BindView (r.id.rb_home) radiobutton mrbhome; @ BindView (R.id.rb_type) RadioButton mRbType; @BindView ( r.id.rb_community) RadioButton mRbCommunity; @BindView ( R.id.rb_cart) RadioButton mRbCart; @BindView (R.id.rb_user) radiobutton mrbuser; @BindView (R.id.rg_main) RadioGroup mrgmain; @Override protected void oncreate (Bundle savedinstancestate) { super.oncreate ( Savedinstancestate); setcontentview (R.layout.activity_main); //binding butterknife and activity, instantiating controls butterknife.bind (this); mrgmain.check (R.id.rb _home); }}
3 You need to write a basefragment because you want to switch the fragment above by RadioButton the bottom of the selection.
Comparing the life cycle of activity and fragment
As the picture is blurred, you can refer to the blog: http://blog.csdn.net/forever_crying/article/details/8238863/
The custom fragment inherits fragment, which must be guided by the V4 bag.
Custom fragment at least 3 methods to rewrite
1, OnCreate sub-class to use the context, need to get here
2. Oncreateview here is where the page UI is displayed, showing the return view
3. onactivitycreated here is the callback after the activity is created, where you can initialize the custom view to get the data in the fragment
package com.yuanlp.shoppingmall.base;import android.content.context;import android.os.bundle; import android.support.annotation.nullable;import android.support.v4.app.fragment;import android.view.layoutinflater;import android.view.view;import android.view.viewgroup;/** * Basic Fragement, a few other pages to inherit this base class * inherit fragment must rewrite at least 3 methods, * 1, oncreate Get Context * 2, oncreateview show UI layout * 3, onactivitycreated Initialize various spaces, get data, etc. */public abstract class basefragement extends fragment { protected Context context; /** * oncreate is the creation of the fragment, similar to Activity.oncreate, in which you can initialize something other than view; * @param savedInstanceState */ @Override public void&nBsp;oncreate (@Nullable bundle savedinstancestate) { super.oncreate (savedinstancestate); context=getactivity () ; } /** * Oncreateview is to create the corresponding view of the fragment, and you must create your own view here and return it to the caller. Responsible for UI creation display * @param inflater * @param container * @param savedInstanceState * @return */ @Override Public view oncreateview (layoutinflater inflater, viewgroup container, bundle savedinstancestate) { &nbsP; return initview (); / /If this code is in the home page, Initview, return this View to show this layout // view view = view.inflate (mcontext, r.layout.fragment_home, null); } /** * force subclasses to implement him, let's show this layout in Oncreateview. * @return */ public abstract view initview (); /** * When the activity is fully started, the callback is initialized here to initialize the custom view. Here is responsible for data acquisition * @param savedInstanceState */ @Override public void onactivitycreated (@ Nullable bundle savedinstancestate) { Super.onactivitycreatEd (savedinstancestate); initdata (); } /** * when a subclass needs to be networked to get data, you can override the method */ public void initdata () { }} With an explanation of the difference between oncreateview and onactivityview : android development-fragment Oncreateview () The difference between the onactivitycreated () and the ① static view does not require onactivitycreated ② you need to use Onactivitycreated ③ to access the view layer of the parent activity when you save the view state, you need to onactivitycreated Method inside do that if the view is static, then there is no need to call in the onactivitycreated method, most of the custom view, A context is required for initialization, and activity is a subclass of the context, so a non-static view initialization call may be an exception when the Oncreateview method is present, so for non-static view, It is best to call the Onactivitycreated method
This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1953464
Mobile mall first day loading interface, the main interface and the creation of basefragment, the use of Bufferknife