修改BUG
修改飘屏,飘屏不合适
This commit is contained in:
@@ -163,8 +163,8 @@ public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresente
|
||||
int screenHeight = displayMetrics.heightPixels;
|
||||
// 设置高度为屏幕高度的100%(全屏)
|
||||
int heightInPx = (int) (screenHeight * 0.79);
|
||||
;
|
||||
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, heightInPx);
|
||||
|
||||
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
window.setBackgroundDrawableResource(android.R.color.transparent);
|
||||
|
||||
// 可选:设置动画样式(从底部弹出)
|
||||
|
||||
@@ -17,7 +17,7 @@ import lombok.Getter;
|
||||
public class GiftXlhChouAdapter extends BaseQuickAdapter<GiftBean, BaseViewHolder> {
|
||||
private List<GiftBean> giftLists = new ArrayList<>();
|
||||
private int selectedPosition = -1;
|
||||
private static final int LOOP_COUNT = 6; // 循环倍数
|
||||
private static final int LOOP_COUNT = 8; // 循环倍数
|
||||
|
||||
|
||||
public GiftXlhChouAdapter() {
|
||||
@@ -138,6 +138,10 @@ public class GiftXlhChouAdapter extends BaseQuickAdapter<GiftBean, BaseViewHolde
|
||||
* @return 原始数据大小
|
||||
*/
|
||||
public int getOriginalDataSize() {
|
||||
return giftLists.size();
|
||||
List<GiftBean> loopData = new ArrayList<>();
|
||||
for (int i = 0; i < LOOP_COUNT; i++) {
|
||||
loopData.addAll(giftLists);
|
||||
}
|
||||
return loopData.size();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
package com.xscm.moduleutil.dialog.giftLottery;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.os.CountDownTimer;
|
||||
import android.os.Handler;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.Window;
|
||||
import android.widget.HorizontalScrollView;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
@@ -39,6 +46,7 @@ import org.greenrobot.eventbus.ThreadMode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class TourClubDialogFragment extends BaseMvpDialogFragment<GiftLotteryPresenter, FragmentTourClubDialogBinding> implements GiftLotteryContacts.View {
|
||||
private String roomId;
|
||||
@@ -56,6 +64,14 @@ public class TourClubDialogFragment extends BaseMvpDialogFragment<GiftLotteryPre
|
||||
private static final int ITEM_COUNT = 24;
|
||||
private static final int ROTATION_COUNT = 3;
|
||||
|
||||
|
||||
private Handler handler = new Handler();
|
||||
private boolean isLotteryRunning = false;
|
||||
private int scrollSpeed = 10; // 滚动速度
|
||||
private int totalScrollTime = 5000; // 总滚动时间,单位:毫秒
|
||||
private int targetPosition;
|
||||
|
||||
|
||||
@Override
|
||||
protected GiftLotteryPresenter bindPresenter() {
|
||||
return new GiftLotteryPresenter(this, getActivity());
|
||||
@@ -109,8 +125,7 @@ public class TourClubDialogFragment extends BaseMvpDialogFragment<GiftLotteryPre
|
||||
int screenHeight = displayMetrics.heightPixels;
|
||||
// 设置高度为屏幕高度的100%(全屏)
|
||||
int heightInPx = (int) (screenHeight * 0.79);
|
||||
;
|
||||
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, heightInPx);
|
||||
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
window.setBackgroundDrawableResource(android.R.color.transparent);
|
||||
|
||||
// 可选:设置动画样式(从底部弹出)
|
||||
@@ -128,6 +143,8 @@ public class TourClubDialogFragment extends BaseMvpDialogFragment<GiftLotteryPre
|
||||
mBinding.tvBd.setOnClickListener(this::onClick);
|
||||
mBinding.tvJl.setOnClickListener(this::onClick);
|
||||
|
||||
|
||||
|
||||
giftXlhChouAdapter=new GiftXlhChouAdapter();
|
||||
// 使用 LinearLayoutManager 横向滚动
|
||||
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
|
||||
@@ -168,14 +185,17 @@ public class TourClubDialogFragment extends BaseMvpDialogFragment<GiftLotteryPre
|
||||
com.hjq.toast.ToastUtils.show("奖池数据加载中,请稍后再试");
|
||||
}
|
||||
} else if (id == R.id.ll_one) {
|
||||
prepareForNewLottery();
|
||||
num="1";
|
||||
MvpPre.xlhChou(roomId,num);
|
||||
|
||||
} else if (id == R.id.ll_ten) {
|
||||
prepareForNewLottery();
|
||||
num="10";
|
||||
MvpPre.xlhChou(roomId,num);
|
||||
|
||||
} else if (id == R.id.ll_hundred) {
|
||||
prepareForNewLottery();
|
||||
num="100";
|
||||
MvpPre.xlhChou(roomId,num);
|
||||
|
||||
@@ -203,7 +223,6 @@ public class TourClubDialogFragment extends BaseMvpDialogFragment<GiftLotteryPre
|
||||
xlhRecordDialog.show(getChildFragmentManager(), "XlhRecordDialog");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
return R.layout.fragment_tour_club_dialog;
|
||||
@@ -248,6 +267,7 @@ public class TourClubDialogFragment extends BaseMvpDialogFragment<GiftLotteryPre
|
||||
}
|
||||
|
||||
giftXlhChouAdapter.setNewData(giftLists);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,6 +371,18 @@ public class TourClubDialogFragment extends BaseMvpDialogFragment<GiftLotteryPre
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
releaseCountDownTimer();
|
||||
// 清理所有待执行的任务
|
||||
clearPendingTasks();
|
||||
|
||||
// 清理滚动帮助类
|
||||
if (scrollHelper != null) {
|
||||
scrollHelper = null;
|
||||
}
|
||||
|
||||
// 清理适配器引用
|
||||
if (giftXlhChouAdapter != null) {
|
||||
giftXlhChouAdapter = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -380,66 +412,136 @@ public class TourClubDialogFragment extends BaseMvpDialogFragment<GiftLotteryPre
|
||||
mBinding.tvIcon.setText(walletBean.getCoin());
|
||||
}
|
||||
}
|
||||
private Runnable pendingLotteryRunnable;
|
||||
private Runnable pendingCenteringRunnable;
|
||||
|
||||
@Override
|
||||
public void xlhChouSuccess(List<XlhDrawBean> data) {
|
||||
if (data != null){
|
||||
// 抽奖完成后执行动画滚动
|
||||
mBinding.recycleView.postDelayed(() -> {
|
||||
int winningPosition = findHighestValueWinningPosition(data);
|
||||
if (winningPosition != -1) {
|
||||
|
||||
startLotteryAnimation(winningPosition);
|
||||
// 取消之前可能存在的任务
|
||||
clearPendingTasks();
|
||||
// 抽奖完成后执行动画滚动
|
||||
pendingLotteryRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// 清理之前的状态
|
||||
if (giftXlhChouAdapter != null) {
|
||||
giftXlhChouAdapter.clearSelection();
|
||||
}
|
||||
int winningPosition = findHighestValueWinningPosition(data);//这是获取到的中奖位置下标
|
||||
if (winningPosition != -1) {
|
||||
if (scrollHelper == null) {
|
||||
scrollHelper = new CenterScrollHelper(mBinding.recycleView);
|
||||
}
|
||||
|
||||
// 计算在循环列表中的目标位置(滚动几圈后停在目标位置)
|
||||
int loopCount = 4; // 滚动4圈
|
||||
int originalSize = giftLists.size();///这是列表的总大小
|
||||
// 计算目标在循环列表中的位置(确保在中间区域)
|
||||
///这是计算总圈数的大小
|
||||
int middleBaseIndex = (loopCount * originalSize);
|
||||
///这里是展示在中奖的位置,加上总圈数的大小,
|
||||
int targetLoopIndex = middleBaseIndex + (winningPosition % originalSize);
|
||||
|
||||
/**
|
||||
// 使用scrollWithCircles方法执行带动画的滚动(带完成回调)
|
||||
scrollHelper.scrollWithCircles(
|
||||
targetLoopIndex, // 在循环列表中的位置
|
||||
loopCount, // 滚动圈数
|
||||
200, // 每个item滚动时间200ms(控制速度)
|
||||
originalSize, // 原始数据大小
|
||||
() -> { // 滚动完成回调
|
||||
// 滚动完成后更新选中状态(使用原始位置)
|
||||
if (giftXlhChouAdapter != null) {
|
||||
giftXlhChouAdapter.setSelectedPosition(winningPosition);
|
||||
}
|
||||
// 滚动完成后延迟一小段时间再居中,确保UI更新完成
|
||||
pendingCenteringRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// 手动将选中项居中
|
||||
centerSelectedItem(winningPosition, originalSize);
|
||||
|
||||
|
||||
// 计算在循环列表中的目标位置(滚动几圈后停在目标位置)
|
||||
int loopCount = 4; // 滚动3圈
|
||||
int originalSize = giftXlhChouAdapter.getOriginalDataSize();
|
||||
// 计算目标在循环列表中的位置(确保在中间区域)
|
||||
|
||||
int middleBaseIndex = (loopCount * originalSize) + (originalSize / 2);
|
||||
int targetLoopIndex = middleBaseIndex + (winningPosition % originalSize);
|
||||
|
||||
// 使用scrollWithCircles方法执行带动画的滚动(带完成回调)
|
||||
scrollHelper.scrollWithCircles(
|
||||
targetLoopIndex, // 在循环列表中的位置
|
||||
loopCount, // 滚动圈数
|
||||
500, // 每个item滚动时间200ms(控制速度)
|
||||
originalSize, // 原始数据大小
|
||||
() -> { // 滚动完成回调
|
||||
// 滚动完成后更新选中状态(使用原始位置)
|
||||
if (giftXlhChouAdapter != null) {
|
||||
giftXlhChouAdapter.setSelectedPosition(winningPosition);
|
||||
// 显示结果对话框
|
||||
getActivity().runOnUiThread(() -> {
|
||||
scrollHelper = null;
|
||||
showResultDialog(data);
|
||||
});
|
||||
}
|
||||
};
|
||||
mBinding.recycleView.postDelayed(pendingCenteringRunnable, 100);
|
||||
}
|
||||
// 滚动完成后延迟一小段时间再居中,确保UI更新完成
|
||||
mBinding.recycleView.postDelayed(() -> {
|
||||
// 手动将选中项居中
|
||||
centerSelectedItem(winningPosition, originalSize);
|
||||
);
|
||||
|
||||
// 显示结果对话框
|
||||
getActivity().runOnUiThread(() -> {
|
||||
showResultDialog(data);
|
||||
});
|
||||
}, 100);
|
||||
// 在滚动完成后再显示对话框
|
||||
// getActivity().runOnUiThread(() -> {
|
||||
// showResultDialog(data);
|
||||
// });
|
||||
}
|
||||
);
|
||||
*/
|
||||
} else {
|
||||
// 如果没有找到中奖位置,直接显示对话框
|
||||
showResultDialog(data);
|
||||
} else {
|
||||
// 如果没有找到中奖位置,直接显示对话框
|
||||
showResultDialog(data);
|
||||
}
|
||||
}
|
||||
}, 300);
|
||||
};
|
||||
mBinding.recycleView.postDelayed(pendingLotteryRunnable, 300);
|
||||
|
||||
|
||||
}
|
||||
MvpPre.wallet();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 为新的抽奖做准备,清理之前的状态
|
||||
*/
|
||||
private void prepareForNewLottery() {
|
||||
// 取消之前可能存在的任务
|
||||
clearPendingTasks();
|
||||
|
||||
// 清除之前的选择状态
|
||||
if (giftXlhChouAdapter != null) {
|
||||
giftXlhChouAdapter.clearSelection();
|
||||
}
|
||||
|
||||
// 重置滚动帮助类
|
||||
if (scrollHelper != null) {
|
||||
scrollHelper.reset();
|
||||
}
|
||||
|
||||
// 停止可能正在进行的滚动
|
||||
mBinding.recycleView.stopScroll();
|
||||
|
||||
// 清理可能存在的回调
|
||||
if (mBinding.recycleView.getHandler() != null) {
|
||||
mBinding.recycleView.getHandler().removeCallbacksAndMessages(null);
|
||||
}
|
||||
|
||||
// 重新初始化滚动帮助类
|
||||
scrollHelper = new CenterScrollHelper(mBinding.recycleView);
|
||||
}
|
||||
/**
|
||||
* 清除所有待执行的任务
|
||||
*/
|
||||
private void clearPendingTasks() {
|
||||
// 取消抽奖动画任务
|
||||
if (pendingLotteryRunnable != null) {
|
||||
mBinding.recycleView.removeCallbacks(pendingLotteryRunnable);
|
||||
pendingLotteryRunnable = null;
|
||||
}
|
||||
|
||||
// 取消居中任务
|
||||
if (pendingCenteringRunnable != null) {
|
||||
mBinding.recycleView.removeCallbacks(pendingCenteringRunnable);
|
||||
pendingCenteringRunnable = null;
|
||||
}
|
||||
|
||||
// 停止可能正在进行的滚动
|
||||
mBinding.recycleView.stopScroll();
|
||||
|
||||
// 清理handler中的所有消息
|
||||
if (mBinding.recycleView.getHandler() != null) {
|
||||
mBinding.recycleView.getHandler().removeCallbacksAndMessages(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void startLotteryAnimation(int targetIndex) {
|
||||
// 确保适配器有数据
|
||||
if (giftXlhChouAdapter == null || giftXlhChouAdapter.getOriginalDataSize() <= 0) {
|
||||
@@ -558,32 +660,53 @@ public class TourClubDialogFragment extends BaseMvpDialogFragment<GiftLotteryPre
|
||||
* @param originalPosition 原始数据中的位置
|
||||
* @param originalSize 原始数据大小
|
||||
*/
|
||||
// 新增优化的居中方法
|
||||
private void centerSelectedItem(int originalPosition, int originalSize) {
|
||||
LinearLayoutManager layoutManager = (LinearLayoutManager) mBinding.recycleView.getLayoutManager();
|
||||
if (layoutManager == null) return;
|
||||
|
||||
// 计算出需要滚动到的目标位置
|
||||
// 计算出需要滚动到的目标位置(确保在中间可见区域)
|
||||
int targetPosition = originalPosition + 4 * originalSize;
|
||||
|
||||
// 使用smoothScrollToPosition滚动到目标位置
|
||||
mBinding.recycleView.smoothScrollToPosition(targetPosition);
|
||||
// 先滚动到目标位置附近
|
||||
mBinding.recycleView.scrollToPosition(targetPosition);
|
||||
|
||||
// 等待滚动完成
|
||||
mBinding.recycleView.postDelayed(() -> {
|
||||
// 计算出选中项的偏移量,确保它在中间
|
||||
View view = layoutManager.findViewByPosition(targetPosition);
|
||||
if (view == null) return;
|
||||
// 使用post确保布局完成后进行精确居中
|
||||
mBinding.recycleView.post(() -> {
|
||||
View targetView = layoutManager.findViewByPosition(targetPosition);
|
||||
if (targetView == null) {
|
||||
// 如果目标view不可见,使用smoothScrollToPosition
|
||||
mBinding.recycleView.smoothScrollToPosition(targetPosition);
|
||||
// 再次尝试居中
|
||||
mBinding.recycleView.postDelayed(() -> {
|
||||
View refreshedView = layoutManager.findViewByPosition(targetPosition);
|
||||
if (refreshedView != null) {
|
||||
performCentering(layoutManager, refreshedView);
|
||||
}
|
||||
}, 50);
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算出RecyclerView的宽度和View的宽度
|
||||
int recyclerViewWidth = mBinding.recycleView.getWidth();
|
||||
int viewWidth = view.getWidth();
|
||||
performCentering(layoutManager, targetView);
|
||||
});
|
||||
}
|
||||
|
||||
// 计算偏移量,确保view居中
|
||||
int offsetToCenter = (recyclerViewWidth / 2) - (viewWidth / 2) - view.getLeft();
|
||||
// 执行实际的居中操作
|
||||
private void performCentering(LinearLayoutManager layoutManager, View targetView) {
|
||||
// 计算RecyclerView的中心点
|
||||
int recyclerViewWidth = mBinding.recycleView.getWidth();
|
||||
int recyclerViewCenter = recyclerViewWidth / 2;
|
||||
|
||||
// 调整滚动的位置
|
||||
mBinding.recycleView.scrollBy(offsetToCenter, 0);
|
||||
}, 500); // 500毫秒的延迟,根据实际情况调整
|
||||
// 计算目标view的中心点
|
||||
int targetViewLeft = targetView.getLeft();
|
||||
int targetViewWidth = targetView.getWidth();
|
||||
int targetViewCenter = targetViewLeft + targetViewWidth / 2;
|
||||
|
||||
// 计算需要滚动的偏移量
|
||||
int offset = targetViewCenter - recyclerViewCenter;
|
||||
|
||||
// 精确滚动使目标view居中
|
||||
mBinding.recycleView.scrollBy(offset, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user