大家好,又见面了,我是你们的朋友全栈君。
近期项目中有点餐功能要求中午和晚上一起点餐,中午和晚上的餐品加载的都是一样的有炒菜和面点,
也可设置每个菜品的起点数量,默认起点数量都是1,项目写的比较急,后期会优化代码数量,增加了布局弹出方式,可以用alert弹窗,也可以切换PopupWindows样式弹出
废话不多说直接上代码
activity页面代码:
public class AnimationActivity extends BaseActivity implements View.OnClickListener, OnAlertCallBack, OnWindoowCallBacks {
private ListView recyclerView;
private ImageView iv_pay_anim;
private List<FoodName> list;
private List<FoodName> lists;
private TextView zhong, wan;
private TextView chao, mian;
private MyAnimationAdapter adapter;
private LinearLayoutManager linearLayoutManager;
private FoodNameBean foodNameBean;
private FoodNameBean foodNameBeans;
private FoodNameBean foodNameBeanss;
private FoodNameBean foodNameBeansss;
private List<FoodNameBean> foodNameBeanList = new ArrayList<>();
private FoodNameLimit foodNameLimit = new FoodNameLimit();
private Reduce reduce = new Reduce();//减法计算
private Add add = new Add();//加法计算
private Summary summary = new Summary();//代理类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation);
recyclerView = (ListView) findViewById(R.id.anim_rv);
iv_pay_anim = (ImageView) findViewById(R.id.iv_pay_anim);
zhong = (TextView) findViewById(R.id.zhong);
wan = (TextView) findViewById(R.id.wan);
chao = (TextView) findViewById(R.id.chao);
mian = (TextView) findViewById(R.id.mian);
setText();
setOnClick();
setFoodNameLimits();
}
/**
* 设置的起点菜品和数量也可不设置默认都是一个
*/
private void setFoodNameLimits() {
foodNameBean = new FoodNameBean("包子", 3);
foodNameBeans = new FoodNameBean("面条", 2);
foodNameBeanss = new FoodNameBean("饺子", 4);
foodNameBeansss = new FoodNameBean("面包", 5);
// foodNameBeanList.add(foodNameBean);
// foodNameBeanList.add(foodNameBeans);
// foodNameBeanList.add(foodNameBeanss);
// foodNameBeanList.add(foodNameBeansss);
}
/**
* 初始化Adapter
*/
private void setText() {
DataBean dataBean = new DataBean();
list = dataBean.getStirFry();
lists = dataBean.getNoodleSport();
adapter = new MyAnimationAdapter(this, list);
recyclerView.setAdapter(adapter);
}
private void setOnClick() {
zhong.setOnClickListener(this);
wan.setOnClickListener(this);
chao.setOnClickListener(this);
mian.setOnClickListener(this);
iv_pay_anim.setOnClickListener(this);
}
private List<AllDataBean> listData = new LinkedList<>();
public void playAnimation(int[] position, TextView textView, int positions, LinearLayout linearLayout, TextView textViews, Integer money) {
setNumber(textView, linearLayout, positions, textViews, start, positionesState, money);
final ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
imageView.setLayoutParams(new LinearLayout.LayoutParams(70, 70));
ViewGroup viewGroup = (ViewGroup) this.getWindow().getDecorView();
viewGroup.addView(imageView);
int[] des = new int[2];
iv_pay_anim.getLocationInWindow(des);
/*动画开始位置,也就是物品的位置;动画结束的位置,也就是购物车的位置 */
Point startPosition = new Point(position[0], position[1]);
Point endPosition = new Point(des[0] + iv_pay_anim.getWidth() / 2 - 50, des[1] + iv_pay_anim.getHeight() / 2);
int pointX = (startPosition.x + endPosition.x) / 2 - 100;
int pointY = startPosition.y - 400;
Point controllPoint = new Point(pointX, pointY);
/*
* 属性动画,依靠TypeEvaluator来实现动画效果,其中位移,缩放,渐变,旋转都是可以直接使用
* 这里是自定义了TypeEvaluator, 我们通过point记录运动的轨迹,然后,物品随着轨迹运动,
* 一旦轨迹发生变化,就会调用addUpdateListener这个方法,我们不断的获取新的位置,是物品移动
* */
ValueAnimator valueAnimator = ValueAnimator.ofObject(new BizierEvaluator2(controllPoint), startPosition, endPosition);
valueAnimator.start();
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
Point point = (Point) valueAnimator.getAnimatedValue();
imageView.setX(point.x);
imageView.setY(point.y);
}
});
/**
* 动画结束,移除掉小圆圈
*/
valueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
ViewGroup rootView = (ViewGroup) AnimationActivity.this.getWindow().getDecorView();
rootView.removeView(imageView);
}
});
}
private int start = 1, positionesState = 1;//start 时间段的状态 positiones菜品的状态
private int reduceState;
private Integer numbers;
public void noPlayAnimation(TextView textView, int position, LinearLayout linearLayout, TextView textViews, Integer state, Integer positionesStates, int bottomAdapterposition, Integer money) {
Integer number = Integer.parseInt(textView.getText().toString());
if (number == 0) {
Toast.makeText(this, "数量已经是0不能在减", Toast.LENGTH_SHORT).show();
return;
}
int allNumber = 0;
/**
* 判断是否设置了起点类
*/
//计算的限制
if (foodNameBeanList.size() > 0) {
numbers = startLimit(false, number, textViews.getText().toString());
} else {
numbers = 1;
}
summary.setPrices(reduce);
allNumber = summary.calculateNumber(numbers, number);
reduceState = summary.calculateState(numbers, number);
if (allNumber == 0) {
AlphaAnimation aa = new AlphaAnimation(1f, 0);
aa.setDuration(1000);
linearLayout.startAnimation(aa);
linearLayout.setVisibility(View.INVISIBLE);
textView.setText(allNumber + "");
//删除并更新数据
delect(position, allNumber, textViews.getText().toString(), state, positionesStates, money);
//删除弹窗中的信息
if (state != null) {
if (listData.size() > 0) {
listData.remove(bottomAdapterposition);
bottomAdapter.setListData(listData);
bottomAdapter.notifyDataSetChanged();
}
setStatrs(start, positionesState);
adapter.notifyDataSetChanged();
return;
}
}
textView.setText(allNumber + "");
//第二种解决方法
delect(position, allNumber, textViews.getText().toString(), state, positionesStates, money);
}
/**
* 刷新数据
*
* @param positions
* @param allNumber
* @param name
* @param stare
* @param positionesStates
*/
private void delect(int positions, int allNumber, String name, Integer stare, Integer positionesStates, Integer money) {
if (stare == null) {
if (allNumber == 0) {
for (int s = 0; s < listData.size(); s++) {
if (listData.get(s).getNumber() == 0) {
listData.remove(s);
return;
}
}
}
setTwoList(positions, allNumber, name, start, positionesState, money, 2);
} else {
setTwoList(positions, allNumber, name, stare, positionesStates, money, 2);
setStatrs(start, positionesState);
adapter.notifyDataSetChanged();
}
}
public void playAnimation(int[] position, TextView textView, int positions, LinearLayout linearLayout, TextView textViews, int state, int positionesState, Integer money) {
setNumber(textView, linearLayout, positions, textViews, state, positionesState, money);
}
/**
* 计算加数量
*/
private Integer moneys = 0;
private void setNumber(TextView textView, LinearLayout linearLayout, int positions, TextView textViews, int state, int positionesState, Integer money) {
moneys = 0;
Integer allNumber = null;
Integer number = Integer.parseInt(textView.getText().toString());
//起点的限制
if (foodNameBeanList.size() > 0) {
numbers = startLimit(true, number, textViews.getText().toString());
} else {
numbers = 1;
}
/**
*优化算法
*/
summary.setPrices(add);
allNumber = summary.calculateNumber(numbers, number);
moneys = summary.calculatePriceMoney(money, numbers);
textView.setText(allNumber + "");
if (allNumber == 1) {
AlphaAnimation aa = new AlphaAnimation(0, 1f);
aa.setDuration(1000);
linearLayout.startAnimation(aa);
linearLayout.setVisibility(View.VISIBLE);
}
//第二种解决方法
setTwoList(positions, allNumber, textViews.getText().toString(), state, positionesState, money, 1);
if (state != start) {
adapter.notifyDataSetChanged();
} else {
setStatrs(state, positionesState);
adapter.notifyDataSetChanged();
}
}
private int forState;
private AlertStyle alertStyle = new AlertStyle();
/**
* 是否有限制
*/
private Integer startLimit(boolean state, Integer number, String name) {
return foodNameLimit
.setState(state)
.setNumber(number)
.setFoodName(name)
.setList(foodNameBeanList)
.LimitNumber();
}
private void setTwoList(int positions, int allNumber, String name, int start, int positionesState, Integer money, Integer addEeduceState) {
forState = 0;
if (listData.size() <= 0) {
listData.add(new AllDataBean(positions, allNumber, name, "", start, positionesState, moneys, money));
} else {
for (int s = 0; s < listData.size(); s++) {
if (listData.get(s).getStart() == start && listData.get(s).getPositiones() == positionesState && listData.get(s).getName().equals(name)) {
if (addEeduceState == 1) {//加法计算
moneys = summary.calculatePriceAddMoney(listData.get(s).getMoneys(), money);
} else if (addEeduceState == 2) {
if (reduceState == 0 || reduceState == 2) {
moneys = summary.calculatePriceMoney(listData.get(s).getMoneys(), money);
} else {
//当设置了foodNameLimit加入这句话
//String names = foodNameLimit.LimitName(listData.get(s).getName());
//调用summary.calculateReduceMoney(listData.get(s).getMoneys(), money,names, allNumber);
//一个一个减
moneys = summary.calculateNoSettingReduceMoney(listData.get(s).getMoneys(), money, "", allNumber);
}
}
listData.get(s).setPosition(positions);
listData.get(s).setNumber(allNumber);
listData.get(s).setName(name);
listData.get(s).setDay("");
listData.get(s).setMoneys(moneys);
listData.get(s).setStart(start);
listData.get(s).setPositiones(positionesState);
if (alertState == 0) {
bottomAdapter.notifyDataSetChanged();
}
forState = 1;
}
}
if (forState == 0) {
listData.add(new AllDataBean(positions, allNumber, name, "", start, positionesState, moneys, money));
}
}
getAllMoneys();
}
/**
* 计算总价格
*/
private Integer allMoney;
private void getAllMoneys() {
allMoney = 0;
for (int s = 0; s < listData.size(); s++) {
allMoney += listData.get(s).getMoneys();
}
if (alertState == 0) {
allMoneys.setText("合计:" + allMoney);
}
}
/**
* 中,晚的状态
*/
public void setStatrs(int statr, int positiones) {
switch (statr) {//中
case 1:
if (positiones == 1) {
setFors(list, listData, statr, positiones);
} else {
setFors(lists, listData, statr, positiones);
}
break;
case 2://晚
if (positiones == 1) {
setFors(list, listData, statr, positiones);
} else {
setFors(lists, listData, statr, positiones);
}
break;
}
}
/**
* 添加数据
*
* @param foodNames
* @param listData
* @param statr
* @param positionesState
*/
private void setFors(List<FoodName> foodNames, List<AllDataBean> listData, int statr, int positionesState) {
for (int i = 0; i < foodNames.size(); i++) {
foodNames.get(i).setFoodNumber(0);
for (int s = 0; s < listData.size(); s++) {
if (listData.get(s).getName().equals(foodNames.get(i).getFoodName()) && listData.get(s).getStart() == statr
&& listData.get(s).getPositiones() == positionesState) {
foodNames.get(i).setFoodNumber(listData.get(s).getNumber());
}
}
}
}
private BottomAdapter bottomAdapter;
public void setTextColor(int color, int colors) {
zhong.setTextColor(getResources().getColor(color));
wan.setTextColor(getResources().getColor(colors));
}
private AllPopupWindowsUtils allPopupWindowsUtils;
private int alertState = 1;
private TextView allMoneys;
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.zhong:
setTextColor(R.color.colorAccent, R.color.colorAccents);
start = 1;
setStatrs(start, positionesState);
adapter.notifyDataSetChanged();
break;
case R.id.wan:
setTextColor(R.color.colorAccents, R.color.colorAccent);
start = 2;
setStatrs(start, positionesState);
adapter.notifyDataSetChanged();
break;
case R.id.chao:
chao.setTextColor(getResources().getColor(R.color.colorAccent));
mian.setTextColor(getResources().getColor(R.color.colorAccents));
positionesState = 1;
setStatrs(start, positionesState);
setListView(list);
adapter.notifyDataSetChanged();
break;
case R.id.mian:
mian.setTextColor(getResources().getColor(R.color.colorAccent));
chao.setTextColor(getResources().getColor(R.color.colorAccents));
positionesState = 2;
setStatrs(start, positionesState);
setListView(lists);
adapter.notifyDataSetChanged();
break;
case R.id.iv_pay_anim:
if (listData.size() <= 0) {
Toast.makeText(AnimationActivity.this, "当前没有菜品请添加查看", Toast.LENGTH_SHORT).show();
return;
}
for (int e = 0; e < listData.size(); e++) {
if (listData.get(e).getNumber() == 0) {
listData.remove(e);
}
}
if (listData.size() <= 0) {
Toast.makeText(AnimationActivity.this, "当前没有菜品请添加查看", Toast.LENGTH_SHORT).show();
return;
}
View view = LayoutInflater.from(this).inflate(R.layout.items, null);
/**
* 切换样式
*/
alertStyle
.setActivity(this)
// .showAlert(view, this);
.showPoupWindows(chao, R.layout.items, this);
break;
case R.id.clear:
if (listData.size() <= 0) {
Toast.makeText(AnimationActivity.this, “当前没有菜品”, Toast.LENGTH_SHORT).show();
return;
}
allMoney = 0;
allMoneys.setText(“合计:” + allMoney);
listData.clear();
bottomAdapter.setListData(listData);
bottomAdapter.notifyDataSetChanged();
break;
}
}
/**
* 更换数据
*
* @param lists
*/
private void setListView(List<FoodName> lists) {
adapter = new MyAnimationAdapter(this, lists);
recyclerView.setAdapter(adapter);
}
/**
* 重新置回0
*
* @param
*/
public void setForList(List<FoodName> forList) {
for (int s = 0; s < forList.size(); s++) {
forList.get(s).setFoodNumber(0);
}
}
/**
* alert样式
*/
private AlertDialog alertDialog;
@Override
public void setOpenViews(View view, final AlertDialog alertDialog) {
this.alertDialog = alertDialog;
showAlert(view, 1);
}
/**
* PopupWindows样式
*/
private AllPopupWindows allPopupWindows;
@Override
public void setOpenViews(View view, final AllPopupWindows allPopupWindows) {
this.allPopupWindows = allPopupWindows;
showAlert(view, 2);
}
@Override
public void setColseViews() {
if (listData.size() == 0) {
setForList(list);
setForList(lists);
adapter.notifyDataSetChanged();
}
alertState = 1;
}
private void showAlert(View view, final int state) {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
TextView clear = (TextView) view.findViewById(R.id.clear);
allMoneys = (TextView) view.findViewById(R.id.allMoneys);
TextView colse = (TextView) view.findViewById(R.id.colse);
colse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (state == 1) {//alert
if (listData.size() == 0) {
setForList(list);
setForList(lists);
adapter.notifyDataSetChanged();
}
alertState = 1;
alertDialog.dismiss();
} else {//popupWindow
allPopupWindows.dismiss();
}
}
});
clear.setOnClickListener(this);
alertState = 0;
bottomAdapter = new BottomAdapter(listData, AnimationActivity.this);
RecyclerView bottomRecyclerView = (RecyclerView) view.findViewById(R.id.bottomData);
bottomRecyclerView.setLayoutManager(linearLayoutManager);
bottomRecyclerView.setAdapter(bottomAdapter);
getAllMoneys();
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/150956.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...