RecycleView下的二次贝塞尔购物车轨迹曲线
程序员文章站
2023-12-27 09:22:03
...
实现思路:
类似饿了么点餐页面,左右两个RecycleView相关联,点击左侧类别,右侧自动置顶,利用二次贝塞尔曲线公式绘制动画轨迹
左侧菜单布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:id="@+id/left_menu_item"
android:background="@drawable/selector_item_select"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/left_menu_textview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/item_dish_top_margin"
android:layout_marginBottom="@dimen/item_dish_top_margin"
android:text="@string/menu_name"
android:textSize="@dimen/item_menu_word_size"
android:textColor="@color/black"/>
</LinearLayout>
右侧菜单布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:id="@+id/right_menu_item"
android:layout_width="match_parent"
android:background="@color/light_gray"
android:layout_height="@dimen/item_dish_menu_hight">
<!--右侧RecycleView的item选中黑色显示条-->
<LinearLayout
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/item_menu_title_margin"
android:background="@color/black">
</LinearLayout>
<!--右侧RecycleView的item选中文本-->
<TextView
android:id="@+id/right_menu_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:text="@string/menu_name"
android:textSize="@dimen/item_menu_word_size"
android:textColor="@color/black"
android:background="@color/light_gray"/>
</LinearLayout>
已选商品上拉布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/right_dish_item"
android:layout_width="match_parent"
android:layout_height="@dimen/item_dish_hight"
android:orientation="vertical">
<!--菜名-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/item_dish_top_margin"
android:layout_weight="1">
<TextView
android:id="@+id/right_dish_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/item_dish_horizontal_margin"
android:text="@string/dish_name"
android:textColor="@color/black"
android:textSize="@dimen/item_dish_word_size" />
</LinearLayout>
<!--单价和数量-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/item_dish_horizontal_margin"
android:text="¥"
android:textColor="@color/red"
android:textSize="@dimen/item_dish_money_size" />
<TextView
android:id="@+id/right_dish_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/item_dish_small_horizontal_margin"
android:text="10.0"
android:textColor="@color/red"
android:textSize="@dimen/item_dish_word_size" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right|center_vertical"
android:layout_marginBottom="1dp"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:gravity="end"
android:orientation="horizontal">
<ImageView
android:id="@+id/right_dish_remove"
android:layout_width="@dimen/item_dish_circle_size"
android:layout_height="@dimen/item_dish_circle_size"
android:clickable="true"
android:src="@drawable/ic_remove_circle_outline_blue_700_36dp" />
<TextView
android:id="@+id/right_dish_account"
android:layout_width="@dimen/item_dish_account_width"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="1"
android:textColor="@color/black"
android:textSize="@dimen/item_dish_small_word_size" />
<ImageView
android:id="@+id/right_dish_add"
android:layout_width="@dimen/item_dish_circle_size"
android:layout_height="@dimen/item_dish_circle_size"
android:clickable="true"
android:src="@drawable/ic_add_circle_blue_700_36dp" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
已选商品架构布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearlayout"
android:layout_above="@+id/shopping_cart_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/deep_gray">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/shopping_cart_right_margin"
android:layout_marginTop="@dimen/shopping_cart_right_margin"
android:layout_marginBottom="@dimen/shopping_cart_right_margin"
android:layout_alignParentLeft="true"
android:text="@string/shopcart"
android:textColor="@color/black"
android:textSize="@dimen/shopping_cart_small_size"/>
<LinearLayout
android:id="@+id/clear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentRight="true"
android:clickable="true"
android:layout_marginRight="@dimen/shopping_cart_right_margin"
android:layout_marginTop="@dimen/shopping_cart_right_margin"
android:layout_marginBottom="@dimen/shopping_cart_right_margin">
<ImageView
android:layout_width="18dp"
android:layout_height="18dp"
android:src="@drawable/ic_delete_grey_500_24dp"
android:scaleType="centerCrop"
android:layout_marginRight="@dimen/item_dish_small_horizontal_margin"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clear"
android:textColor="@color/deep_gray_2"
android:textSize="@dimen/shopping_cart_small_size"/>
</LinearLayout>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="@dimen/shopping_cart_bottom_recycle_height"
android:background="@color/white">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<LinearLayout
android:id="@+id/shopping_cart_bottom"
android:layout_width="match_parent"
android:layout_height="@dimen/shopping_cart_height"
android:background="@color/light_black"
android:orientation="horizontal"
android:clickable="true"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/shopping_cart_total_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="120"
android:textColor="@color/white"
android:textAlignment="center"
android:textSize="@dimen/shopping_cart_total_price_word_size"
android:textStyle="bold"
android:visibility="gone"
android:layout_marginLeft="@dimen/shopping_cart_total_price_left_magrin"/>
</LinearLayout>
<FrameLayout
android:id="@+id/shopping_cart_layout"
android:layout_width="@dimen/shopping_cart_circle_size"
android:layout_height="@dimen/shopping_cart_circle_size"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_above="@+id/lineview"
android:clickable="true"
android:background="@drawable/circle">
<ImageView
android:id="@+id/shopping_cart"
android:layout_width="@dimen/shopping_cart_image_height"
android:layout_height="@dimen/shopping_cart_image_height"
android:layout_gravity="center"
android:src="@drawable/ic_shopping_cart_white_24dp"/>
</FrameLayout>
<TextView
android:id="@+id/lineview"
android:layout_width="match_parent"
android:layout_height="@dimen/shopping_cart_bottom_margin"
android:visibility="invisible"
android:layout_alignParentBottom="true"/>
<TextView
android:id="@+id/shopping_cart_total_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/small_red_circle"
android:text="1"
android:textColor="@color/white"
android:textSize="10sp"
android:textStyle="bold"
android:layout_above="@id/shopping_cart_bottom"
android:layout_alignRight="@id/shopping_cart_layout"/>
</RelativeLayout>
左侧RecycleView的adapter适配器
package com.example.ytf.elmapplication.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.ytf.elmapplication.R;
import com.example.ytf.elmapplication.model.DishMenu;
import java.util.ArrayList;
import java.util.List;
/**
* Created by YTF on 2017/7/25.
*/
public class LeftMenuAdapter extends RecyclerView.Adapter {
private Context mContext;
private ArrayList<DishMenu> mMenuList;
private int mSelectedNum;
private List<onItemSelectedListener> mSelectedListenerList;
public interface onItemSelectedListener{
public void onLeftItemSelected(int postion,DishMenu menu);
}
public void addItemSelectedListener(onItemSelectedListener listener){
if(mSelectedListenerList!=null)
mSelectedListenerList.add(listener);
}
public void removeItemSelectedListener(onItemSelectedListener listener){
if(mSelectedListenerList!=null && !mSelectedListenerList.isEmpty())
mSelectedListenerList.remove(listener);
}
public LeftMenuAdapter(Context mContext, ArrayList<DishMenu> mMenuList){
this.mContext = mContext;
this.mMenuList = mMenuList;
this.mSelectedNum = -1;
this.mSelectedListenerList = new ArrayList<>();
if(mMenuList.size()>0)
mSelectedNum = 0;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.left_menu_item, parent, false);
LeftMenuViewHolder viewHolder = new LeftMenuViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
DishMenu dishMenu = mMenuList.get(position);
LeftMenuViewHolder viewHolder = (LeftMenuViewHolder)holder;
viewHolder.menuName.setText(dishMenu.getMenuName());
if(mSelectedNum==position){
viewHolder.menuLayout.setSelected(true);
}else{
viewHolder.menuLayout.setSelected(false);
}
}
@Override
public int getItemCount() {
return mMenuList.size();
}
public void setSelectedNum(int selectedNum) {
if(selectedNum<getItemCount() && selectedNum>=0 ) {
this.mSelectedNum = selectedNum;
notifyDataSetChanged();
}
}
public int getSelectedNum() {
return mSelectedNum;
}
private class LeftMenuViewHolder extends RecyclerView.ViewHolder{
TextView menuName;
LinearLayout menuLayout;
public LeftMenuViewHolder(final View itemView) {
super(itemView);
menuName = (TextView)itemView.findViewById(R.id.left_menu_textview);
menuLayout = (LinearLayout)itemView.findViewById(R.id.left_menu_item);
menuLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int clickPosition = getAdapterPosition();
// setSelectedNum(clickPosition);
notifyItemSelected(clickPosition);
}
});
}
}
private void notifyItemSelected(int position) {
if(mSelectedListenerList!=null && !mSelectedListenerList.isEmpty()){
for(onItemSelectedListener listener:mSelectedListenerList){
listener.onLeftItemSelected(position,mMenuList.get(position));
}
}
}
}
右侧RecycleView的适配器adapter
package com.example.ytf.elmapplication.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.ytf.elmapplication.R;
import com.example.ytf.elmapplication.imp.ShopCartImp;
import com.example.ytf.elmapplication.model.Dish;
import com.example.ytf.elmapplication.model.DishMenu;
import com.example.ytf.elmapplication.model.ShopCart;
import java.util.ArrayList;
/**
* Created by YTF on 2017/7/25.
*/
public class RightDishAdapter extends RecyclerView.Adapter {
private final int MENU_TYPE = 0;
private final int DISH_TYPE = 1;
private final int HEAD_TYPE = 2;
private Context mContext;
private ArrayList<DishMenu> mMenuList;
private int mItemCount;
private ShopCart shopCart;
private ShopCartImp shopCartImp;
public RightDishAdapter(Context mContext, ArrayList<DishMenu> mMenuList, ShopCart shopCart){
this.mContext = mContext;
this.mMenuList = mMenuList;
this.mItemCount = mMenuList.size();
this.shopCart = shopCart;
for(DishMenu menu:mMenuList){
mItemCount+=menu.getDishList().size();
}
}
@Override
public int getItemViewType(int position) {
int sum=0;
for(DishMenu menu:mMenuList){
if(position==sum){
return MENU_TYPE;
}
sum+=menu.getDishList().size()+1;
}
return DISH_TYPE;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType==MENU_TYPE){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.right_menu_item, parent, false);
MenuViewHolder viewHolder = new MenuViewHolder(view);
return viewHolder;
}else{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.right_dish_item, parent, false);
DishViewHolder viewHolder = new DishViewHolder(view);
return viewHolder;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if(getItemViewType(position)==MENU_TYPE){
MenuViewHolder menuholder = (MenuViewHolder)holder;
if(menuholder!=null) {
menuholder.right_menu_title.setText(getMenuByPosition(position).getMenuName());
menuholder.right_menu_layout.setContentDescription(position+"");
}
}else {
final DishViewHolder dishholder = (DishViewHolder) holder;
if (dishholder != null) {
final Dish dish = getDishByPosition(position);
dishholder.right_dish_name_tv.setText(dish.getDishName());
dishholder.right_dish_price_tv.setText(dish.getDishPrice()+"");
dishholder.right_dish_layout.setContentDescription(position+"");
int count = 0;
if(shopCart.getShoppingSingleMap().containsKey(dish)){
count = shopCart.getShoppingSingleMap().get(dish);
}
if(count<=0){
dishholder.right_dish_remove_iv.setVisibility(View.GONE);
dishholder.right_dish_account_tv.setVisibility(View.GONE);
}else {
dishholder.right_dish_remove_iv.setVisibility(View.VISIBLE);
dishholder.right_dish_account_tv.setVisibility(View.VISIBLE);
dishholder.right_dish_account_tv.setText(count+"");
}
dishholder.right_dish_add_iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(shopCart.addShoppingSingle(dish)) {
notifyItemChanged(position);
if(shopCartImp!=null)
shopCartImp.add(view,position);
}
}
});
dishholder.right_dish_remove_iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(shopCart.subShoppingSingle(dish)){
notifyItemChanged(position);
if(shopCartImp!=null)
shopCartImp.remove(view,position);
}
}
});
}
}
}
public DishMenu getMenuByPosition(int position){
int sum =0;
for(DishMenu menu:mMenuList){
if(position==sum){
return menu;
}
sum+=menu.getDishList().size()+1;
}
return null;
}
public Dish getDishByPosition(int position){
for(DishMenu menu:mMenuList){
if(position>0 && position<=menu.getDishList().size()){
return menu.getDishList().get(position-1);
}
else{
position-=menu.getDishList().size()+1;
}
}
return null;
}
public DishMenu getMenuOfMenuByPosition(int position){
for(DishMenu menu:mMenuList){
if(position==0)return menu;
if(position>0 && position<=menu.getDishList().size()){
return menu;
}
else{
position-=menu.getDishList().size()+1;
}
}
return null;
}
@Override
public int getItemCount() {
return mItemCount;
}
public ShopCartImp getShopCartImp() {
return shopCartImp;
}
public void setShopCartImp(ShopCartImp shopCartImp) {
this.shopCartImp = shopCartImp;
}
private class MenuViewHolder extends RecyclerView.ViewHolder{
private LinearLayout right_menu_layout;
private TextView right_menu_title;
public MenuViewHolder(View itemView) {
super(itemView);
right_menu_layout = itemView.findViewById(R.id.right_menu_item);
right_menu_title = (TextView)itemView.findViewById(R.id.right_menu_tv);
}
}
private class DishViewHolder extends RecyclerView.ViewHolder{
private TextView right_dish_name_tv;
private TextView right_dish_price_tv;
private LinearLayout right_dish_layout;
private ImageView right_dish_remove_iv;
private ImageView right_dish_add_iv;
private TextView right_dish_account_tv;
public DishViewHolder(View itemView) {
super(itemView);
right_dish_name_tv = (TextView)itemView.findViewById(R.id.right_dish_name);
right_dish_price_tv = (TextView)itemView.findViewById(R.id.right_dish_price);
right_dish_layout = (LinearLayout)itemView.findViewById(R.id.right_dish_item);
right_dish_remove_iv = (ImageView)itemView.findViewById(R.id.right_dish_remove);
right_dish_add_iv = (ImageView)itemView.findViewById(R.id.right_dish_add);
right_dish_account_tv = (TextView) itemView.findViewById(R.id.right_dish_account);
}
}
}
已选商品的上拉适配器
package com.example.ytf.elmapplication.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.ytf.elmapplication.R;
import com.example.ytf.elmapplication.imp.ShopCartImp;
import com.example.ytf.elmapplication.model.Dish;
import com.example.ytf.elmapplication.model.DishMenu;
import com.example.ytf.elmapplication.model.ShopCart;
import java.util.ArrayList;
/**
* Created by YTF on 2017/7/25.
*/
public class RightDishAdapter extends RecyclerView.Adapter {
private final int MENU_TYPE = 0;
private final int DISH_TYPE = 1;
private final int HEAD_TYPE = 2;
private Context mContext;
private ArrayList<DishMenu> mMenuList;
private int mItemCount;
private ShopCart shopCart;
private ShopCartImp shopCartImp;
public RightDishAdapter(Context mContext, ArrayList<DishMenu> mMenuList, ShopCart shopCart){
this.mContext = mContext;
this.mMenuList = mMenuList;
this.mItemCount = mMenuList.size();
this.shopCart = shopCart;
for(DishMenu menu:mMenuList){
mItemCount+=menu.getDishList().size();
}
}
@Override
public int getItemViewType(int position) {
int sum=0;
for(DishMenu menu:mMenuList){
if(position==sum){
return MENU_TYPE;
}
sum+=menu.getDishList().size()+1;
}
return DISH_TYPE;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType==MENU_TYPE){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.right_menu_item, parent, false);
MenuViewHolder viewHolder = new MenuViewHolder(view);
return viewHolder;
}else{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.right_dish_item, parent, false);
DishViewHolder viewHolder = new DishViewHolder(view);
return viewHolder;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if(getItemViewType(position)==MENU_TYPE){
MenuViewHolder menuholder = (MenuViewHolder)holder;
if(menuholder!=null) {
menuholder.right_menu_title.setText(getMenuByPosition(position).getMenuName());
menuholder.right_menu_layout.setContentDescription(position+"");
}
}else {
final DishViewHolder dishholder = (DishViewHolder) holder;
if (dishholder != null) {
final Dish dish = getDishByPosition(position);
dishholder.right_dish_name_tv.setText(dish.getDishName());
dishholder.right_dish_price_tv.setText(dish.getDishPrice()+"");
dishholder.right_dish_layout.setContentDescription(position+"");
int count = 0;
if(shopCart.getShoppingSingleMap().containsKey(dish)){
count = shopCart.getShoppingSingleMap().get(dish);
}
if(count<=0){
dishholder.right_dish_remove_iv.setVisibility(View.GONE);
dishholder.right_dish_account_tv.setVisibility(View.GONE);
}else {
dishholder.right_dish_remove_iv.setVisibility(View.VISIBLE);
dishholder.right_dish_account_tv.setVisibility(View.VISIBLE);
dishholder.right_dish_account_tv.setText(count+"");
}
dishholder.right_dish_add_iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(shopCart.addShoppingSingle(dish)) {
notifyItemChanged(position);
if(shopCartImp!=null)
shopCartImp.add(view,position);
}
}
});
dishholder.right_dish_remove_iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(shopCart.subShoppingSingle(dish)){
notifyItemChanged(position);
if(shopCartImp!=null)
shopCartImp.remove(view,position);
}
}
});
}
}
}
public DishMenu getMenuByPosition(int position){
int sum =0;
for(DishMenu menu:mMenuList){
if(position==sum){
return menu;
}
sum+=menu.getDishList().size()+1;
}
return null;
}
public Dish getDishByPosition(int position){
for(DishMenu menu:mMenuList){
if(position>0 && position<=menu.getDishList().size()){
return menu.getDishList().get(position-1);
}
else{
position-=menu.getDishList().size()+1;
}
}
return null;
}
public DishMenu getMenuOfMenuByPosition(int position){
for(DishMenu menu:mMenuList){
if(position==0)return menu;
if(position>0 && position<=menu.getDishList().size()){
return menu;
}
else{
position-=menu.getDishList().size()+1;
}
}
return null;
}
@Override
public int getItemCount() {
return mItemCount;
}
public ShopCartImp getShopCartImp() {
return shopCartImp;
}
public void setShopCartImp(ShopCartImp shopCartImp) {
this.shopCartImp = shopCartImp;
}
private class MenuViewHolder extends RecyclerView.ViewHolder{
private LinearLayout right_menu_layout;
private TextView right_menu_title;
public MenuViewHolder(View itemView) {
super(itemView);
right_menu_layout = itemView.findViewById(R.id.right_menu_item);
right_menu_title = (TextView)itemView.findViewById(R.id.right_menu_tv);
}
}
private class DishViewHolder extends RecyclerView.ViewHolder{
private TextView right_dish_name_tv;
private TextView right_dish_price_tv;
private LinearLayout right_dish_layout;
private ImageView right_dish_remove_iv;
private ImageView right_dish_add_iv;
private TextView right_dish_account_tv;
public DishViewHolder(View itemView) {
super(itemView);
right_dish_name_tv = (TextView)itemView.findViewById(R.id.right_dish_name);
right_dish_price_tv = (TextView)itemView.findViewById(R.id.right_dish_price);
right_dish_layout = (LinearLayout)itemView.findViewById(R.id.right_dish_item);
right_dish_remove_iv = (ImageView)itemView.findViewById(R.id.right_dish_remove);
right_dish_add_iv = (ImageView)itemView.findViewById(R.id.right_dish_add);
right_dish_account_tv = (TextView) itemView.findViewById(R.id.right_dish_account);
}
}
}
商品接口
package com.example.ytf.elmapplication.imp;
import android.view.View;
/**
* Created by YTF on 2017/7/25.
*/
public interface ShopCartImp {
void add(View view, int postion);
void remove(View view, int postion);
}
菜单属性类
package com.example.ytf.elmapplication.model;
/**
* Created by YTF on 2017/7/25.
* 菜单属性类
*/
public class Dish {
private String dishName;
private double dishPrice;
private int dishAmount;
private int dishRemain;
public Dish(String dishName, double dishPrice, int dishAmount) {
this.dishName = dishName;
this.dishPrice = dishPrice;
this.dishAmount = dishAmount;
this.dishRemain = dishAmount;
}
public String getDishName() {
return dishName;
}
public void setDishName(String dishName) {
this.dishName = dishName;
}
public double getDishPrice() {
return dishPrice;
}
public void setDishPrice(double dishPrice) {
this.dishPrice = dishPrice;
}
public int getDishAmount() {
return dishAmount;
}
public void setDishAmount(int dishAmount) {
this.dishAmount = dishAmount;
}
public int getDishRemain() {
return dishRemain;
}
public void setDishRemain(int dishRemain) {
this.dishRemain = dishRemain;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
return obj instanceof Dish &&
this.dishName.equals(((Dish)obj).dishName) &&
this.dishPrice == ((Dish)obj).dishPrice &&
this.dishAmount == ((Dish)obj).dishAmount &&
this.dishRemain == ((Dish)obj).dishRemain;
}
public int hashCode(){
int code = this.dishName.hashCode()+(int)this.dishPrice;
return code;
}
}
菜单名称实体类
package com.example.ytf.elmapplication.model;
import java.util.ArrayList;
/**
* Created by YTF on 2017/7/25.
*/
public class DishMenu {
private String menuName;
private ArrayList<Dish> dishList;
public DishMenu(){
}
public DishMenu(String menuName, ArrayList<Dish> dishList) {
this.menuName = menuName;
this.dishList = dishList;
}
public String getMenuName() {
return menuName;
}
public void setMenuName(String menuName) {
this.menuName = menuName;
}
public ArrayList<Dish> getDishList() {
return dishList;
}
public void setDishList(ArrayList<Dish> dishList) {
this.dishList = dishList;
}
}
已选商品的数量价格实体类
package com.example.ytf.elmapplication.model;
import android.util.Log;
import java.util.HashMap;
import java.util.Map;
/**
* Created by YTF on 2017/7/25.
*/
public class ShopCart {
private int shoppingAccount;//商品总数
private double shoppingTotalPrice;//商品总价钱
private Map<Dish, Integer> shoppingSingle;//单个物品的总价价钱
public ShopCart() {
this.shoppingAccount = 0;
this.shoppingTotalPrice = 0;
this.shoppingSingle = new HashMap<>();
}
public int getShoppingAccount() {
return shoppingAccount;
}
public double getShoppingTotalPrice() {
return shoppingTotalPrice;
}
public Map<Dish, Integer> getShoppingSingleMap() {
return shoppingSingle;
}
// 增加商品数量
public boolean addShoppingSingle(Dish dish) {
int remain = dish.getDishRemain();
if (remain <= 0)
return false;
dish.setDishRemain(--remain);
int num = 0;
if (shoppingSingle.containsKey(dish)) {
num = shoppingSingle.get(dish);
}
num += 1;
shoppingSingle.put(dish, num);
Log.e("TAG", "addShoppingSingle: " + shoppingSingle.get(dish));
shoppingTotalPrice += dish.getDishPrice();
shoppingAccount++;
return true;
}
//减去商品数量
public boolean subShoppingSingle(Dish dish) {
int num = 0;
if (shoppingSingle.containsKey(dish)) {
num = shoppingSingle.get(dish);
}
if (num <= 0) return false;
num--;
int remain = dish.getDishRemain();
dish.setDishRemain(++remain);
shoppingSingle.put(dish, num);
if (num == 0) shoppingSingle.remove(dish);
shoppingTotalPrice -= dish.getDishPrice();
shoppingAccount--;
return true;
}
// 获取选中商品数量
public int getDishAccount() {
return shoppingSingle.size();
}
//清空选中商品数量
public void clear() {
this.shoppingAccount = 0;
this.shoppingTotalPrice = 0;
this.shoppingSingle.clear();
}
}
自定义view1
package com.example.ytf.elmapplication.wiget;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.PointF;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* Created by YTF on 2017/7/25.
* 自定义Imageview控件
*/
@SuppressLint("AppCompatCustomView")
public class FakeAddImageView extends ImageView {
private PointF mPointF;
public FakeAddImageView(Context context) {
super(context);
}
public FakeAddImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public FakeAddImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public FakeAddImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void setMPointF(PointF pointF) {
setX(pointF.x);
setY(pointF.y);
}
public PointF getmPointF() {
return mPointF;
}
public void setmPointF(PointF mPointF) {
setX(mPointF.x);
setY(mPointF.y);
}
}
自定义view2二次贝塞尔曲线绘制轨迹
package com.example.ytf.elmapplication.wiget;
/**
* Created by YTF on 2017/7/25.
*/
import android.animation.TypeEvaluator;
import android.graphics.PointF;
/**
* 自定义估值器
*/
public class PointFTypeEvaluator implements TypeEvaluator<PointF> {
/**
* 每个估值器对应一个属性动画,每个属性动画仅对应唯一一个控制点
*/
PointF control;
/**
* 估值器返回值
*/
PointF mPointF = new PointF();
public PointFTypeEvaluator(PointF control) {
this.control = control;
}
@Override
public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
return getBezierPoint(startValue, endValue, control, fraction);
}
/**
* 二次贝塞尔曲线公式
*
* @param start 开始的数据点
* @param end 结束的数据点
* @param control 控制点
* @param t float 0-1
* @return 不同t对应的PointF
*/
private PointF getBezierPoint(PointF start, PointF end, PointF control, float t) {
mPointF.x = (1 - t) * (1 - t) * start.x + 2 * t * (1 - t) * control.x + t * t * end.x;
mPointF.y = (1 - t) * (1 - t) * start.y + 2 * t * (1 - t) * control.y + t * t * end.y;
return mPointF;
}
}
自定义view3自定义分割线
package com.example.ytf.elmapplication.wiget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* Created by YTF on 2017/7/25.
* 自定义RecycleView的分割线
*/
public class RecycleViewDivider extends RecyclerView.ItemDecoration {
private Paint mPaint;
private Drawable mDivider;
private int mDividerHeight = 2;//分割线高度,默认为1px
private int mOrientation;//列表的方向:LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
/**
* 默认分割线:高度为2px,颜色为灰色
*
* @param context
* @param orientation 列表方向
*/
public RecycleViewDivider(Context context, int orientation) {
if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
throw new IllegalArgumentException("请输入正确的参数!");
}
mOrientation = orientation;
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
}
/**
* 自定义分割线
*
* @param context
* @param orientation 列表方向
* @param drawableId 分割线图片
*/
public RecycleViewDivider(Context context, int orientation, int drawableId) {
this(context, orientation);
mDivider = ContextCompat.getDrawable(context, drawableId);
mDividerHeight = mDivider.getIntrinsicHeight();
}
/**
* 自定义分割线
*
* @param context
* @param orientation 列表方向
* @param dividerHeight 分割线高度
* @param dividerColor 分割线颜色
*/
public RecycleViewDivider(Context context, int orientation, int dividerHeight, int dividerColor) {
this(context, orientation);
mDividerHeight = dividerHeight;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(dividerColor);
mPaint.setStyle(Paint.Style.FILL);
}
//获取分割线尺寸
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0, 0, 0, mDividerHeight);
}
//绘制分割线
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
if (mOrientation == LinearLayoutManager.VERTICAL) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
//绘制横向 item 分割线
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + layoutParams.bottomMargin;
final int bottom = top + mDividerHeight;
if (mDivider != null) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
if (mPaint != null) {
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
}
//绘制纵向 item 分割线
private void drawVertical(Canvas canvas, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int left = child.getRight() + layoutParams.rightMargin;
final int right = left + mDividerHeight;
if (mDivider != null) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
if (mPaint != null) {
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
}
}
自定义view4
package com.example.ytf.elmapplication.wiget;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.ytf.elmapplication.R;
import com.example.ytf.elmapplication.adapter.PopupDishAdapter;
import com.example.ytf.elmapplication.imp.ShopCartImp;
import com.example.ytf.elmapplication.model.ShopCart;
/**
* Created by YTF on 2017/7/25.
*/
public class ShopCartDialog extends Dialog implements View.OnClickListener,ShopCartImp {
private LinearLayout linearLayout,bottomLayout,clearLayout;
private FrameLayout shopingcartLayout;
private ShopCart shopCart;
private TextView totalPriceTextView;
private TextView totalPriceNumTextView;
private RecyclerView recyclerView;
private PopupDishAdapter dishAdapter;
private ShopCartDialogImp shopCartDialogImp;
public ShopCartDialog(Context context, ShopCart shopCart, int themeResId) {
super(context,themeResId);
this.shopCart = shopCart;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cart_popupview);
linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
clearLayout = (LinearLayout)findViewById(R.id.clear_layout);
shopingcartLayout = (FrameLayout)findViewById(R.id.shopping_cart_layout);
bottomLayout = (LinearLayout)findViewById(R.id.shopping_cart_bottom);
totalPriceTextView = (TextView)findViewById(R.id.shopping_cart_total_tv);
totalPriceNumTextView = (TextView)findViewById(R.id.shopping_cart_total_num);
recyclerView = (RecyclerView)findViewById(R.id.recycleview);
shopingcartLayout.setOnClickListener(this);
bottomLayout.setOnClickListener(this);
clearLayout.setOnClickListener(this);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
dishAdapter = new PopupDishAdapter(getContext(),shopCart);
recyclerView.setAdapter(dishAdapter);
dishAdapter.setShopCartImp(this);
showTotalPrice();
}
@Override
public void show() {
super.show();
animationShow(1000);
}
@Override
public void dismiss() {
animationHide(1000);
}
private void showTotalPrice(){
if(shopCart!=null && shopCart.getShoppingTotalPrice()>0){
totalPriceTextView.setVisibility(View.VISIBLE);
totalPriceTextView.setText("¥ "+shopCart.getShoppingTotalPrice());
totalPriceNumTextView.setVisibility(View.VISIBLE);
totalPriceNumTextView.setText(""+shopCart.getShoppingAccount());
}else {
totalPriceTextView.setVisibility(View.GONE);
totalPriceNumTextView.setVisibility(View.GONE);
}
}
private void animationShow(int mDuration) {
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(
ObjectAnimator.ofFloat(linearLayout, "translationY",1000, 0).setDuration(mDuration)
);
animatorSet.start();
}
private void animationHide(int mDuration) {
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(
ObjectAnimator.ofFloat(linearLayout, "translationY",0,1000).setDuration(mDuration)
);
animatorSet.start();
if(shopCartDialogImp!=null){
shopCartDialogImp.dialogDismiss();
}
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
ShopCartDialog.super.dismiss();
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.shopping_cart_bottom:
case R.id.shopping_cart_layout:
this.dismiss();
break;
case R.id.clear_layout:
clear();
break;
}
}
@Override
public void add(View view, int postion) {
showTotalPrice();
}
@Override
public void remove(View view, int postion) {
showTotalPrice();
if(shopCart.getShoppingAccount()==0){
this.dismiss();
}
}
public ShopCartDialogImp getShopCartDialogImp() {
return shopCartDialogImp;
}
public void setShopCartDialogImp(ShopCartDialogImp shopCartDialogImp) {
this.shopCartDialogImp = shopCartDialogImp;
}
public interface ShopCartDialogImp{
public void dialogDismiss();
}
public void clear(){
shopCart.clear();
showTotalPrice();
if(shopCart.getShoppingAccount()==0){
this.dismiss();
}
}
}
MainAcvitity填充数据,实现逻辑
package com.example.ytf.elmapplication;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.PointF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.ytf.elmapplication.adapter.LeftMenuAdapter;
import com.example.ytf.elmapplication.adapter.RightDishAdapter;
import com.example.ytf.elmapplication.imp.ShopCartImp;
import com.example.ytf.elmapplication.model.Dish;
import com.example.ytf.elmapplication.model.DishMenu;
import com.example.ytf.elmapplication.model.ShopCart;
import com.example.ytf.elmapplication.wiget.FakeAddImageView;
import com.example.ytf.elmapplication.wiget.PointFTypeEvaluator;
import com.example.ytf.elmapplication.wiget.ShopCartDialog;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements LeftMenuAdapter.onItemSelectedListener,ShopCartImp,ShopCartDialog.ShopCartDialogImp {
private final static String TAG = "MainActivity";
private RecyclerView leftMenu;//左侧菜单栏
private RecyclerView rightMenu;//右侧菜单栏
private TextView headerView;
private LinearLayout headerLayout;//右侧菜单栏最上面的菜单
private LinearLayout bottomLayout;
private DishMenu headMenu;
private LeftMenuAdapter leftAdapter;
private RightDishAdapter rightAdapter;
private ArrayList<DishMenu> dishMenuList;//数据源
private boolean leftClickType = false;//左侧菜单点击引发的右侧联动
private ShopCart shopCart;
// private FakeAddImageView fakeAddImageView;
private ImageView shoppingCartView;
private FrameLayout shopingCartLayout;
private TextView totalPriceTextView;
private TextView totalPriceNumTextView;
private RelativeLayout mainLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
initAdapter();
}
private void initView(){
mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
leftMenu = (RecyclerView)findViewById(R.id.left_menu);
rightMenu = (RecyclerView)findViewById(R.id.right_menu);
headerView = (TextView)findViewById(R.id.right_menu_tv);
headerLayout = (LinearLayout)findViewById(R.id.right_menu_item);
// fakeAddImageView = (FakeAddImageView)findViewById(R.id.right_dish_fake_add);
bottomLayout = (LinearLayout)findViewById(R.id.shopping_cart_bottom);
shoppingCartView = (ImageView) findViewById(R.id.shopping_cart);
shopingCartLayout = (FrameLayout) findViewById(R.id.shopping_cart_layout);
totalPriceTextView = (TextView)findViewById(R.id.shopping_cart_total_tv);
totalPriceNumTextView = (TextView)findViewById(R.id.shopping_cart_total_num);
leftMenu.setLayoutManager(new LinearLayoutManager(this));
rightMenu.setLayoutManager(new LinearLayoutManager(this));
rightMenu.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if( recyclerView.canScrollVertically(1)==false) {//无法下滑
showHeadView();
return;
}
View underView = null;
if(dy>0)
underView = rightMenu.findChildViewUnder(headerLayout.getX(),headerLayout.getMeasuredHeight()+1);
else
underView = rightMenu.findChildViewUnder(headerLayout.getX(),0);
if(underView!=null && underView.getContentDescription()!=null ){
int position = Integer.parseInt(underView.getContentDescription().toString());
DishMenu menu = rightAdapter.getMenuOfMenuByPosition(position);
if(leftClickType || !menu.getMenuName().equals(headMenu.getMenuName())) {
if (dy> 0 && headerLayout.getTranslationY()<=1 && headerLayout.getTranslationY()>= -1 * headerLayout.getMeasuredHeight()*4/5 && !leftClickType) {// underView.getTop()>9
int dealtY = underView.getTop() - headerLayout.getMeasuredHeight();
headerLayout.setTranslationY(dealtY);
// Log.e(TAG, "onScrolled: "+headerLayout.getTranslationY()+" "+headerLayout.getBottom()+" - "+headerLayout.getMeasuredHeight() );
}
else if(dy<0 && headerLayout.getTranslationY()<=0 && !leftClickType) {
headerView.setText(menu.getMenuName());
int dealtY = underView.getBottom() - headerLayout.getMeasuredHeight();
headerLayout.setTranslationY(dealtY);
// Log.e(TAG, "onScrolled: "+headerLayout.getTranslationY()+" "+headerLayout.getBottom()+" - "+headerLayout.getMeasuredHeight() );
}
else{
headerLayout.setTranslationY(0);
headMenu = menu;
headerView.setText(headMenu.getMenuName());
for (int i = 0; i < dishMenuList.size(); i++) {
if (dishMenuList.get(i) == headMenu) {
leftAdapter.setSelectedNum(i);
break;
}
}
if(leftClickType)leftClickType=false;
Log.e(TAG, "onScrolled: "+menu.getMenuName() );
}
}
}
}
});
shopingCartLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showCart(view);
}
});
}
private void initData(){
shopCart = new ShopCart();
dishMenuList = new ArrayList<>();
ArrayList<Dish> dishs1 = new ArrayList<>();
dishs1.add(new Dish("面包",1.0,10));
dishs1.add(new Dish("蛋挞",1.0,10));
dishs1.add(new Dish("牛奶",1.0,10));
dishs1.add(new Dish("肠粉",1.0,10));
dishs1.add(new Dish("绿茶饼",1.0,10));
dishs1.add(new Dish("花卷",1.0,10));
dishs1.add(new Dish("包子",1.0,10));
DishMenu breakfast = new DishMenu("早点",dishs1);
ArrayList<Dish> dishs2 = new ArrayList<>();
dishs2.add(new Dish("粥",1.0,10));
dishs2.add(new Dish("炒饭",1.0,10));
dishs2.add(new Dish("炒米粉",1.0,10));
dishs2.add(new Dish("炒粿条",1.0,10));
dishs2.add(new Dish("炒牛河",1.0,10));
dishs2.add(new Dish("炒菜",1.0,10));
DishMenu launch = new DishMenu("午餐",dishs2);
ArrayList<Dish> dishs3 = new ArrayList<>();
dishs3.add(new Dish("淋菜",1.0,10));
dishs3.add(new Dish("川菜",1.0,10));
dishs3.add(new Dish("湘菜",1.0,10));
dishs3.add(new Dish("粤菜",1.0,10));
dishs3.add(new Dish("赣菜",1.0,10));
dishs3.add(new Dish("东北菜",1.0,10));
DishMenu evening = new DishMenu("晚餐",dishs3);
ArrayList<Dish> dishs4 = new ArrayList<>();
dishs4.add(new Dish("淋菜",1.0,10));
dishs4.add(new Dish("川菜",1.0,10));
dishs4.add(new Dish("湘菜",1.0,10));
dishs4.add(new Dish("粤菜",1.0,10));
dishs4.add(new Dish("赣菜",1.0,10));
dishs4.add(new Dish("东北菜",1.0,10));
DishMenu menu1 = new DishMenu("夜宵",dishs3);
dishMenuList.add(breakfast);
dishMenuList.add(launch);
dishMenuList.add(evening);
dishMenuList.add(menu1);
}
private void initAdapter(){
leftAdapter = new LeftMenuAdapter(this,dishMenuList);
rightAdapter = new RightDishAdapter(this,dishMenuList,shopCart);
rightMenu.setAdapter(rightAdapter);
leftMenu.setAdapter(leftAdapter);
leftAdapter.addItemSelectedListener(this);
rightAdapter.setShopCartImp(this);
initHeadView();
}
private void initHeadView(){
headMenu = rightAdapter.getMenuOfMenuByPosition(0);
headerLayout.setContentDescription("0");
headerView.setText(headMenu.getMenuName());
}
@Override
protected void onDestroy() {
super.onDestroy();
leftAdapter.removeItemSelectedListener(this);
}
private void showHeadView(){
headerLayout.setTranslationY(0);
View underView = rightMenu.findChildViewUnder(headerView.getX(),0);
if(underView!=null && underView.getContentDescription()!=null){
int position = Integer.parseInt(underView.getContentDescription().toString());
DishMenu menu = rightAdapter.getMenuOfMenuByPosition(position+1);
headMenu = menu;
headerView.setText(headMenu.getMenuName());
for (int i = 0; i < dishMenuList.size(); i++) {
if (dishMenuList.get(i) == headMenu) {
leftAdapter.setSelectedNum(i);
break;
}
}
}
}
@Override
public void onLeftItemSelected(int position, DishMenu menu) {
int sum=0;
for(int i = 0;i<position;i++){
sum+=dishMenuList.get(i).getDishList().size()+1;
}
LinearLayoutManager layoutManager = (LinearLayoutManager) rightMenu.getLayoutManager();
layoutManager.scrollToPositionWithOffset(sum,0);
leftClickType = true;
}
@Override
public void add(View view,int position) {
int[] addLocation = new int[2];
int[] cartLocation = new int[2];
int[] recycleLocation = new int[2];
view.getLocationInWindow(addLocation);
shoppingCartView.getLocationInWindow(cartLocation);
rightMenu.getLocationInWindow(recycleLocation);
PointF startP = new PointF();
PointF endP = new PointF();
PointF controlP = new PointF();
startP.x = addLocation[0];
startP.y = addLocation[1]-recycleLocation[1];
endP.x = cartLocation[0];
endP.y = cartLocation[1]-recycleLocation[1];
controlP.x = endP.x;
controlP.y = startP.y;
final FakeAddImageView fakeAddImageView = new FakeAddImageView(this);
mainLayout.addView(fakeAddImageView);
fakeAddImageView.setImageResource(R.drawable.ic_add_circle_blue_700_36dp);
fakeAddImageView.getLayoutParams().width = getResources().getDimensionPixelSize(R.dimen.item_dish_circle_size);
fakeAddImageView.getLayoutParams().height = getResources().getDimensionPixelSize(R.dimen.item_dish_circle_size);
fakeAddImageView.setVisibility(View.VISIBLE);
ObjectAnimator addAnimator = ObjectAnimator.ofObject(fakeAddImageView, "mPointF",
new PointFTypeEvaluator(controlP), startP, endP);
addAnimator.setInterpolator(new AccelerateInterpolator());
addAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
fakeAddImageView.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animator animator) {
fakeAddImageView.setVisibility(View.GONE);
mainLayout.removeView(fakeAddImageView);
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
ObjectAnimator scaleAnimatorX = new ObjectAnimator().ofFloat(shoppingCartView,"scaleX", 0.6f, 1.0f);
ObjectAnimator scaleAnimatorY = new ObjectAnimator().ofFloat(shoppingCartView,"scaleY", 0.6f, 1.0f);
scaleAnimatorX.setInterpolator(new AccelerateInterpolator());
scaleAnimatorY.setInterpolator(new AccelerateInterpolator());
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(scaleAnimatorX).with(scaleAnimatorY).after(addAnimator);
animatorSet.setDuration(800);
animatorSet.start();
showTotalPrice();
}
@Override
public void remove(View view,int position) {
showTotalPrice();
}
private void showTotalPrice(){
if(shopCart!=null && shopCart.getShoppingTotalPrice()>0){
totalPriceTextView.setVisibility(View.VISIBLE);
totalPriceTextView.setText("¥ "+shopCart.getShoppingTotalPrice());
totalPriceNumTextView.setVisibility(View.VISIBLE);
totalPriceNumTextView.setText(""+shopCart.getShoppingAccount());
}else {
totalPriceTextView.setVisibility(View.GONE);
totalPriceNumTextView.setVisibility(View.GONE);
}
}
private void showCart(View view) {
if(shopCart!=null && shopCart.getShoppingAccount()>0){
ShopCartDialog dialog = new ShopCartDialog(this,shopCart,R.style.cartdialog);
Window window = dialog.getWindow();
dialog.setShopCartDialogImp(this);
dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(true);
dialog.show();
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.BOTTOM;
params.dimAmount =0.5f;
window.setAttributes(params);
}
}
@Override
public void dialogDismiss() {
showTotalPrice();
rightAdapter.notifyDataSetChanged();
}
}