Because of this question I can now move a child from Framelayout
outside the limits of his father, but the problem is that if I loose the Framelayout
child outside the limits of his father, I can not move it, here what happens:
Code activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false">
<FrameLayout
android:id="@+id/parentMove"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@color/colorPrimary"
android:elevation="15dp"
android:layout_marginLeft="100dp">
<FrameLayout
android:id="@+id/move2"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@android:color/holo_green_dark"
android:elevation="10dp">
</FrameLayout>
</FrameLayout>
</FrameLayout>
Code MainActivity
:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener
{
FrameLayout move2, parentMove, activity_main;
float dX2, dY2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity_main = (FrameLayout) findViewById(R.id.activity_main);
activity_main.setOnTouchListener(this);
parentMove = (FrameLayout) findViewById(R.id.parentMove);
parentMove.setOnTouchListener(this);
move2 = (FrameLayout) findViewById(R.id.move2);
move2.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent e)
{
if (v == move2)
{
Log.d("PINCHO","move2");
}
if ( v == activity_main)
{
Log.d("PINCHO","activity_main");
}
if ( v == parentMove)
{
Log.d("PINCHO","parentMove");
}
if (v == move2 && e.getAction() == MotionEvent.ACTION_DOWN)
{
dX2 = v.getX() - e.getRawX();
dY2 = v.getY() - e.getRawY();
}
if (v == move2 && e.getAction() == MotionEvent.ACTION_MOVE)
{
move2.setY(e.getRawY()+dY2);
move2.setX(e.getRawX()+dX2);
}
return true;
}
}