Skip to content

Commit

Permalink
1.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
hoanganhtuan95ptit committed Mar 7, 2020
1 parent f570d7f commit 7cdfa13
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import android.view.*
import android.widget.RelativeLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.appbar.AppBarLayout.OnOffsetChangedListener
import com.hoanganhtuan95ptit.draggable.utils.*
import com.hoanganhtuan95ptit.draggable.widget.DragBehavior
import com.hoanganhtuan95ptit.draggable.widget.DragFrame
import kotlinx.android.synthetic.main.layout_draggable_panel.view.*
import kotlin.math.abs


open class DraggablePanel @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {
Expand Down Expand Up @@ -43,13 +45,14 @@ open class DraggablePanel @JvmOverloads constructor(

var frameInitializing = false// toàn bộ giao diện đã được khởi tạo hay chưa

var finalState: State? = null// trạng thái của Draggable đang hướng đến
var finalHeight = 0// chiều tao của view first đang hướng đến khi max
var mTempState: State? = null// trạng thái của Draggable đang hướng đến
var mTempHeight = 0// chiều tao của view first đang hướng đến khi max

var needExpand = true// cần expand appbarLayout
var firstViewMove = false// view first đang được di chuyển

var velocityY = 0f
var velocityTracker: VelocityTracker? = null
var velocityY = 0f// tốc độ khi MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL
var velocityTracker: VelocityTracker? = null // tốc độ khi MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL

var mCurrentState: State? = null
var mCurrentPercent = -1f
Expand Down Expand Up @@ -89,7 +92,7 @@ open class DraggablePanel @JvmOverloads constructor(

mMarginBottomWhenMin = typedArray.getDimensionPixelSize(R.styleable.DraggablePanel_margin_bottom_when_min, 8.toPx())

finalState = State.values()[typedArray.getInt(R.styleable.DraggablePanel_state, 3)]
mTempState = State.values()[typedArray.getInt(R.styleable.DraggablePanel_state, 3)]

typedArray.recycle()
} else {
Expand All @@ -103,10 +106,10 @@ open class DraggablePanel @JvmOverloads constructor(

mMarginBottomWhenMin = 8.toPx()

finalState = State.CLOSE
mTempState = State.CLOSE
}

finalHeight = mHeightWhenMax
mTempHeight = mHeightWhenMax
mHeightWhenMaxDefault = mHeightWhenMax
mHeightWhenMinDefault = mHeightWhenMin

Expand Down Expand Up @@ -214,7 +217,7 @@ open class DraggablePanel @JvmOverloads constructor(

mMarginTopWhenMin = height - mHeightWhenMin - mMarginBottomWhenMin

mHeightWhenMax = finalHeight
mHeightWhenMax = mTempHeight
mHeightWhenMaxDefault = (width * 9 / 16f).toInt()

mHeightWhenMiddle = (height - mPercentWhenMiddle * mMarginBottomWhenMin - mPercentWhenMiddle * mMarginTopWhenMin).toInt()
Expand All @@ -227,7 +230,7 @@ open class DraggablePanel @JvmOverloads constructor(
setMarginTop(mMarginTopWhenMin)
gone()

when (finalState) {
when (mTempState) {
State.MAX -> {
maximize()
}
Expand Down Expand Up @@ -260,8 +263,9 @@ open class DraggablePanel @JvmOverloads constructor(
* thiết lập chiều cao first view
*/
open fun setHeightMax(height: Int) {
finalHeight = height
if (frameInitializing && finalState == mCurrentState) {// nếu view đã được khởi tạo và đã không drag thì sẽ mở rộng
needExpand = true
mTempHeight = height
if (frameInitializing && mTempState == mCurrentState) {// nếu view đã được khởi tạo và đã không drag thì sẽ mở rộng
maximize()
}
}
Expand All @@ -270,16 +274,35 @@ open class DraggablePanel @JvmOverloads constructor(
* mở rộng lâyout
*/
open fun maximize() {
finalState = State.MAX
mTempState = State.MAX
if (!frameInitializing) {
return
}
when (mCurrentState) {
State.MAX -> {
appbarLayout.resizeAnimation(-1, finalHeight, 300) {
mHeightWhenMax = finalHeight
appbarLayout.resizeAnimation(-1, mTempHeight, 300) {
mHeightWhenMax = mTempHeight

if (mCurrentPercent != 0f || !needExpand) {//
updateState()
return@resizeAnimation
}

appbarLayout.addOnOffsetChangedListener(object : OnOffsetChangedListener {
override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
if (mCurrentPercent != 0f || !needExpand) {
appbarLayout.removeOnOffsetChangedListener(this)
return
}

if (abs(verticalOffset) == 0) {
appbarLayout.removeOnOffsetChangedListener(this)
needExpand = false
updateState()
}
}
})
appbarLayout.setExpanded(true, true)
updateState()
}
}
State.MIN -> {
Expand All @@ -296,7 +319,7 @@ open class DraggablePanel @JvmOverloads constructor(
* thu nhỏ layout
*/
open fun minimize() {
finalState = State.MIN
mTempState = State.MIN
if (!frameInitializing) {
return
}
Expand All @@ -319,7 +342,7 @@ open class DraggablePanel @JvmOverloads constructor(
* đóng layout
*/
open fun close() {
finalState = State.CLOSE
mTempState = State.CLOSE
if (!frameInitializing) {
return
}
Expand Down Expand Up @@ -434,24 +457,24 @@ open class DraggablePanel @JvmOverloads constructor(
}

private fun minToMaxAnim(onEnd: () -> Unit) {
finalState = State.MAX
mTempState = State.MAX
springYAnim(0f, onEnd)
}

private fun maxToMinAnim(onEnd: () -> Unit) {
finalState = State.MIN
mTempState = State.MIN
springYAnim(mMarginTopWhenMin.toFloat(), onEnd)
}

private fun minToCloseAnim(onEnd: () -> Unit) {
finalState = State.CLOSE
mTempState = State.CLOSE
translationYAnim((mHeightWhenMinDefault + mMarginBottomWhenMin).toFloat()) {
onEnd()
}
}

private fun closeToMinAnim(onEnd: () -> Unit) {
finalState = State.MIN
mTempState = State.MIN
translationYAnim((0).toFloat()) {
onEnd()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.hoanganhtuan95ptit.example

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.hoanganhtuan95ptit.draggable.DraggablePanel
import com.hoanganhtuan95ptit.draggable.utils.toPx
import com.hoanganhtuan95ptit.example.fragment.BottomFragment
import com.hoanganhtuan95ptit.example.fragment.TopFragment
import kotlinx.android.synthetic.main.activity_custom.*
import kotlinx.android.synthetic.main.layout_bottom.*
import kotlin.math.max
import kotlin.math.min

class CustomActivity : AppCompatActivity() {

Expand All @@ -31,6 +34,16 @@ class CustomActivity : AppCompatActivity() {
btnMax.setOnClickListener { draggablePanel.maximize() }
btnMin.setOnClickListener { draggablePanel.minimize() }
btnClose.setOnClickListener { draggablePanel.close() }
btnSetHeightMax.setOnClickListener {
var heightMax = 0
if (etHeightMax.text.isNotEmpty()) {
heightMax = etHeightMax.text.toString().toInt()
}
heightMax = max(heightMax, 200)
heightMax = min(heightMax, 400)

draggablePanel.setHeightMax(heightMax.toPx())
}

}
}
21 changes: 19 additions & 2 deletions example/src/main/res/layout/activity_custom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,36 @@
android:layout_height="wrap_content"
android:text="Close" />

<EditText
android:id="@+id/etHeightMax"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:digits="0123456789"
android:hint="Enter height max db (max 350, min 200)"
android:inputType="number" />

<Button
android:id="@+id/btnSetHeightMax"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Height Max" />

</LinearLayout>

<View
android:id="@+id/alpha"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000" />
android:background="#000"
tools:visibility="gone" />

<com.hoanganhtuan95ptit.example.custom.DraggableSource
android:id="@+id/draggablePanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:height_when_max="300dp"
app:height_when_min="58dp"
app:state="MIN" />
app:state="MIN"
tools:visibility="gone" />
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 7cdfa13

Please sign in to comment.