フローティングウィンドウの移動ができない[android : java]

実現したいこと

androidでフローティングウィンドウをスワイプで動かせるようにしたいです

発生している問題・分からないこと

フローティングウィンドウを表示させるところまではいくのですが、表示後に、ボタンの親ビューがタッチリスナーを受け取ってくれず、スワイプでの移動ができません。

以下、再現コードです。
Mainactivity

java

1package com.example.myapplication;2 3import android.content.Context;4import android.content.Intent;5import android.graphics.PixelFormat;6import android.net.Uri;7import android.os.Bundle;8import android.provider.Settings;9import android.view.LayoutInflater;10import android.view.MotionEvent;11import android.view.View;12import android.view.WindowManager;13import android.widget.Button;14 15import androidx.activity.result.ActivityResultLauncher;16import androidx.activity.result.contract.ActivityResultContracts;17import androidx.appcompat.app.AppCompatActivity;18 19public class MainActivity extends AppCompatActivity {20 @Override21 protected void onCreate(Bundle savedInstanceState) {22 super.onCreate(savedInstanceState);23 setContentView(R.layout.main);24 25 ActivityResultLauncher<Intent> launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {});26 Button button = findViewById(R.id.button);27 button.setOnClickListener(view -> {28 if (!Settings.canDrawOverlays(this)) {29 launcher.launch(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())));30 }else showFloatingView();31 });32 }33 34 private void showFloatingView() {35 WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);36 View floatingView = LayoutInflater.from(this).inflate(R.layout.floating, null);37 38 WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);39 40 floatingView.setOnTouchListener(new View.OnTouchListener() {41 private final WindowManager.LayoutParams paramsF = params;42 private int initialX;43 private int initialY;44 private float initialTouchX;45 private float initialTouchY;46 47 @Override48 public boolean onTouch(View v, MotionEvent event) {49 switch (event.getAction()) {50 case MotionEvent.ACTION_DOWN :51 initialX = paramsF.x;52 initialY = paramsF.y;53 initialTouchX = event.getRawX();54 initialTouchY = event.getRawY();55 break;56 case MotionEvent.ACTION_MOVE :57 paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);58 paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);59 manager.updateViewLayout(floatingView, paramsF);60 break;61 }62 v.performClick();63 return false;64 }65 });66 manager.addView(floatingView, params);67 68 Button closeButton = floatingView.findViewById(R.id.close);69 70 closeButton.setOnClickListener(view -> manager.removeViewImmediate(floatingView));71 }72}73

main.xml

xml

1<?xml version="1.0" encoding="utf-8"?>2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"3 xmlns:app="http://schemas.android.com/apk/res-auto"4 xmlns:tools="http://schemas.android.com/tools"5 android:id="@+id/constraint"6 android:layout_width="match_parent"7 android:layout_height="match_parent">8 9 <Button10 android:id="@+id/button"11 android:layout_width="wrap_content"12 android:layout_height="wrap_content"13 android:text="Button"14 app:layout_constraintEnd_toEndOf="parent"15 app:layout_constraintStart_toStartOf="parent" />16</androidx.constraintlayout.widget.ConstraintLayout>

floating.xml

xml

1<?xml version="1.0" encoding="utf-8"?>2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"3 xmlns:app="http://schemas.android.com/apk/res-auto"4 android:layout_width="match_parent"5 android:layout_height="match_parent">6 7 <Button8 android:id="@+id/button2"9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:text="Button" />12 13 <Button14 android:id="@+id/button3"15 android:layout_width="wrap_content"16 android:layout_height="wrap_content"17 android:text="Button"18 app:layout_constraintStart_toEndOf="@+id/button2" />19 20 <Button21 android:id="@+id/close"22 android:layout_width="wrap_content"23 android:layout_height="wrap_content"24 android:text="close"25 app:layout_constraintStart_toEndOf="@+id/button3" />26</androidx.constraintlayout.widget.ConstraintLayout>

該当のソースコード

特になし

試したこと・調べたこと

上記の詳細・結果

WindowManager.LayoutParamsの値を変更してみたり、ボタンのclickableをfalseにしてみたり、focas系もいろいろいじりましたが駄目でした。
いくら調べてもフローティング表示に関しての記事が全然出てこず、原因も全然わかりません。

補足

特になし

コメントを投稿

0 コメント