[Android] Core Method of interface switch, android Switch
Based on the split Interface
View ==> ViewGroup ==> RelativeLayout. The subject uses RelativeLayout as the placeholder.
Difference between View and ViewGroup: ViewGroup has the unique addView () and removeView () methods. After RelativeLayout is added to the View, the latter will overwrite the previous View. After LinearLayout is added to the View, will be discharged sequentially, not suitable
Design principle:
Data processing in the tree structure follows the combined design mode.
Locate the RelativeLayout object of the subject and add the View object.
Switch the second interface two seconds later, and use Handler to send delayed messages to simulate Switching
RelativeLayout object. Add the second View object.
Problem:
MainActivity. java
Private Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {loadMemberView () ;};/*** initialize the subject */public void initMiddle () {mainContainer = (RelativeLayout) findViewById (R. id. il_main); loadLoginView (); handler. sendEmptyMessageDelayed (1, 2000);}/*** load login interface */private void loadLoginView () {View child = new IndexView (this ). getLoginView (); mainContainer. addView (child);}/*** load member interface */private void loadMemberView () {View child = new IndexView (this ). getMemberView (); mainContainer. removeAllViews (); mainContainer. addView (child );}
IndexView. java
Package com. tsh. lottery. views; import android. content. context; import android. view. view; import android. widget. textView;/*** interface * @ author taoshihan **/public class IndexView {private Context context; public IndexView (Context context) {this. context = context;}/*** get login interface * @ return */public View getLoginView () {TextView TV = new TextView (context); TV. setText ("My login interface"); return TV;}/*** get Member interface * @ return */public View getMemberView () {TextView TV = new TextView (context); TV. setText ("I am a Member interface"); return TV ;}}