CoordinatorLayout
协调者:一般会是两个控件,一个Dependency一个child ,CoordinatorLayout的主要功能就是协调这两个控件,使child跟随Dependency的布局变化而变化(比如:位置,大小等)。其中变化的规则,则是由一个CoordinatorLayout.Behavior来决定。demo:
一:布局
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.kxl.mydemo.view.CoordinaterDependencyView android:id="@+id/coor_layout" android:layout_width="60dp" android:text="Dependency" android:textColor="#000000" android:layout_height="60dp" android:background="#55ff55" /> <Button android:layout_width="100dp" android:layout_height="50dp" android:background="#ff5555" android:text="child" app:layout_behavior="com.kxl.mydemo.coordinator.MoveBehavior" /> </android.support.design.widget.CoordinatorLayout>
二。自定义的一个,可移动的view:com.kxl.mydemo.view.CoordinaterDependencyView
import android.content.Context; import android.support.design.widget.CoordinatorLayout; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * Created by kxl on 2016/10/18. */ public class CoordinaterDependencyView extends TextView { private String TAG = "CoordinaterDependencyView"; private int width; private int height; int lastx; int lasty; public CoordinaterDependencyView(Context context) { super(context); DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); int displaywidth = displayMetrics.widthPixels; int displayheight = displayMetrics.heightPixels; Log.i(TAG,"displaywidth:"+displaywidth+" displayheight:"+displayheight); } public CoordinaterDependencyView(Context context, AttributeSet attrs) { super(context, attrs); width = getMeasuredWidth(); height = getMeasuredHeight(); } @Override public boolean onTouchEvent(MotionEvent event) { int x = (int)event.getRawX(); int y = (int)event.getRawY(); Log.i(TAG,"onTouchEvent x:"+x+" y:"+y); switch (event.getAction()){ case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams(); int nx = layoutParams.leftMargin+x-lastx; int ny = layoutParams.topMargin+y-lasty; layoutParams.leftMargin = nx; layoutParams.topMargin = ny; setLayoutParams(layoutParams); requestLayout(); break; case MotionEvent.ACTION_UP: break; default: break; } lastx = x; lasty = y; return true; } }
三:协调规则。Behavior:
import android.content.Context; import android.support.design.widget.CoordinatorLayout; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.View; import android.view.ViewGroup; import com.kxl.mydemo.view.CoordinaterDependencyView; /** * Created by kxl on 2016/10/18. */ public class MoveBehavior extends CoordinatorLayout.Behavior<View> { int width; public MoveBehavior(Context context, AttributeSet attrs) { super(context, attrs); DisplayMetrics display = context.getResources().getDisplayMetrics(); width = display.widthPixels; } /** * @return 返回是否是child依赖的布局 */ @Override public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) { return dependency instanceof CoordinaterDependencyView; } /** * dependency控件有变化时,会调用这个方法 */ @Override public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { ViewGroup.MarginLayoutParams pa = (ViewGroup.MarginLayoutParams) child.getLayoutParams(); pa.leftMargin = width - dependency.getLeft(); pa.topMargin = dependency.getTop(); child.setLayoutParams(pa); return true; } }