要解決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;
}
});
請注意,使用懸浮窗口需要適當的權限申請,并且在不需要時要及時移除懸浮窗口。