在向下和向上滑动时隐藏并显示constraintlayout

时间:2018-03-21 08:55:07

标签: android android-constraintlayout

enter image description here

我想让渐变区域可以滑动(向下显示,向上滑动以隐藏)。

这是我的代码:

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值设置高度?

2 个答案:

答案 0 :(得分:0)

试试这个

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);
}

我认为它可以帮助你

答案 1 :(得分:0)

使用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 = ...
}
相关问题