要解决Android ViewPager悬浮窗问题,你可以尝试以下方法:
// 创建悬浮窗口
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);
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
ViewGroup.LayoutParams viewPagerParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
ViewPager viewPager = new ViewPager(context);
viewPager.setLayoutParams(viewPagerParams);
// 将ViewPager添加到悬浮窗口上
windowManager.addView(viewPager, params);
viewPager.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(viewPager, params);
return true;
}
return false;
}
});
请注意,使用悬浮窗口需要适当的权限申请,并且在不需要时要及时移除悬浮窗口。