在父视图中限制DragShadow

时间:2018-04-05 06:56:11

标签: android drag-and-drop

我是Android新手。我已动态创建TextView并将其添加到RelativeLayout中。 TextView是可拖动的并且工作正常,但我的问题是TextView阴影来自相对布局,而不是父版本。

请帮我解决。

XML代码包含相对布局和图像视图:

class TagFriendsActivity : AppCompatActivity(){

lateinit var imgView : ImageView
lateinit var string : String
lateinit var txtv : TextView
lateinit var rl : RelativeLayout

var screenX :Float = 0.0f
var screenY : Float = 0.0f

lateinit var tv : TextView

@SuppressLint("NewApi")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_tag_friends)

    imgView = findViewById(R.id.imgView)
    txtv = findViewById(R.id.txtv)
    rl = findViewById(R.id.rl)

    tv = TextView(this)

    imgView.setOnClickListener {

        tv = TextView(this)

        tv.setText("Who's this")
        tv.textColor = Color.WHITE
        tv.padding = 15
        tv.background = (ContextCompat.getDrawable(this, R.drawable.editbuttonborder))
        tv.x = screenX
        tv.y = screenY
        rl.addView(tv)

        data()

        tv.setOnTouchListener(ChoiceTouchListener())

    }

    imgView.setOnTouchListener(object : View.OnTouchListener {
        override fun onTouch(v: View, event: MotionEvent): Boolean {
            screenX = event.x
            screenY = event.y
            val viewX = screenX - v.left
            val viewY = screenY - v.top
            println(" " + screenX + " "+ screenY)
            return false
        }
    })

    rl.setOnDragListener(ChoiceDragListener())

}

private fun data(){
    var i = Intent(this,FriendsNameActivity::class.java)
    startActivityForResult(i,100)
}

@SuppressLint("ResourceType")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (data == null){
        rl.removeView(tv)
    }

    if (requestCode == 100 && resultCode == 100 && data != null) {

        var string = data!!.getStringExtra("data")
        txtv.setText(string)

        tv.setText(string)

        println(screenY)
        println(screenX)

    }
}
private inner class ChoiceDragListener : OnDragListener {

    @SuppressLint("NewApi")
    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    override fun onDrag(v: View, event: DragEvent): Boolean {
        val view = event.localState as View

        val viewgroup = view.parent as ViewGroup

        when (event.action) {
            DragEvent.ACTION_DRAG_STARTED -> {
            }
            DragEvent.ACTION_DRAG_ENTERED -> {
                               }
            DragEvent.ACTION_DRAG_LOCATION -> {
            }
            DragEvent.ACTION_DRAG_EXITED -> {
            }
            DragEvent.ACTION_DROP -> {

                viewgroup.removeView(view)

                var X = event.getX()
                var Y = event.getY()
                view.setX(X - (view.getWidth() / 2))
                view.setY(Y - (view.getHeight() / 2))
                rl = v as RelativeLayout
                rl.addView(view)

                view.visibility = View.VISIBLE

                return true
            }
            DragEvent.ACTION_DRAG_ENDED -> {
                if (dropEventNotHandled(event)) {
                    view.setVisibility(View.VISIBLE);
                }

            }

            else -> {

            }
        }//no action necessary
        //no action necessary
        //no action necessary
        //no action necessary
        return true
    }
}

private fun dropEventNotHandled(dragEvent: DragEvent): Boolean {
    return !dragEvent.result
}

class ChoiceTouchListener() : OnTouchListener {

    @SuppressLint("NewApi")
    override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {

        if (p1!!.actionMasked == MotionEvent.ACTION_DOWN) {

            var shadowBuilder  = View.DragShadowBuilder(p0)

            p0!!.startDrag(null, shadowBuilder, p0, 0)

            p0!!.visibility = View.INVISIBLE
            return true
        }

        else {
            return false
        }

    }
}

0 个答案:

没有答案