简单购物车逻辑

MainActivity.xml:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".v.MainActivity">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>

Fragmentone.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".fragment.Fragmentone">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8">
    </android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="全选"
            android:textSize="20sp"/>

        <CheckBox
            android:id="@+id/quanxuan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="结算"
            android:textSize="20sp"
            android:layout_marginLeft="50dp"/>

        <TextView
            android:id="@+id/text_sum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="0.00"
            android:textSize="20sp"/>
    </LinearLayout>

</LinearLayout>

Fragmenttwo.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:qq="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".fragment.Fragmenttwo">

    <Button
        android:id="@+id/shuxing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="属性动画"/>

  
</LinearLayout>

最外层适配器的布局:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/checkbox_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/shangjia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="商家"
            android:textSize="20sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
    </LinearLayout>
</LinearLayout>
里面一层的适配器:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/checkbox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/imageview"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="网络"
            android:textSize="20sp"/>

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="网络"
            android:layout_marginLeft="100dp"
            android:textSize="20sp"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button1"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:text="+"
                android:layout_marginLeft="100dp"/>

            <TextView
                android:id="@+id/text3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="xx"
                android:padding="10dp"
                android:textSize="20sp"/>

            <Button
                android:id="@+id/button2"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:text="-"
                />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
ContarctInterface:

public interface ContarctInterface {
    //v层接口
    public interface ProductViewInterface{

        public void ShowDisplay(Object object);

    }
    //p层接口
    public interface PresenterInterface{

        public void toProduct();
        
    }
}
Model层:
Callback callback;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String json= (String) msg.obj;
            Gson gson=new Gson();
            ShowBean showBean=gson.fromJson(json,ShowBean.class);
            callback.success(showBean);

        }
    };

    public void getRequest() {
        OkHttpUtil util=OkHttpUtil.getInstance();

        util.doGet(new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String json=response.body().string();
                Message message=new Message();
                message.obj=json;
                handler.sendMessage(message);
            }
        });
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public interface Callback{
        public void success(Object object);
    }
}
Product层:
public class MyPresenter<T>implements ContarctInterface.PresenterInterface {
    T tt;
    MyModel model;

    public MyPresenter(T tt) {
        this.tt = tt;
        model=new MyModel();
    }

    @Override
    public void toProduct() {

        model.setCallback(new MyModel.Callback() {
            @Override
            public void success(Object object) {
                ContarctInterface.ProductViewInterface productViewInterface= (ContarctInterface.ProductViewInterface) tt;
                productViewInterface.ShowDisplay(object);
            }
        });
        model.getRequest();
    }
}
View层:
public class MainActivity extends AppCompatActivity {

    public TextView mTextMessage;
    public ViewPager viewpager;
    public List<Fragment> list;
    public BottomNavigationView navigate;

    public BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    viewpager.setCurrentItem(0);
                    return true;
                case R.id.navigation_dashboard:
                    viewpager.setCurrentItem(1);
                    return true;
                case R.id.navigation_notifications:
                    viewpager.setCurrentItem(2);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        viewpager=findViewById(R.id.viewpager);
        navigate=findViewById(R.id.navigation);

        list=new ArrayList<Fragment>();
        Fragmentone fragmentone=new Fragmentone();
        Fragmenttwo fragmenttwo=new Fragmenttwo();
        Fragmentthree fragmentthree=new Fragmentthree();

        list.add(fragmentone);
        list.add(fragmenttwo);
        list.add(fragmentthree);

        viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return list.get(i);
            }

            @Override
            public int getCount() {
                return 3;
            }
        });
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {

                switch (i){
                    case 0:
                        navigate.setSelectedItemId(R.id.navigation_home);
                        break;
                    case 1:
                        navigate.setSelectedItemId(R.id.navigation_dashboard);
                        break;
                    case 2:
                        navigate.setSelectedItemId(R.id.navigation_notifications);
                        break;

                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

}
Util层:
public class OkHttpUtil {
    static OkHttpUtil util;
    OkHttpClient okHttpClient;

    private OkHttpUtil(){
        okHttpClient=new OkHttpClient.Builder().addInterceptor(new MyInterceptor()).build();
    }
    public static OkHttpUtil getInstance() {
        if (util == null) {
            util=new OkHttpUtil();
        }
        return util;
    }

    public void doGet(Callback callback){
        Request request=new Request.Builder()
                .url("http://172.17.8.100/ks/product/getCarts?uid=51")
                .get()
                .build();
        Call call=okHttpClient.newCall(request);
        call.enqueue(callback);
    }
    public class MyInterceptor implements Interceptor{

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request=chain.request();
            Response response=chain.proceed(request);
            return response;
        }
    }
}
Fragmentone:
public class Fragmentone extends Fragment implements ContarctInterface.ProductViewInterface {

