Skip to content

Commit af09811

Browse files
author
yechao
committed
add DynamicLayout
1 parent 307a0e3 commit af09811

File tree

7 files changed

+128
-4
lines changed

7 files changed

+128
-4
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ star :kiss:
4343
| :--: | :--: | :--: |
4444
| ItemTouchHelper | FloatView | GuideLine |
4545

46-
| <img src="/gif/divider.png" width="285"/> |
47-
| :--: |
48-
| divider |
46+
| <img src="/gif/divider.png" width="285"/> | <img src="/gif/dynamic_layout.gif" width="285"/> |
47+
| :--: | :--: |
48+
| divider | DynamicLayout |
4949

5050
### 组件(Components)
5151
* FloatView
@@ -78,6 +78,7 @@ star :kiss:
7878
* ...
7979

8080
### 近期更新(Update)
81+
- 【2023-05-10】Add DynamicLayout
8182
- 【2022-09-13】Add divider
8283
- 【2022-09-08】Add GuideLine
8384
- 【2022-07-31】Add FloatView

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<activity android:name=".activity.FloatViewActivity" />
4646
<activity android:name=".activity.GuideLineActivity" />
4747
<activity android:name=".activity.DividerActivity" />
48+
<activity android:name=".activity.DynamicLayoutActivity" />
4849
</application>
4950

5051
</manifest>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.yechaoa.materialdesign.activity
2+
3+
import android.annotation.SuppressLint
4+
import android.util.Log
5+
import android.view.MotionEvent
6+
import android.view.ViewGroup
7+
import androidx.core.view.ViewCompat.offsetTopAndBottom
8+
import com.yechaoa.materialdesign.R
9+
import com.yechaoa.materialdesign.databinding.ActivityDynamicLayoutBinding
10+
11+
/**
12+
* 拖拽修改上下布局占比
13+
*/
14+
class DynamicLayoutActivity : ToolbarActivity<ActivityDynamicLayoutBinding>() {
15+
16+
override fun getViewBinding(): ActivityDynamicLayoutBinding {
17+
return ActivityDynamicLayoutBinding.inflate(layoutInflater)
18+
}
19+
20+
override fun setToolbar() {
21+
mToolbar.setTitle(R.string.dynamic_layout)
22+
}
23+
24+
override fun initView() {
25+
setTouchListener()
26+
}
27+
28+
private var mDownY = 0F
29+
30+
@SuppressLint("ClickableViewAccessibility")
31+
private fun setTouchListener() {
32+
mBinding.layoutLine.setOnTouchListener { v, event ->
33+
val y = event.y
34+
when (event.action) {
35+
MotionEvent.ACTION_DOWN -> {
36+
mDownY = event.y
37+
}
38+
MotionEvent.ACTION_MOVE -> {
39+
val topHeight = (y - mDownY).toInt()
40+
offsetTopAndBottom(v, (y - mDownY).toInt())
41+
refreshTopLayout(topHeight)
42+
}
43+
MotionEvent.ACTION_UP -> {
44+
// TODO: 保存当前位置、
45+
// TODO: 优化:添加上下布局的最大、最小高度约束
46+
}
47+
}
48+
true
49+
}
50+
}
51+
52+
private fun refreshTopLayout(topHeight: Int) {
53+
Log.d("aaa topHeight = ", topHeight.toString())
54+
val layoutParams = mBinding.layoutTop.layoutParams as ViewGroup.LayoutParams
55+
layoutParams.height = layoutParams.height + topHeight
56+
mBinding.layoutTop.layoutParams = layoutParams
57+
}
58+
59+
}

app/src/main/java/com/yechaoa/materialdesign/activity/MainActivity.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ class MainActivity : ToolbarActivity<ActivityMainBinding>() {
5454
getString(R.string.notification),
5555
getString(R.string.float_view),
5656
getString(R.string.guide_line),
57-
getString(R.string.divider)
57+
getString(R.string.divider),
58+
getString(R.string.dynamic_layout)
59+
5860
)
5961

6062
mAdapter = MainAdapter(this, mList)
@@ -100,6 +102,7 @@ class MainActivity : ToolbarActivity<ActivityMainBinding>() {
100102
17 -> openActivity(FloatViewActivity::class.java)
101103
18 -> openActivity(GuideLineActivity::class.java)
102104
19 -> openActivity(DividerActivity::class.java)
105+
20 -> openActivity(DynamicLayoutActivity::class.java)
103106
}
104107
}
105108
})
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:orientation="vertical"
8+
tools:context="com.yechaoa.materialdesign.activity.FloatingActionButtonActivity">
9+
10+
<include
11+
android:id="@+id/include"
12+
layout="@layout/layout_toolbar" />
13+
14+
<LinearLayout
15+
android:layout_width="match_parent"
16+
android:layout_height="match_parent"
17+
android:orientation="vertical"
18+
app:layout_constraintBottom_toBottomOf="parent"
19+
app:layout_constraintStart_toStartOf="parent"
20+
app:layout_constraintTop_toBottomOf="@+id/include">
21+
22+
<TextView
23+
android:id="@+id/layout_top"
24+
android:layout_width="match_parent"
25+
android:layout_height="300dp"
26+
android:background="@color/colorAccent"
27+
android:gravity="center"
28+
android:text="上布局"
29+
android:textColor="@color/white"
30+
android:textSize="30sp"
31+
android:textStyle="bold" />
32+
33+
<TextView
34+
android:id="@+id/layout_line"
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content"
37+
android:background="@color/red"
38+
android:textStyle="bold"
39+
android:gravity="center"
40+
android:padding="6dp"
41+
android:text="分割线(上下拖拽见效果)"
42+
android:textColor="@color/white"
43+
android:textSize="14sp" />
44+
45+
<TextView
46+
android:id="@+id/layout_bottom"
47+
android:layout_width="match_parent"
48+
android:layout_height="0dp"
49+
android:layout_weight="1"
50+
android:background="@color/greenPrimary"
51+
android:gravity="center"
52+
android:text="下布局"
53+
android:textColor="@color/white"
54+
android:textSize="30sp"
55+
android:textStyle="bold" />
56+
57+
</LinearLayout>
58+
59+
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<string name="float_view">FloatView</string>
2929
<string name="guide_line">GuideLine</string>
3030
<string name="divider">Divider</string>
31+
<string name="dynamic_layout">DynamicLayout</string>
3132

3233
<string name="pull_to_refresh">下拉开始刷新</string>
3334

gif/dynamic_layout.gif

256 KB
Loading

0 commit comments

Comments
 (0)