Show activity after Network load is complete

Source: Internet
Author: User
Tags xmlns

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; }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.