    public RecyclerView recycler;
    public CheckBox checkBox;
    public ContarctInterface.PresenterInterface presenterInterface;
    public List<ShowData> mlist=new ArrayList<>();
    public List<Show>qlist=new ArrayList<>();
    public MyAdapter adapter;
    public TextView tsum;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view=View.inflate(getContext(),R.layout.fragmentone,null);
       recycler=view.findViewById(R.id.recyclerview);
       checkBox=view.findViewById(R.id.quanxuan);
       tsum=view.findViewById(R.id.text_sum);

        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler.setLayoutManager(linearLayoutManager);
        presenterInterface=new MyPresenter(this);
        presenterInterface.toProduct();

        adapter=new MyAdapter(mlist,getActivity(),this);
        recycler.setAdapter(adapter);

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isChecked()) {
                    setCheckAll(true);
                    total();
                }else {
                    setCheckAll(false);

                }
                adapter.notifyDataSetChanged();
            }

        });

    }
    //全选
    private void setCheckAll(boolean b) {
        for (int i = 0; i <mlist.size() ; i++) {
            mlist.get(i).isChecked=b;
            for (int j = 0; j < mlist.get(i).getList().size(); j++) {
                mlist.get(i).getList().get(j).isChecked=b;
            }
        }
        total();
        adapter.notifyDataSetChanged();
    }

    //商家全选
    public void setShangjiaAll(int i, int i1, boolean checked) {
        mlist.get(i).isChecked=checked;
        if (i1 !=-1) {
            mlist.get(i).getList().get(i1).isChecked=false;
        }else {
            for (int j = 0; j <mlist.get(i).getList().size() ; j++) {
                mlist.get(i).getList().get(j).isChecked=checked;
            }
        }
        total();
        adapter.notifyDataSetChanged();
    }

    //总价
    public void total() {
        int asum=0;
        for (int i = 0; i <mlist.size() ; i++) {
            for (int j = 0; j <mlist.get(i).getList().size(); j++) {

                if (mlist.get(i).getList().get(j).isChecked) {
                    int pic= (int) Double.parseDouble(mlist.get(i).getList().get(j).getPrice());
                    int num= (int) Double.parseDouble(mlist.get(i).getList().get(j).getSelected());
                    int too=pic*num;
                    asum+=Double.parseDouble(mlist.get(i).getList().get(j).getPrice())+too;
                }
            }
        }
        tsum.setText(asum+"");

    }

    @Override
    public void ShowDisplay(Object object) {

        ShowBean showBean= (ShowBean) object;
        List<ShowData>list=showBean.getData();
        mlist.addAll(list);
        adapter.notifyDataSetChanged();
    }
}
MyAdapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.holder> {

    List<ShowData>list;
    Context context;
    Fragmentone fragmentone;

    public MyAdapter(List<ShowData> list, Context context, Fragmentone fragmentone) {
        this.list = list;
        this.context = context;
        this.fragmentone = fragmentone;
    }

    @NonNull
    @Override
    public holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.myadapter_item,null);
        return new holder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull final holder holder, final int i) {

        boolean isChecked=list.get(i).isChecked();
        String title=list.get(i).getSellerName();
        holder.acheckbox.setChecked(isChecked);
        holder.textview.setText(title);

        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        holder.recyclerView.setLayoutManager(linearLayoutManager);
        ProductAdapter productAdapter=new ProductAdapter(list.get(i).getList(),context);
        holder.recyclerView.setAdapter(productAdapter);

        holder.acheckbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragmentone.setShangjiaAll(i,-1,holder.acheckbox.isChecked());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class holder extends RecyclerView.ViewHolder{

        public CheckBox acheckbox;
        public TextView textview;
        public RecyclerView recyclerView;

        public holder(@NonNull View itemView) {
            super(itemView);
            acheckbox=itemView.findViewById(R.id.checkbox_id);
            textview=itemView.findViewById(R.id.shangjia);
            recyclerView=itemView.findViewById(R.id.recyclerview);
        }
    }
}
ProductAdapter:
public class ProductAdapter  extends RecyclerView.Adapter<ProductAdapter.holder1> {

    public List<Show> qlist;
    public Context context;
    public MainActivity mainActivity;

