Skip to content

Commit

Permalink
add DynamicLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
yechao committed May 9, 2023
1 parent 307a0e3 commit af09811
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 4 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ star :kiss:
| :--: | :--: | :--: |
| ItemTouchHelper | FloatView | GuideLine |

| <img src="/gif/divider.png" width="285"/> |
| :--: |
| divider |
| <img src="/gif/divider.png" width="285"/> | <img src="/gif/dynamic_layout.gif" width="285"/> |
| :--: | :--: |
| divider | DynamicLayout |

### 组件(Components)
* FloatView
Expand Down Expand Up @@ -78,6 +78,7 @@ star :kiss:
* ...

### 近期更新(Update)
- 【2023-05-10】Add DynamicLayout
- 【2022-09-13】Add divider
- 【2022-09-08】Add GuideLine
- 【2022-07-31】Add FloatView
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<activity android:name=".activity.FloatViewActivity" />
<activity android:name=".activity.GuideLineActivity" />
<activity android:name=".activity.DividerActivity" />
<activity android:name=".activity.DynamicLayoutActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.yechaoa.materialdesign.activity

import android.annotation.SuppressLint
import android.util.Log
import android.view.MotionEvent
import android.view.ViewGroup
import androidx.core.view.ViewCompat.offsetTopAndBottom
import com.yechaoa.materialdesign.R
import com.yechaoa.materialdesign.databinding.ActivityDynamicLayoutBinding

/**
* 拖拽修改上下布局占比
*/
class DynamicLayoutActivity : ToolbarActivity<ActivityDynamicLayoutBinding>() {

override fun getViewBinding(): ActivityDynamicLayoutBinding {
return ActivityDynamicLayoutBinding.inflate(layoutInflater)
}

override fun setToolbar() {
mToolbar.setTitle(R.string.dynamic_layout)
}

override fun initView() {
setTouchListener()
}

private var mDownY = 0F

@SuppressLint("ClickableViewAccessibility")
private fun setTouchListener() {
mBinding.layoutLine.setOnTouchListener { v, event ->
val y = event.y
when (event.action) {
MotionEvent.ACTION_DOWN -> {
mDownY = event.y
}
MotionEvent.ACTION_MOVE -> {
val topHeight = (y - mDownY).toInt()
offsetTopAndBottom(v, (y - mDownY).toInt())
refreshTopLayout(topHeight)
}
MotionEvent.ACTION_UP -> {
// TODO: 保存当前位置、
// TODO: 优化:添加上下布局的最大、最小高度约束
}
}
true
}
}

private fun refreshTopLayout(topHeight: Int) {
Log.d("aaa topHeight = ", topHeight.toString())
val layoutParams = mBinding.layoutTop.layoutParams as ViewGroup.LayoutParams
layoutParams.height = layoutParams.height + topHeight
mBinding.layoutTop.layoutParams = layoutParams
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class MainActivity : ToolbarActivity<ActivityMainBinding>() {
getString(R.string.notification),
getString(R.string.float_view),
getString(R.string.guide_line),
getString(R.string.divider)
getString(R.string.divider),
getString(R.string.dynamic_layout)

)

mAdapter = MainAdapter(this, mList)
Expand Down Expand Up @@ -100,6 +102,7 @@ class MainActivity : ToolbarActivity<ActivityMainBinding>() {
17 -> openActivity(FloatViewActivity::class.java)
18 -> openActivity(GuideLineActivity::class.java)
19 -> openActivity(DividerActivity::class.java)
20 -> openActivity(DynamicLayoutActivity::class.java)
}
}
})
Expand Down
59 changes: 59 additions & 0 deletions app/src/main/res/layout/activity_dynamic_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.yechaoa.materialdesign.activity.FloatingActionButtonActivity">

<include
android:id="@+id/include"
layout="@layout/layout_toolbar" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/include">

<TextView
android:id="@+id/layout_top"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="上布局"
android:textColor="@color/white"
android:textSize="30sp"
android:textStyle="bold" />

<TextView
android:id="@+id/layout_line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/red"
android:textStyle="bold"
android:gravity="center"
android:padding="6dp"
android:text="分割线(上下拖拽见效果)"
android:textColor="@color/white"
android:textSize="14sp" />

<TextView
android:id="@+id/layout_bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/greenPrimary"
android:gravity="center"
android:text="下布局"
android:textColor="@color/white"
android:textSize="30sp"
android:textStyle="bold" />

</LinearLayout>

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<string name="float_view">FloatView</string>
<string name="guide_line">GuideLine</string>
<string name="divider">Divider</string>
<string name="dynamic_layout">DynamicLayout</string>

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

Expand Down
Binary file added gif/dynamic_layout.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit af09811

Please sign in to comment.