修改交友布局
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package com.xscm.moduleutil.dialog.giftLottery;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
*@author qx
|
||||
*@data 2025/8/25
|
||||
*@description: 盲盒抽奖的实体类
|
||||
*/
|
||||
@Data
|
||||
public class GiftLottery {
|
||||
private String id;
|
||||
private String icon;
|
||||
private String number;
|
||||
private String name;
|
||||
private String price;
|
||||
private boolean isSelected;
|
||||
|
||||
public GiftLottery(String id, String icon, String number, String name, String price, boolean isSelected) {
|
||||
this.id = id;
|
||||
this.icon = icon;
|
||||
this.number = number;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.isSelected = isSelected;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package com.xscm.moduleutil.dialog.giftLottery;
|
||||
|
||||
import android.animation.AnimatorSet;
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.animation.AccelerateDecelerateInterpolator;
|
||||
import android.view.animation.Animation;
|
||||
import android.view.animation.ScaleAnimation;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.xscm.moduleutil.R;
|
||||
import com.xscm.moduleutil.utils.ImageUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author qx
|
||||
* @data 2025/8/25
|
||||
* @description: 盲盒抽奖展示的视图
|
||||
*/
|
||||
// GiftLotteryAdapter.java
|
||||
public class GiftLotteryAdapter extends RecyclerView.Adapter<GiftLotteryAdapter.ViewHolder> {
|
||||
|
||||
private List<GiftLottery> giftList;
|
||||
private OnGiftClickListener listener;
|
||||
private int currentPos = -1; // 当前正在动画的位置
|
||||
private boolean isForward = true; // 是否正向扫描
|
||||
private int totalItems;
|
||||
|
||||
public GiftLotteryAdapter() {
|
||||
|
||||
}
|
||||
|
||||
public interface OnGiftClickListener {
|
||||
void onGiftClick(GiftLottery item);
|
||||
}
|
||||
|
||||
private RecyclerView recyclerView;
|
||||
|
||||
@Override
|
||||
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
|
||||
super.onAttachedToRecyclerView(recyclerView);
|
||||
this.recyclerView = recyclerView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
|
||||
super.onDetachedFromRecyclerView(recyclerView);
|
||||
this.recyclerView = null;
|
||||
}
|
||||
|
||||
public GiftLotteryAdapter(List<GiftLottery> list, OnGiftClickListener listener) {
|
||||
this.giftList = list;
|
||||
this.listener = listener;
|
||||
this.totalItems = list.size();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.item_gift_lottery, parent, false);
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
GiftLottery item = giftList.get(position);
|
||||
holder.bind(item);
|
||||
|
||||
// 设置是否选中(仅用于视觉反馈)
|
||||
if (item.isSelected()) {
|
||||
holder.itemView.setSelected(true);
|
||||
holder.itemView.setBackgroundResource(R.mipmap.ic_auction);
|
||||
} else {
|
||||
holder.itemView.setSelected(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return giftList.size();
|
||||
}
|
||||
|
||||
// 开始扫描动画
|
||||
public void startScanAnimation() {
|
||||
if (totalItems == 0) return;
|
||||
|
||||
currentPos = -1;
|
||||
isForward = true;
|
||||
scanNext();
|
||||
}
|
||||
|
||||
private void scanNext() {
|
||||
if (currentPos >= totalItems) {
|
||||
// 正向结束,开始反向
|
||||
isForward = false;
|
||||
currentPos = totalItems - 1;
|
||||
scanNext();
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentPos < 0) {
|
||||
// 初始状态,开始正向
|
||||
currentPos = 0;
|
||||
animateItem(currentPos);
|
||||
return;
|
||||
}
|
||||
|
||||
// 移动到下一个位置
|
||||
if (isForward) {
|
||||
currentPos++;
|
||||
if (currentPos >= totalItems) {
|
||||
// 正向结束,准备反向
|
||||
isForward = false;
|
||||
currentPos = totalItems - 1;
|
||||
scanNext();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
currentPos--;
|
||||
if (currentPos < 0) {
|
||||
// 反向结束,停止动画
|
||||
finishScan();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 执行动画
|
||||
animateItem(currentPos);
|
||||
}
|
||||
|
||||
private void animateItem(int pos) {
|
||||
View itemView = getViewAtPosition(pos);
|
||||
if (itemView == null) return;
|
||||
|
||||
// 缩放动画
|
||||
AnimatorSet animatorSet = new AnimatorSet();
|
||||
animatorSet.playTogether(
|
||||
ObjectAnimator.ofFloat(itemView, "scaleX", 1f, 1.2f),
|
||||
ObjectAnimator.ofFloat(itemView, "scaleY", 1f, 1.2f)
|
||||
);
|
||||
animatorSet.setDuration(200);
|
||||
animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
|
||||
animatorSet.start();
|
||||
|
||||
// 延迟下一次动画
|
||||
Handler handler = new Handler(Looper.getMainLooper());
|
||||
handler.postDelayed(() -> {
|
||||
scanNext();
|
||||
}, 300); // 每个 item 动画间隔 300ms
|
||||
}
|
||||
|
||||
private View getViewAtPosition(int position) {
|
||||
if (recyclerView == null || recyclerView.getLayoutManager() == null) {
|
||||
return null;
|
||||
}
|
||||
return recyclerView.getLayoutManager().findViewByPosition(position);
|
||||
}
|
||||
|
||||
private int getBindingAdapterPositionForView(View view) {
|
||||
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
|
||||
return params.getViewLayoutPosition();
|
||||
}
|
||||
|
||||
private void finishScan() {
|
||||
// 扫描完成,随机选择一个中奖项
|
||||
Random random = new Random();
|
||||
int winnerIndex = random.nextInt(totalItems);
|
||||
giftList.get(winnerIndex).setSelected(true);
|
||||
notifyItemChanged(winnerIndex);
|
||||
}
|
||||
|
||||
static class ViewHolder extends RecyclerView.ViewHolder {
|
||||
ImageView imgIcon;
|
||||
TextView tvName, tvCount;
|
||||
|
||||
ViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
imgIcon = itemView.findViewById(R.id.img_gift_icon);
|
||||
tvName = itemView.findViewById(R.id.tv_gift_name);
|
||||
tvCount = itemView.findViewById(R.id.tv_gift_count);
|
||||
}
|
||||
|
||||
void bind(GiftLottery item) {
|
||||
imgIcon.setImageResource(Integer.parseInt(item.getIcon()));
|
||||
tvName.setText(item.getName());
|
||||
tvCount.setText(item.getNumber());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.xscm.moduleutil.dialog.giftLottery;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import com.xscm.moduleutil.activity.IPresenter;
|
||||
import com.xscm.moduleutil.activity.IView;
|
||||
|
||||
public class GiftLotteryContacts {
|
||||
public interface View extends IView<Activity> {
|
||||
|
||||
}
|
||||
|
||||
public interface IRoomPre extends IPresenter {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package com.xscm.moduleutil.dialog.giftLottery;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.recyclerview.widget.GridLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.xscm.moduleutil.R;
|
||||
import com.xscm.moduleutil.activity.IPresenter;
|
||||
import com.xscm.moduleutil.base.BaseMvpDialogFragment;
|
||||
import com.xscm.moduleutil.databinding.DialogGiftLotteryBinding;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author qx
|
||||
* @data 2025/8/18
|
||||
* @description: 礼物抽奖
|
||||
*/
|
||||
|
||||
public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresenter, DialogGiftLotteryBinding> implements GiftLotteryContacts.View {
|
||||
|
||||
private int topCount = 4;
|
||||
private int bottomCount = 4;
|
||||
|
||||
@Override
|
||||
protected GiftLotteryPresenter bindPresenter() {
|
||||
return new GiftLotteryPresenter(this, getActivity());
|
||||
}
|
||||
public static GiftLotteryDialog newInstance(int top, int bottom) {
|
||||
GiftLotteryDialog dialog = new GiftLotteryDialog();
|
||||
Bundle args = new Bundle();
|
||||
args.putInt("top", top);
|
||||
args.putInt("bottom", bottom);
|
||||
dialog.setArguments(args);
|
||||
return dialog;
|
||||
}
|
||||
@Nullable
|
||||
@Override
|
||||
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
||||
Dialog dialog = super.onCreateDialog(savedInstanceState);
|
||||
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
dialog.setCancelable(true);
|
||||
return dialog;
|
||||
}
|
||||
@Override
|
||||
public void onAttach(@NonNull Context context) {
|
||||
super.onAttach(context);
|
||||
topCount = getArguments().getInt("top");
|
||||
bottomCount = getArguments().getInt("bottom");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initData() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initView() {
|
||||
|
||||
mBinding.recyclerGifts.setLayoutManager(new GridLayoutManager(requireContext(), 4));
|
||||
List<GiftLottery> gifts = new ArrayList<>();
|
||||
for (int i = 0; i < topCount + bottomCount; i++) {
|
||||
gifts.add(new GiftLottery(
|
||||
String.valueOf(i),
|
||||
"R.mipmap.ic_launcher",
|
||||
"66666",
|
||||
"柔情似水",
|
||||
"66666",
|
||||
false
|
||||
));
|
||||
} GiftLotteryAdapter adapter = new GiftLotteryAdapter(gifts, new GiftLotteryAdapter.OnGiftClickListener() {
|
||||
|
||||
@Override
|
||||
public void onGiftClick(GiftLottery item) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
mBinding.recyclerGifts.setAdapter(adapter);
|
||||
|
||||
// 绑定按钮点击事件
|
||||
mBinding.btnLotteryOnce.setOnClickListener(v -> {
|
||||
adapter.startScanAnimation(); // 启动扫描动画
|
||||
});
|
||||
|
||||
mBinding.btnLotteryTen.setOnClickListener(v -> {
|
||||
// 抽奖10次,每次启动一次扫描
|
||||
for (int i = 0; i < 10; i++) {
|
||||
adapter.startScanAnimation();
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
return R.layout.dialog_gift_lottery;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.xscm.moduleutil.dialog.giftLottery;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.xscm.moduleutil.presenter.BasePresenter;
|
||||
|
||||
public class GiftLotteryPresenter extends BasePresenter<GiftLotteryContacts.View> implements GiftLotteryContacts.IRoomPre {
|
||||
GiftLotteryContacts.View mView;
|
||||
public GiftLotteryPresenter(GiftLotteryContacts.View view, Context context) {
|
||||
super(view, context);
|
||||
mView = view;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user