上下滑动时隐藏和显示约束布局
hide and show constraintlayout on swipe down and up
我想让渐变区域可以滑动(向下滑动显示,向上滑动隐藏)。
这是我的代码:
val scopeLayout = inflaterView.findViewById<ConstraintLayout>(R.id.scope_layout)
scopeLayout.setOnTouchListener({ v, event ->
when (event.action) {
MotionEvent.ACTION_DOWN ->
Toast.makeText(context, "you just touch the screen :-)", Toast.LENGTH_SHORT).show()
scopeLayout.height = 215 // error val cannot be reassigned
}
true
})
我得到了错误 val cannot be reassigned
。以及如何使用 dp
值设置高度?
试试这个
public override bool OnTouchEvent (MotionEvent e)
{
var action = e.Action;
switch (action) {
case MotionEventActions.Pointer3Down:
Toast.MakeText (this, "hey", ToastLength.Long);
break;
case MotionEventActions.PointerUp:
Toast.MakeText (this, "yo", ToastLength.Long);
break;
default:
break;
}
if (e.Action == MotionEventActions.Move) {
return true;
}
return base.OnTouchEvent (e);
}
我觉得对你有帮助
使用 var
而不是 val
因为 val
是不可变的。
关于如何计算dp到px你可以
val Int.dp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
val Int.px: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
然后这样使用88.dp
编辑:
scopeLayout.layoutParams = scopeLayout.layoutParams.apply {
width = ...
height = ...
}
我想让渐变区域可以滑动(向下滑动显示,向上滑动隐藏)。
这是我的代码:
val scopeLayout = inflaterView.findViewById<ConstraintLayout>(R.id.scope_layout)
scopeLayout.setOnTouchListener({ v, event ->
when (event.action) {
MotionEvent.ACTION_DOWN ->
Toast.makeText(context, "you just touch the screen :-)", Toast.LENGTH_SHORT).show()
scopeLayout.height = 215 // error val cannot be reassigned
}
true
})
我得到了错误 val cannot be reassigned
。以及如何使用 dp
值设置高度?
试试这个
public override bool OnTouchEvent (MotionEvent e)
{
var action = e.Action;
switch (action) {
case MotionEventActions.Pointer3Down:
Toast.MakeText (this, "hey", ToastLength.Long);
break;
case MotionEventActions.PointerUp:
Toast.MakeText (this, "yo", ToastLength.Long);
break;
default:
break;
}
if (e.Action == MotionEventActions.Move) {
return true;
}
return base.OnTouchEvent (e);
}
我觉得对你有帮助
使用 var
而不是 val
因为 val
是不可变的。
关于如何计算dp到px你可以
val Int.dp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
val Int.px: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
然后这样使用88.dp
编辑:
scopeLayout.layoutParams = scopeLayout.layoutParams.apply {
width = ...
height = ...
}