一饼疙、概念
CoordinatorLayout 的類繼承關(guān)系:
CoordinatorLayout extends ViewGroup implements NestedScrollingParent2,
NestedScrollingParent3
主要作用:
- 作為頂層布局庸追;
- 作為協(xié)調(diào)子 View 之間交互的容器。
二群嗤、使用
1.CoordinatorLayout 與 FloatingActionButton
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/contentView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/anchorView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/anchorView"
app:layout_anchorGravity="bottom|right"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:onClick="onClick"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "zwm, onCreate, Target30");
setContentView(R.layout.activity_main);
}
public void onClick(View v) {
Log.d(TAG, "zwm, onClick");
switch (v.getId()) {
case R.id.fab:
Snackbar.make(findViewById(R.id.contentView), "Hello, Android!", Snackbar.LENGTH_SHORT).show();
break;
}
}
}
日志打印:
2020-09-17 21:15:00.882 9490-9490/com.tomorrow.target30 D/MainActivity: zwm, onCreate, Target30
2020-09-17 21:15:02.823 9490-9490/com.tomorrow.target30 D/MainActivity: zwm, onClick
CoordinatorLayout 提供了兩個屬性用來設(shè)置 FloatingActionButton 的位置:
- layout_anchor:設(shè)置 FloatingActionButton 的錨點兵琳。
- layout_anchorGravity:設(shè)置 FloatingActionButton 相對錨點的位置狂秘。
當(dāng) Snackbar 顯示和隱藏的時候,CoordinatorLayout 會動態(tài)調(diào)整 FloatingActionButton 的位置躯肌。
2.CoordinatorLayout 與 AppBarLayout
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center"
android:textSize="30sp"
android:textColor="@android:color/white"
android:text="Header"
android:background="#0000FF"
app:layout_scrollFlags="scroll"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
RecyclerViewAdapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter {
private List<RecyclerViewBean> mData;
private LayoutInflater mInflater;
public RecyclerViewAdapter(Context context, List<RecyclerViewBean> data) {
this.mData = data;
this.mInflater = LayoutInflater.from(context);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
switch (viewType) {
case RecyclerViewBean.TYPE_ITEM:
return new ContentHolder(mInflater.inflate(R.layout.recyclerview_content_layout, viewGroup, false));
case RecyclerViewBean.TYPE_TITLE:
return new TitleHolder(mInflater.inflate(R.layout.recyclerview_title_layout, viewGroup, false));
default:
return null;
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
RecyclerViewBean infoBean = mData.get(i);
int viewType = getItemViewType(i);
switch (viewType) {
case RecyclerViewBean.TYPE_ITEM:
ContentHolder contentHolder = (ContentHolder) viewHolder;
contentHolder.tvTitle.setText(infoBean.title);
contentHolder.tvContent.setText(infoBean.content);
break;
case RecyclerViewBean.TYPE_TITLE:
TitleHolder titleHolder = (TitleHolder) viewHolder;
titleHolder.tvTitle.setText(infoBean.title);
break;
default:
break;
}
}
@Override
public int getItemCount() {
return mData == null ? 0 : mData.size();
}
@Override
public int getItemViewType(int position) {
return mData.get(position).type;
}
public static class TitleHolder extends RecyclerView.ViewHolder {
TextView tvTitle;
TitleHolder(@NonNull View itemView) {
super(itemView);
tvTitle = itemView.findViewById(R.id.tv_title);
}
}
public static class ContentHolder extends RecyclerView.ViewHolder {
TextView tvTitle;
TextView tvContent;
ContentHolder(@NonNull View itemView) {
super(itemView);
tvTitle = itemView.findViewById(R.id.tv_title);
tvContent = itemView.findViewById(R.id.tv_content);
}
}
}
RecyclerViewBean:
public class RecyclerViewBean {
public static final int TYPE_TITLE = 1;
public static final int TYPE_ITEM = 2;
public int type;
public String title;
public String content;
}
recyclerview_title_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="#63BB0B"
android:layout_height="40dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#000000"
android:text="評論"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
recyclerview_content_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="wrap_content">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textColor="#000000"
android:textSize="16dp"
android:text="評論"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textColor="#000000"
android:textSize="16dp"
android:text="內(nèi)容"
app:layout_constraintTop_toBottomOf="@+id/tv_title"
app:layout_constraintLeft_toLeftOf="parent" />
<View
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_content"
android:layout_marginTop="5dp"
android:layout_width="0dp"
android:layout_height="5dp"
android:background="#686868"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "zwm, onCreate, Target30");
setContentView(R.layout.activity_main);
initRecyclerView();
}
private void initRecyclerView() {
mRecyclerView = findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
List<RecyclerViewBean> data = getCommentData();
RecyclerViewAdapter rvAdapter = new RecyclerViewAdapter(this, data);
mRecyclerView.setAdapter(rvAdapter);
}
private List<RecyclerViewBean> getCommentData() {
List<RecyclerViewBean> commentList = new ArrayList<>();
RecyclerViewBean titleBean = new RecyclerViewBean();
titleBean.type = RecyclerViewBean.TYPE_TITLE;
titleBean.title = "評論列表";
commentList.add(titleBean);
for (int i = 0; i < 40; i++) {
RecyclerViewBean contentBean = new RecyclerViewBean();
contentBean.type = RecyclerViewBean.TYPE_ITEM;
contentBean.title = "評論標(biāo)題" + i;
contentBean.content = "評論內(nèi)容" + i;
commentList.add(contentBean);
}
return commentList;
}
}
-
CoordinatorLayout 中可滾動的 View(如上例中 RecyclerView)者春,需要設(shè)置以下屬性:
app:layout_behavior="@string/appbar_scrolling_view_behavior"
-
AppBarLayout 中的 View(如上例中 TextView),如要想要滾動到屏幕外清女,必須設(shè)置以下屬性:
app:layout_scrollFlags="scroll"
scroll 的效果為:隱藏的時候钱烟,先整體向上滾動,直到 AppBarLayout 完全隱藏嫡丙,再開始滾動 Scrolling View拴袭;顯示的時候,直到 Scrolling View 頂部完全出現(xiàn)后曙博,再開始滾動 AppBarLayout 到完全顯示拥刻。
除了 scroll,還有下面幾個取值父泳,這些屬性都必須與 scroll 一起使用 "|" 運算符:
-
enterAlways:與 scroll 類似(scroll|enterAlways)般哼,只不過向下滾動先讓 AppBarLayout 完全顯示,再滾動 Scrolling View:
activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:gravity="center" android:textSize="30sp" android:textColor="@android:color/white" android:text="Header" android:background="#0000FF" app:layout_scrollFlags="scroll|enterAlways"/> </com.google.android.material.appbar.AppBarLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </androidx.coordinatorlayout.widget.CoordinatorLayout>
-
enterAlwaysCollapsed:需要和 enterAlways 一起使用(scroll|enterAlways|enterAlwaysCollapsed)惠窄,和 enterAlways 不一樣的是逝她,不會在 AppBarLayout 完全顯示后才滾動 Scrolling View,而是先滾動 AppBarLayout 到最小高度睬捶,然后滾動 Scrolling View 直到頂部完全出現(xiàn)黔宛,最后再滾動 AppBarLayout 到完全顯示。注意:需要定義 View 的最小高度(minHeight)才有效果:
activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:gravity="center" android:textSize="30sp" android:textColor="@android:color/white" android:text="Header" android:background="#0000FF" android:minHeight="20dp" app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"/> </com.google.android.material.appbar.AppBarLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </androidx.coordinatorlayout.widget.CoordinatorLayout>
-
exitUntilCollapsed:定義了 AppBarLayout 消失的規(guī)則。發(fā)生向上滾動事件時臀晃,AppBarLayout 向上滾動退出直至最小高度(minHeight)觉渴,然后 Scrolling View 開始滾動。也就是 AppBarLayout 不會完全退出屏幕:
activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:gravity="center" android:textSize="30sp" android:textColor="@android:color/white" android:text="Header" android:background="#0000FF" android:minHeight="20dp" app:layout_scrollFlags="scroll|exitUntilCollapsed"/> </com.google.android.material.appbar.AppBarLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </androidx.coordinatorlayout.widget.CoordinatorLayout>
enterAlwaysCollapsed 與 exitUntilCollapsed 在實際的使用中徽惋,更多的是與 CollapsingToolbarLayout 一起使用案淋。
3.CoordinatorLayout 與 CollapsingToolbarLayout
CollapsingToolbarLayout 繼承自 FrameLayout,它是用來實現(xiàn) Toolbar 的折疊效果险绘,一般它的直接子 View 是 Toolbar踢京,當(dāng)然也可以是其它類型的 View:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#0000FF"
app:layout_scrollFlags="scroll|enterAlways">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_collapseMode="pin"
app:navigationIcon="@mipmap/ic_launcher_round"
app:title="Hello, Android!"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
- app:contentScrim:表示 CollapsingToolbarLayout 折疊之后的“前景色”,我們看到 Toolbar 的變色宦棺,其實是因為前景色遮擋了而已瓣距。
- app:layout_scrollFlags="scroll|enterAlways" 效果為:向下滾動先讓 AppBarLayout 完全顯示,再滾動 Scrolling View代咸。
- app:layout_collapseMode="pin" 確保 CollapsingToolbarLayout 折疊完成之前蹈丸,Toolbar 一直固定在頂部不動。
使用 app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed" 及 app:layout_collapseMode="pin"呐芥,讓 Toolbar 一直固定在頂部不動:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#0000FF"
app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_collapseMode="pin"
app:title="Hello, Android!"
app:navigationIcon="@mipmap/ic_launcher"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
使用 app:layout_collapseMode="parallax"逻杖、app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed" 及 app:layout_collapseMode="pin",產(chǎn)生視差效果:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#0000FF"
app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image"
android:scaleType="centerCrop"
app:layout_collapseParallaxMultiplier="0.9"
app:layout_collapseMode="parallax"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_collapseMode="pin"
app:title="Hello, Android!"
app:navigationIcon="@mipmap/ic_launcher"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
三思瘟、原理
CoordinatorLayout 的核心是 Behavior荸百,Behavior 提供了二十多個空方法給使用者來重寫,主要分為四類:
- 與 Touch 事件相關(guān)的方法
- 與 NestedScroll 相關(guān)的方法
- 與控件依賴相關(guān)的方法
- 其他方法滨攻,如測量和布局等
重要概念:
- child够话,是一個 View,是該 Behavior 所要操作的對象
- dependency铡买,也是一個 View,是 child 的依賴對象霎箍,也是該 Behavior 對 child 進行操作的根據(jù)
重要方法:
-
layoutDependsOn
public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull V child, @NonNull View dependency)
它會被 Behavior 的 LayoutParams 的 dependsOn 方法調(diào)用:
boolean dependsOn(CoordinatorLayout parent, View child, View dependency) { return dependency == mAnchorDirectChild || shouldDodge(dependency, ViewCompat.getLayoutDirection(parent)) || (mBehavior != null && mBehavior.layoutDependsOn(parent, child, dependency)); }
dependsOn 方法就是用來確定依賴關(guān)系的奇钞。最簡單的確定依賴關(guān)系的方法是重寫 layoutDependsOn 方法,并在一定條件下返回 true 即可漂坏。
-
onDependentViewChanged
public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull V child, @NonNull View dependency)
當(dāng) dependency 發(fā)生改變的時候景埃,這個方法會調(diào)用。
自定義簡單的 Behavior:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#0000FF"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:text="child"
app:layout_behavior="com.tomorrow.target30.MyBehavior" />
<com.tomorrow.target30.TempView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#CC88CC"
android:gravity="center"
android:textColor="#FFFFFF"
android:text="dependency"
android:clickable="true"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MyBehavior:
public class MyBehavior extends CoordinatorLayout.Behavior<Button> {
private static final String TAG = MyBehavior.class.getSimpleName();
private int mWidth;
public MyBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
DisplayMetrics display = context.getResources().getDisplayMetrics();
mWidth = display.widthPixels;
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, Button child, View dependency) {
Log.d(TAG, "zwm, layoutDependsOn, parent: " + parent + ", child: " + child + ", dependency: " + dependency);
//如果dependency是TempView的實例顶别,說明它就是我們所需要的Dependency
return dependency instanceof TempView;
}
//每次dependency位置發(fā)生變化谷徙,都會執(zhí)行onDependentViewChanged方法
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, Button btn, View dependency) {
Log.d(TAG, "zwm, onDependentViewChanged, parent: " + parent + ", btn: " + btn + ", dependency: " + dependency);
//根據(jù)dependency的位置,設(shè)置Button的位置
int top = dependency.getTop();
int left = dependency.getLeft();
int x = mWidth - left - btn.getWidth();
int y = top;
setPosition(btn, x, y);
return true;
}
private void setPosition(View v, int x, int y) {
CoordinatorLayout.MarginLayoutParams layoutParams = (CoordinatorLayout.MarginLayoutParams) v.getLayoutParams();
layoutParams.leftMargin = x;
layoutParams.topMargin = y;
v.setLayoutParams(layoutParams);
}
}
TempView:
public class TempView extends TextView {
private static final String TAG = TempView.class.getSimpleName();
private int mTouchStartX;
private int mTouchStartY;
public TempView(Context context) {
super(context);
}
public TempView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "zwm, onTouchEvent action: " + event.getAction());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mTouchStartX = (int) event.getX();
mTouchStartY = (int) event.getY();
Log.d(TAG, "zwm, mTouchStartX: " + mTouchStartX + ", mTouchStartY: " + mTouchStartY);
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getX() - mTouchStartX;
int dy = (int) event.getY() - mTouchStartY;
Log.d(TAG, "zwm, dx: " + dx + ", dy: " + dy);
setPosition(dx, dy);
break;
case MotionEvent.ACTION_UP:
break;
}
return super.onTouchEvent(event);
}
private void setPosition(int dx, int dy) {
CoordinatorLayout.MarginLayoutParams layoutParams = (CoordinatorLayout.MarginLayoutParams) getLayoutParams();
layoutParams.leftMargin += dx;
layoutParams.topMargin += dy;
setLayoutParams(layoutParams);
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "zwm, onCreate, Target30");
setContentView(R.layout.activity_main);
}
}
日志打友币铩:
2020-09-21 12:14:29.337 32265-32265/com.tomorrow.target30 D/MainActivity: zwm, onCreate, Target30
2020-09-21 12:14:29.379 32265-32265/com.tomorrow.target30 D/MyBehavior: zwm, layoutDependsOn, parent: androidx.coordinatorlayout.widget.CoordinatorLayout{88636b V.E...... ......I. 0,0-0,0}, child: androidx.appcompat.widget.AppCompatButton{7dd18ba VFED..C.. ......ID 0,0-0,0 #7f080057 app:id/btn}, dependency: com.tomorrow.target30.TempView{81f1ec8 VFED..C.. ......ID 0,0-0,0}
2020-09-21 12:14:29.397 32265-32265/com.tomorrow.target30 D/MyBehavior: zwm, layoutDependsOn, parent: androidx.coordinatorlayout.widget.CoordinatorLayout{88636b V.E...... ......I. 0,0-0,0}, child: androidx.appcompat.widget.AppCompatButton{7dd18ba VFED..C.. ......ID 0,0-0,0 #7f080057 app:id/btn}, dependency: com.tomorrow.target30.TempView{81f1ec8 VFED..C.. ......ID 0,0-0,0}
2020-09-21 12:14:29.398 32265-32265/com.tomorrow.target30 D/MyBehavior: zwm, layoutDependsOn, parent: androidx.coordinatorlayout.widget.CoordinatorLayout{88636b V.E...... ......ID 0,0-1080,2232}, child: androidx.appcompat.widget.AppCompatButton{7dd18ba VFED..C.. ......ID 0,0-264,150 #7f080057 app:id/btn}, dependency: com.tomorrow.target30.TempView{81f1ec8 VFED..C.. ......ID 0,0-226,150}
2020-09-21 12:14:29.398 32265-32265/com.tomorrow.target30 D/MyBehavior: zwm, onDependentViewChanged, parent: androidx.coordinatorlayout.widget.CoordinatorLayout{88636b V.E...... ......ID 0,0-1080,2232}, btn: androidx.appcompat.widget.AppCompatButton{7dd18ba VFED..C.. ......ID 0,0-264,150 #7f080057 app:id/btn}, dependency: com.tomorrow.target30.TempView{81f1ec8 VFED..C.. ......ID 0,0-226,150}
2020-09-21 12:14:29.408 32265-32265/com.tomorrow.target30 D/MyBehavior: zwm, layoutDependsOn, parent: androidx.coordinatorlayout.widget.CoordinatorLayout{88636b V.E...... ........ 0,0-1080,2232}, child: androidx.appcompat.widget.AppCompatButton{7dd18ba VFED..C.. ........ 0,0-264,150 #7f080057 app:id/btn}, dependency: com.tomorrow.target30.TempView{81f1ec8 VFED..C.. ........ 0,0-226,150}
2020-09-21 12:14:33.048 32265-32265/com.tomorrow.target30 D/TempView: zwm, onTouchEvent action: 0
2020-09-21 12:14:33.048 32265-32265/com.tomorrow.target30 D/TempView: zwm, mTouchStartX: 160, mTouchStartY: 126
2020-09-21 12:14:33.093 32265-32265/com.tomorrow.target30 D/TempView: zwm, onTouchEvent action: 2
2020-09-21 12:14:33.093 32265-32265/com.tomorrow.target30 D/TempView: zwm, dx: 1, dy: 0
2020-09-21 12:14:33.095 32265-32265/com.tomorrow.target30 D/MyBehavior: zwm, layoutDependsOn, parent: androidx.coordinatorlayout.widget.CoordinatorLayout{88636b V.E...... ......I. 0,0-1080,2232}, child: androidx.appcompat.widget.AppCompatButton{7dd18ba VFED..C.. ........ 816,0-1080,150 #7f080057 app:id/btn}, dependency: com.tomorrow.target30.TempView{81f1ec8 VFED..C.. ...p..I. 0,0-226,150}
2020-09-21 12:14:33.097 32265-32265/com.tomorrow.target30 D/MyBehavior: zwm, layoutDependsOn, parent: androidx.coordinatorlayout.widget.CoordinatorLayout{88636b V.E...... ......ID 0,0-1080,2232}, child: androidx.appcompat.widget.AppCompatButton{7dd18ba VFED..C.. ........ 816,0-1080,150 #7f080057 app:id/btn}, dependency: com.tomorrow.target30.TempView{81f1ec8 VFED..C.. ...p..ID 1,0-227,150}
2020-09-21 12:14:33.097 32265-32265/com.tomorrow.target30 D/MyBehavior: zwm, onDependentViewChanged, parent: androidx.coordinatorlayout.widget.CoordinatorLayout{88636b V.E...... ......ID 0,0-1080,2232}, btn: androidx.appcompat.widget.AppCompatButton{7dd18ba VFED..C.. ........ 816,0-1080,150 #7f080057 app:id/btn}, dependency: com.tomorrow.target30.TempView{81f1ec8 VFED..C.. ...p..ID 1,0-227,150}