ANDROID Перетащите вид в любом месте макета - PullRequest
0 голосов
/ 21 октября 2019

Я хотел бы перетащить представление в любое место на экране

Я реализовал сенсорный прослушиватель, и единственная проблема заключается в том, что представления, которые предшествуют другим в иерархии, имеют тенденцию тянуть их вперед. Я хочу, чтобы каждое представление можно было перетаскивать в любое место независимо от других.

Я убедился, что удалил вид, затем добавил его с другими параметрами в конце перетаскивания.

public class MainActivity extends Activity {  
int clickCount;
private float dx, dy;
private boolean isDragging;
private ViewGroup RootLayout;
private int Position_X;
private int Position_Y;
private TextView txtV01, txtV02, txtV03;

long startTime = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RootLayout = (ViewGroup) findViewById(R.id.main_layout);

    txtV02 = (TextView) findViewById(R.id.txt02);
    txtV02.setOnTouchListener(myTouchListener());

    txtV01 = (TextView) findViewById(R.id.txt01);
    txtV01.setOnTouchListener(myTouchListener());

    txtV03 = (TextView) findViewById(R.id.txtV03);
    txtV03.setOnTouchListener(myTouchListener());
}


private View.OnTouchListener myTouchListener() {
    return new View.OnTouchListener() {

        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            final int x = (int) event.getRawX();
            final int y = (int) event.getRawY();

            switch (event.getAction() & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN:
                    LinearLayout.LayoutParams lParams = 
(LinearLayout.LayoutParams)
                            view.getLayoutParams();

                    dx = x - lParams.leftMargin;
                    dy = y - lParams.topMargin;

                    break;

                case MotionEvent.ACTION_UP:
                     break;

                case MotionEvent.ACTION_MOVE:

                    LinearLayout.LayoutParams layoutParams = 
(LinearLayout.LayoutParams) view
                            .getLayoutParams();
                    layoutParams.leftMargin = x - (int) dx;
                    layoutParams.topMargin = y - (int) dy;
                    layoutParams.rightMargin = 0;
                    layoutParams.bottomMargin = 0;

                    view.setLayoutParams(layoutParams);

                    break;
            }
            RootLayout.invalidate();
            return true;
        }
    };
  }
}

макет:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="space">

<Space
    android:layout_width="match_parent"
    android:layout_height="387dp"
    android:tag="space_top" />

<TextView
    android:id="@+id/txt01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:fontFamily="cursive"
    android:tag="line1"
    android:text="This is line one ONE ONE ONE"
    android:textAlignment="center"
    android:textSize="33dp">

</TextView>

<Space
    android:layout_width="match_parent"
    android:layout_height="35dp" />

<TextView
    android:id="@+id/txt02"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:tag="line2"
    android:text="This is line TWO TWO NOT Line 1"
    android:textAlignment="center"
    android:textSize="18dp">
</TextView>

<Space
    android:layout_width="match_parent"
    android:layout_height="35dp" />

<TextView
    android:id="@+id/txtV03"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:tag="line2"
    android:text="This is line THREE THREE Leave me"
    android:textAlignment="center"
    android:textSize="18dp">
   </TextView>
</LinearLayout>

Ваша помощь будетбыть оцененным.

...