Now you need to swap two controls (two relativelayout in this article) and find two methods:
1, using Layoutparams to change the properties of two layout, that is, its relative relationship (below, etc.), to achieve the position of the exchange, but there is no exchange of animation effect, is "instantaneous" exchange.
2, the use of animation exchange control position, I need to achieve the animation effect.
For example, Exchange Layoutone with Layouttwo.
First, the method of using Layoutparams is introduced.
<span style= "FONT-SIZE:18PX;" >package com.exchange;import com.exchange.r;import Android.app.activity;import android.os.Bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import android.widget.relativelayout;import android.widget.toast;/* * Exchange layout with Layoutparams * Author: [email protected] * DATE:2015/7/15 */public class Paramsexchangeactivity extends Activity {p Rivate Button btnex;private layoutinflater inflater;private relativelayout myfirst,mysecond,layoutone,layouttwo;// Set controls ' ID, the ID is a random as you like, does not use zeroprivate int btnexid = 11;private int Layoutoneid = 12;PR ivate int layouttwoid = 13;//exchange flag, in order to swap back and Forthprivate boolean tag_firstlayouttop; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.layout_main); btnex=(Button) Findviewbyid (R.id.button_exchange); Btnex.setonclicklistener (New Btnexonclicklistener ()); Inflater=getlayoutinflater (); Tag_firstlayouttop = true; init layoutone Myfirst = (relativelayout) inflater.inflate (R.layout.layout_first, null). FINDVIEWB Yid (R.id.myfirst); Layoutone = (relativelayout) Findviewbyid (R.id.layoutone); Layoutone.removeallviews (); Layoutone.addview (Myfirst); init layouttwo Mysecond = (relativelayout) inflater.inflate (R.layout.layout_second, null). Findvie Wbyid (R.id.mysecond); Layouttwo = (relativelayout) Findviewbyid (r.id.layouttwo); Layouttwo.removeallviews (); Layouttwo.addview (Mysecond); } public class Btnexonclicklistener implements Onclicklistener {@Override public void OnClick (View v) { Toast.maketext (Getbasecontext (), "exchange!", Toast.length_short). Show (); Set ID for their Params btnex.setid (BTNEXID); Layoutone.setid (Layoutoneid); Layouttwo.setid (layouttwoid); Relativelayout.layoutparams params; if (tag_firstlayouttop) {params = (relativelayout.layoutparams) layouttwo.getlayoutparams (); Params.removerule (Relativelayout.below);//remove the exist ' BELOW ' rule params.addrule (relativelayout.below,11);// Add a new one ' BELOW ' Rule,below control No. layouttwo.setlayoutparams (params); params = (relativelayout.layoutparams) layoutone.getlayoutparams (); Params.removerule (Relativelayout.below); Params.addrule (relativelayout.below,13);//below control NO. Layoutone.setlayoutparams (params); tag_firstlayouttop=false;//Change the flag}else{//vice versa params = (relativelayout.layoutparams) layoutone.g Etlayoutparams (); Params.removerule (Relativelayout.below); Params.addrule (relativelayout.below,11); Layoutone.setlayoutparams (params); params = (relativelayout.layoutparams) layouttwo.getlayoutparams (); Params.removerule (Relativelayout.below); Params.addrule (relativelayout.below,12); Layouttwo.setlayoutparams (params); Tag_firstlayouttop=true; }}}}</span>
Second, using the animation exchange control
The method of exchanging with animation is very simple:
Objectanimator.offloat (Layouttwo, "Translationy", -300). Setduration (+). Start (); Objectanimator.offloat (Layoutone, "Translationy"). Setduration (+). Start ();
The full code is as follows:
Package Com.exchange;import Android.animation.objectanimator;import Android.app.activity;import android.os.Bundle; Import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.relativelayout;import Android.widget.toast;public Class Animexchangeactivity extends Activity {private Button btnex;private layoutinflater inflater;private relativelayout Myfirst,mysecond,layoutone,layouttwo;private Boolean tag_firstlayouttop; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.layout_main); btnex= (Button) Findviewbyid (R.id.button_exchange); Btnex.setonclicklistener (New Btnexonclicklistener ()); Inflater=getlayoutinflater (); Tag_firstlayouttop = true; init layoutone Myfirst = (relativelayout) inflater.inflate (R.layout.layout_first, NULL). Findviewbyid (R.id.myfirst); Layoutone = (relativelayout) Findviewbyid (R.id.layoutone); Layoutone.removeallviews (); Layoutone.addview (Myfirst); init layouttwo Mysecond = (relativelayout) inflater.inflate (R.layout.layout_second, null). Findvie Wbyid (R.id.mysecond); Layouttwo = (relativelayout) Findviewbyid (r.id.layouttwo); Layouttwo.removeallviews (); Layouttwo.addview (Mysecond); } public class Btnexonclicklistener implements Onclicklistener {@Override public void OnClick (View v) { Toast.maketext (Getbasecontext (), "exchange!", Toast.length_short). Show (); if (tag_firstlayouttop) {//move upward and downward objectanimator.offloat (Layouttwo, "Translationy", -300). SetDu Ration (+). Start (); Objectanimator.offloat (Layoutone, "Translationy"). Setduration (+). Start (); Tag_firstlayouttop = false; }else{//back to normal position ObjectAnimator.offloat (Layoutone, "Translationy", 0). Setduration (+). Start (); Objectanimator.offloat (Layouttwo, "Translationy", 0). Setduration (+). Start (); Tag_firstlayouttop = true; } } }}
Source code Download Portal: Chain later.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android-Swap control location: Layoutparams-based instantaneous switching and objectanimator animation effect Exchange