    public ProductAdapter(List<Show> qlist, Context context) {
        this.qlist = qlist;
        this.context = context;
        mainActivity= (MainActivity) context;
    }

    @NonNull
    @Override
    public holder1 onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.productadapter_item,null);
        return new holder1(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final holder1 holder1, final int i) {

        boolean isChecked=qlist.get(i).isChecked();
        String image=qlist.get(i).getImages();
        String title=qlist.get(i).getTitle();
        String price=qlist.get(i).getPrice();
        final String num=qlist.get(i).getNum();

        holder1.checkBox1.setChecked(isChecked);
        Glide.with(context).load(image).into(holder1.image1);
        holder1.text1.setText(title);
        holder1.text2.setText(price);
        holder1.text3.setText(num);

    }

    @Override
    public int getItemCount() {
        return qlist.size();
    }

    public class holder1 extends RecyclerView.ViewHolder{

        public CheckBox checkBox1;
        public ImageView image1;
        public TextView text1,text2,text3;
        public Button but_add,but_clear;

        public holder1(@NonNull View itemView) {
            super(itemView);

            checkBox1=itemView.findViewById(R.id.checkbox1);
            image1=itemView.findViewById(R.id.imageview);
            text1=itemView.findViewById(R.id.text1);
            text2=itemView.findViewById(R.id.text2);
            text3=itemView.findViewById(R.id.text3);
            but_add=itemView.findViewById(R.id.button1);
            but_clear=itemView.findViewById(R.id.button2);
        }
    }
}

动画:
第一步:
在values文件夹下建attrs.xml:
 <declare-styleable name="MyView">
        <attr name="myText" format="string"></attr>
        <attr name="myTextColor" format="color"></attr>
        <attr name="myTextSize" format="dimension"></attr>
    </declare-styleable>
第二步:
建一个类继承View:
Paint paint=new Paint();
    String text;
    int textColor;
    float textsize;
    int w,h,x=0;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(50);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);

        TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.MyView);
        text=typedArray.getString(R.styleable.MyView_myText);
        textColor=typedArray.getColor(R.styleable.MyView_myTextColor,Color.GREEN);
        textsize=typedArray.getDimension(R.styleable.MyView_myTextSize,30f);
        typedArray.recycle();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        w=getMeasuredWidth();
        h=getMeasuredHeight();
    }

    boolean isgong=true;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(100,100,100,paint);
        if (isgong) {
        }
    }
    ValueAnimator valueAnimator;


    public void startAnimation() {

        isgong=false;
        valueAnimator=ValueAnimator.ofInt(0,w);
        valueAnimator.setDuration(3000);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                animation.getAnimatedValue();
                postInvalidate();
            }
        });
        valueAnimator.start();
    }
}
第三步:
首页在最上面写上:  xmlns:qq="http://schemas.android.com/apk/res-auto"
不写的话实现不了效果:
写布局:
<com.example.gouwuche.coust.MyView
        android:id="@+id/myview"
        android:layout_width="300dp"
        android:layout_height="300dp"
        qq:myText="要写的内容"
        qq:myTextColor="#00ff00"
        qq:myTextSize="20sp"
        />
第四步:
写代码:
 MyView myView;
    Button button;

    ObjectAnimator objectAnimator1;
    ObjectAnimator objectAnimator2;
    ObjectAnimator objectAnimator3;
    ObjectAnimator objectAnimator4;
    ObjectAnimator objectAnimator5;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=View.inflate(getContext(),R.layout.fragmenttwo,null);
        myView=view.findViewById(R.id.myview);
        button=view.findViewById(R.id.shuxing);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                objectAnimator1=ObjectAnimator.ofFloat(myView,"translationX",0,300,0);
                objectAnimator2=ObjectAnimator.ofFloat(myView,"rotation",0,300,0);
                objectAnimator3=ObjectAnimator.ofFloat(myView,"scaleX",1,3,1);
                objectAnimator4=ObjectAnimator.ofFloat(myView,"scaleY",1,3,1);
                objectAnimator5=ObjectAnimator.ofFloat(myView,"alpha",1,0,1);

                AnimatorSet animatorSet=new AnimatorSet();
                animatorSet.play(objectAnimator1).with(objectAnimator2).with(objectAnimator3).with(objectAnimator4).with(objectAnimator5);

                animatorSet.setDuration(1500);
                animatorSet.start();
            }
        });

        return view;

    }

猜你喜欢

转载自blog.csdn.net/weixin_43584998/article/details/89060006