Requirement: Enter activity, initiate network request, show activity, hide loading page after request data is successfully obtained, display load page if request is unsuccessful.
Scenario: The most understandable is to add a full-screen view with the layout, the request after the success of the hidden. But in this case, each page has to be modified layout more trouble,
So now with the dynamic Code scheme, encapsulated into a method, each call can be.
The code is as follows:
Layout:
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout
xmlns:android= "http://schemas.android.com/apk /res/android "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android: background= "@color/j" >
<textview
android:layout_width= "match_parent"
android:layout_height= " 160DP "
android:background=" @color/b "
android:gravity=" center "
android:text=" "/>
</ Relativelayout>
To load a page layout:
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout
xmlns:android= "http://schemas.android.com/apk /res/android "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android: Background= "#F9000000"
>
<progressbar
android:layout_width= "wrap_content"
android:layout _height= "Wrap_content"
android:layout_centerinparent= "true"/>
<textview
android:id= "@+id/t1 "
android:layout_width=" 100DP "
android:layout_height=" 100DP "
android:layout_centerhorizontal=" True "
android:layout_margintop=" 120DP "
android:background=" @color/b "
android:gravity=" center "
android:text= "1"/>
</RelativeLayout>
Activity code:
public class Efbactivity extends Appcompatactivity {private Context mcontext; Handler Mhandler = new Handler () {@Override public void Handlemessage (Message msg) {Super.ha
Ndlemessage (msg);
Mview.setvisibility (View.gone);
}
};
View MView;
@Override protected void OnCreate (@Nullable Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.E_FB);
Mcontext = this;
MView = Createv (efbactivity.this);
Operation Mhandler.sendemptymessagedelayed (1, 3000) after successful simulation of data loading; }/** * Dynamically created ProgressBar * *@paramA@return */Public View Createv (activity a) {//1. Find the viewgroup of the activity root, the type is framelayout. Framelayout Rootcontainer = (framelayout) A.findviewbyid (Android. r.id.content);//fixed notation, return to root view//2. The location where the initialization control is displayed framelayout.layoutparams LP = new Framelayout.layoutparams (Frame
Layout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT);
lp.gravity = Gravity.center;
3. Set control display Location final view view = View.inflate (Mcontext, r.layout.e_fb_dialog, NULL);
TextView T1 = (TextView) View.findviewbyid (R.ID.T1);
T1.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {
Set the click after Hide Load page view.setvisibility (view.gone);
}
});
View.setvisibility (view.visible);
4. Add the control to the root node under Rootcontainer.addview (view);
return view; }
}