Android RecyclerView实现购物车功能

购物车实现图片:

准备工作
1,在项目中建立shoppingcart的Model
在这里插入图片描述

2,在与Activity上一级包,相同路径建立Adapter和model包(package)
如图:
在这里插入图片描述

3,Adapter中建立名字为 ShopAdapter 的类(class)
4,Model中建立名字为 Shop 的类
5,将:创建与下面图片一样图片加入drawable文件中
add.png +图片
down.png -图片
login.png 初始小图片(无作用)
login2.png 初始小图片(无作用)
//下面自己可随便找,名字一样即可
pic1.jpg
pic2.jpg
pic3.jpg
pic4.jpg
pic5.jpg
pic6.jpg
pic7.jpg
pic8.jpg
pic9.jpg
pic10.jpg
pic11.png
pic12.jpg
pic13.jpg
pic14.jpg

开始复制粘贴:
1,在activity_main.xml中粘贴:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EFE8E8"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#FF9800"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/clear"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="fill_vertical"
            android:layout_weight="1"
            android:gravity="center|start"
            android:text="   清空"
            android:textColor="#FDFAFA"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/upt"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="购物车"
            android:textColor="#F6F5F5"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/delect"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center|end"
            android:text="删除   "
            android:textColor="#F6F5F5"
            android:textSize="18sp" />
    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="640dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout1"
        app:layout_constraintVertical_bias="0.0">

    </androidx.recyclerview.widget.RecyclerView>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent">

        <CheckBox
            android:id="@+id/checkBoxall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选"
            android:textColor="#FF9800"
            app:layout_constraintBaseline_toBaselineOf="@+id/textView9"
            tools:layout_editor_absoluteX="1dp" />

        <TextView
            android:id="@+id/textView9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="60dp"
            android:text="结算的价钱"
            android:textColor="#FF9800"
            app:layout_constraintStart_toEndOf="@+id/checkBoxall"
            tools:layout_editor_absoluteY="15dp" />

        <TextView
            android:id="@+id/priceall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:text="0.0"
            android:textColor="#FF9800"
            app:layout_constraintBaseline_toBaselineOf="@+id/textView9"
            app:layout_constraintStart_toEndOf="@+id/textView9" />

        <Button
            android:id="@+id/buttonall"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginStart="92dp"
            android:text="结算"
            app:layout_constraintBaseline_toBaselineOf="@+id/priceall"
            app:layout_constraintStart_toEndOf="@+id/priceall" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

2,在shop.xml中粘贴:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="#FAF6F6"
    android:backgroundTint="#FFFEFE"
    android:visibility="visible"
    tools:visibility="visible">

    <CheckBox
        android:id="@+id/radio"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginStart="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/photo"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/photo"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="68dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/login2" />

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="11dp"
        android:layout_marginTop="7dp"
        android:text="商品名字"
        app:layout_constraintStart_toEndOf="@+id/photo"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/test2"
        android:layout_width="160dp"
        android:layout_height="30dp"
        android:layout_marginTop="4dp"
        android:text="对商品描叙"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="@+id/text1"
        app:layout_constraintTop_toBottomOf="@+id/text1" />

    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:text="100"
        android:textColor="#FF9800"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/test2" />

    <ImageView
        android:id="@+id/ptoto_down"
        android:layout_width="30dp"
        android:layout_height="15dp"
        android:layout_marginStart="3dp"
        android:layout_marginBottom="40dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/test2"
        app:layout_constraintTop_toTopOf="@+id/num"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@drawable/down" />

    <TextView
        android:id="@+id/num"
        android:layout_width="38dp"
        android:layout_height="16dp"
        android:layout_marginBottom="40dp"
        android:autoText="false"
        android:gravity="center"
        android:text="0"
        android:textAllCaps="false"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/ptoto_down" />

    <ImageView
        android:id="@+id/photo_add"
        android:layout_width="30dp"
        android:layout_height="15dp"
        app:layout_constraintBottom_toBottomOf="@+id/num"
        app:layout_constraintStart_toEndOf="@+id/num"
        app:srcCompat="@drawable/add" />

</androidx.constraintlayout.widget.ConstraintLayout>

3,在ShopAdapter类中粘贴:

package com.llw.Adapter;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ComponentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.llw.model.Shop;
import com.llw.shoppingcart.MainActivity;
import com.llw.shoppingcart.R;

import java.util.List;

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> {

    private List<Shop> list;
    public ShopAdapter(List<Shop>list){
        this.list=list;
    }
    public boolean[] flag = new boolean[100];
    public int sum=0;
    public int sum1=0;
    TextView priceAll;

//计算sum和
    public int getSum1(){
        sum1=0;
        for(int i=0;i<list.size();i++){
            if(flag[i]==true){
                sum1+=list.get(i).getNum()*list.get(i).getPrice();
            }
        }
        return sum1;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{

        ImageView photo;
        ImageView photo_add;
        ImageView photo_down;
        TextView text1;
        TextView text2;
        TextView price;
        TextView num;
        CheckBox radio;
        public ViewHolder(@NonNull View view) {
            super(view);
            photo=(ImageView) view.findViewById(R.id.photo);
            photo_add=(ImageView) view.findViewById(R.id.photo_add);
            photo_down=(ImageView) view.findViewById(R.id.ptoto_down);
            text1=(TextView) view.findViewById(R.id.text1);
            text2=(TextView) view.findViewById(R.id.test2);
            price=(TextView) view.findViewById(R.id.price);
            num=(TextView) view.findViewById(R.id.num);
            radio=(CheckBox) view.findViewById(R.id.radio);

        }
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.shop,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        View view2=LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main,parent,false);
        ViewHolder viewHolder2=new ViewHolder(view2);//获取MainActivity的布局操作

        //监听事件
        //监听点击的图片事件
        viewHolder.photo.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                setPosition( viewHolder.getAdapterPosition() );///
                int pos=viewHolder.getAdapterPosition();
                Shop shop=list.get(pos);
                Uri webpage;
                if(shop.getPhotoId()==R.drawable.pic1){
                    webpage = Uri.parse("https://item.jd.com/100018761109.html");
                }
                else if(shop.getPhotoId()==R.drawable.pic2){
                    webpage = Uri.parse("https://item.jd.com/10023842249226.html");
                }
                else if(shop.getPhotoId()==R.drawable.pic3){
                    webpage = Uri.parse("https://item.jd.com/100018633249.html");
                }
                else if(shop.getPhotoId()==R.drawable.pic4){
                    webpage = Uri.parse("https://item.jd.com/10022335886010.html");
                }
                else if(shop.getPhotoId()==R.drawable.pic5){
                    webpage = Uri.parse("https://item.jd.com/8534207.html");
                }
                else if(shop.getPhotoId()==R.drawable.pic6){
                    webpage = Uri.parse("https://item.jd.com/100014205656.html");
                }
                else if(shop.getPhotoId()==R.drawable.pic7){
                    webpage = Uri.parse("https://item.jd.com/100027703540.html");
                }
                else if(shop.getPhotoId()==R.drawable.pic8){
                    webpage = Uri.parse("https://item.jd.com/10040851500488.html");
                }
                else if(shop.getPhotoId()==R.drawable.pic9){
                    webpage = Uri.parse("https://item.jd.com/100031564120.html");
                }
                else if(shop.getPhotoId()==R.drawable.pic10){
                    webpage = Uri.parse("https://item.jd.com/7170704.html");
                }
                else if(shop.getPhotoId()==R.drawable.pic11){
                    webpage = Uri.parse("https://item.jd.com/10036491869801.html");
                }
                else if(shop.getPhotoId()==R.drawable.pic12){
                    webpage = Uri.parse("https://item.jd.com/10032573727362.html");
                }
                else if(shop.getPhotoId()==R.drawable.pic13){
                    webpage = Uri.parse("https://item.jd.com/11820243915.html#none");
                }
                else if(shop.getPhotoId()==R.drawable.pic14){
                    webpage = Uri.parse("https://item.jd.com/1198996.html");
                }
                else {
                    webpage=Uri.parse("");
                }
                Intent intent = new Intent();
                intent.setAction( Intent.ACTION_VIEW );
                intent.setData( webpage );
                parent.getContext().startActivity(intent);
            }
        });
        //add+1
        viewHolder.photo_add.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                int pos=viewHolder.getAdapterPosition();
                Shop shop= list.get(pos);
                int x=shop.getNum();
                x++;
                shop.setNum(x);
                viewHolder.num.setText(x+"");
                if(flag[pos]==true){
                    priceAll=view2.findViewById(R.id.priceall);
                    priceAll.setText("一共:"+getSum1()+".0元");
                    Toast.makeText(view.getContext(),""+priceAll.getText(),Toast.LENGTH_SHORT).show();

                }
            }
        });

        //down-1
        viewHolder.photo_down.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                int pos=viewHolder.getAdapterPosition();
                Shop shop= list.get(pos);
                int x=shop.getNum();
                x--;
                if(x<0) x=0;
                shop.setNum(x);
                viewHolder.num.setText(x+"");
                if(flag[pos]==true){
                    priceAll=view2.findViewById(R.id.priceall);
                    priceAll.setText("一共:"+getSum1()+".0元");
                    Toast.makeText(view.getContext(),""+priceAll.getText(),Toast.LENGTH_SHORT).show();
                }
            }
        });
        //设置图片监听事件



        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Shop shop=list.get(position);
        holder.text1.setText(shop.getText1());
        holder.text2.setText(shop.getText2());
        holder.price.setText(shop.getPrice()+".0");
        holder.num.setText(shop.getNum()+"");
        holder.photo.setImageResource(shop.getPhotoId());
 //       holder.photo_add.setImageResource(shop.getPhoto_addId());
//        holder.photo_down.setImageResource(shop.getPhoto_downId());
//        holder.radio.setVisibility(View.VISIBLE);
        //checkbutton的绑定
        holder.radio.setText((position+1)+"");//设置多选按钮的位置值,删除行后会更新
        holder.radio.setOnCheckedChangeListener(null);
        holder.radio.setChecked(flag[position]);

        holder.radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                flag[position]=b;
            }
        });


    }

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

    private int position;
    public int getPosition() {
        return position;
    }
    public void setPosition(int position) {
        this.position = position;
    }

}

4,在Shop类中粘贴:

package com.llw.model;

public class Shop {
    private String text1;//商品名称
    private String text2;//商品描叙
    private int price;//商品价格
    private int num;//商品数量
    private int photoId;
    private int photo_addId;
    private int photo_downId;
    private boolean isChecked;

    public Shop(String text1,String text2,int photoId,int num,int price){
        this.text1 = text1;this.text2 = text2;this.photoId = photoId;this.num = num;this.price = price;
//        ,int price,int num,int photoId,int photo_addId,int photo_downId,boolean isChecked
//        this.price = price;this.num = num;
//        this.photoId = photoId;this.photo_downId = photo_downId;this.isChecked =isChecked;
//        this.photo_addId = photo_addId;
    }
    public void setText1(String text1) {
        this.text1 = text1;
    }

    public void setText2(String text2) {
        this.text2 = text2;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public void setPhotoId(int photoId) {
        this.photoId = photoId;
    }

    public void setPhoto_addId(int photo_addId) {
        this.photo_addId = photo_addId;
    }

    public void setPhoto_downId(int photo_downId) {
        this.photo_downId = photo_downId;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

    public String getText1() {
        return text1;
    }

    public String getText2() {
        return text2;
    }

    public int getPrice() {
        return price;
    }

    public int getNum() {
        return num;
    }

    public int getPhotoId() {
        return photoId;
    }

    public int getPhoto_addId() {
        return photo_addId;
    }

    public int getPhoto_downId() {
        return photo_downId;
    }

    public boolean isChecked() {
        return isChecked;
    }
}

5,在MainActivity中粘贴:

package com.llw.shoppingcart;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.llw.Adapter.ShopAdapter;
import com.llw.model.Shop;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView1;
    private CheckBox selectAll;
    private boolean[]f;
    private TextView clr;
    private TextView del;
    private Button buttonAll;
    private TextView priceAll;

    private List<Shop> list=new ArrayList<>();
    private RecyclerView recyclerView;
    private ShopAdapter adapter;
    public void initShop(){
            Shop shop=new Shop("5G手机 小米 红米","Redmi K50 电竞版 全新",R.drawable.pic1,1,3849);
            list.add(shop);
            Shop shop1=new Shop("全友家居餐桌椅","家用小户型吃饭桌子",R.drawable.pic2,1,4071);
            list.add(shop1);
            Shop shop2=new Shop("15.6英寸轻薄游戏本","华硕天选air 2022 12代",R.drawable.pic3,1,9999);
            list.add(shop2);
            Shop shop3=new Shop("圣闲女童爱莎公主裙","春夏款可拆披纱棉质舒适",R.drawable.pic4,1,108);
            list.add(shop3);
            Shop shop4=new Shop("高钙儿童纯牛奶","伊利 QQ星儿童纯牛奶",R.drawable.pic5,1,56);
            list.add(shop4);
            Shop shop5=new Shop("男女同款厚底熊猫鞋","老爹鞋休闲运动鞋情侣鞋",R.drawable.pic6,1,359);
            list.add(shop5);
            Shop shop6=new Shop("一次性医用外科口罩","无菌三层外科灭菌口罩",R.drawable.pic7,1,39);
            list.add(shop6);
            Shop shop7=new Shop("苹果13promax手机壳","iphone13保护套透明超薄",R.drawable.pic8,1,34);
            list.add(shop7);
            Shop shop8=new Shop("佳洁士3D炫白牙膏","美白牙膏去黄去牙渍",R.drawable.pic9,1,27);
            list.add(shop8);
            Shop shop9=new Shop("大连美早樱桃","车厘子巨无霸JJJ级",R.drawable.pic10,1,160);
            list.add(shop9);
            Shop shop10=new Shop("华为nova9","9号色 8+128G全网通",R.drawable.pic11,1,2519);
            list.add(shop10);
            Shop shop11=new Shop("蓝牙耳机","oppo 白色尊享升级版",R.drawable.pic12,1,89);
            list.add(shop11);
            Shop shop12=new Shop(" T9民谣吉他","新手入门练习琴guitar",R.drawable.pic13,1,175);
            list.add(shop12);
            Shop shop13=new Shop("卡西欧(CASIO)手表","商务休闲男表石英表",R.drawable.pic14,1,505);
            list.add(shop13);

    }

    public void initRecyclerView2() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        // 定义一个线性布局管理器(默认是垂直排列)
        LinearLayoutManager layoutManager = new LinearLayoutManager(this); //默认垂直排列
        recyclerView.setLayoutManager(layoutManager);
        adapter = new ShopAdapter(list);
        recyclerView.setAdapter(adapter);

        //添加默认的分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }


    //全选和全不选功能
    public void selectAll(){
        selectAll=(CheckBox)findViewById(R.id.checkBoxall);
        recyclerView=(RecyclerView) findViewById(R.id.recyclerView);
        f=new boolean[100];
        selectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b==true){
                    for(int i=0;i<100;i++){
                        adapter.flag[i]=true;
                        adapter.notifyDataSetChanged();
                    }
                }
                else{
                    for(int i=0;i<100;i++){
                        adapter.flag[i]=false;
                        adapter.notifyDataSetChanged();
                    }
                }
                //更新数据
                // adapter = new ShopAdapter(list);
                // adapter.notifyDataSetChanged();
            }
        });
    }
    //移除功能
    public void removeItem(int position){
        list.remove(position);
        adapter.notifyItemRemoved(position);
        adapter.notifyItemRangeChanged(position,list.size());//必须用这个不然会紊乱
    }
    //删除功能
    public  void delete(){
        del=(TextView)findViewById(R.id.delect);
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              //  Toast.makeText(MainActivity.this,"删除功能",Toast.LENGTH_SHORT).show();
                for(int i=0;i<list.size();i++){
                    if(adapter.flag[i]==true){
                        removeItem(i);
                        //删除list数组后还要更新flag数组 i没有了i+1的变成了i所有flag[i]=flag[1+1]
                        for(int j=i;j<list.size()-1;j++) adapter.flag[j]=adapter.flag[j+1];
                        //删除后i,i--才行,列如第0行删除后仍然从第0行开始
                        i--;
                    }
                }
            }
        });
    }
    //清空功能
    public void clear(){
        clr=(TextView)findViewById(R.id.clear);
        clr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            //    Toast.makeText(MainActivity.this,"清空功能",Toast.LENGTH_SHORT).show();
                list.clear();
                adapter.notifyDataSetChanged();
            }
        });
    }
    //计算合计总价钱
    public void allPrice(){
        buttonAll=(Button)findViewById(R.id.buttonall);
        priceAll=(TextView)findViewById(R.id.priceall);
        buttonAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                priceAll.setText(adapter.getSum1()+".0");
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initShop();
        initRecyclerView2();
        selectAll();
        delete();
        clear();
        allPrice();

        //ActionBar
        ActionBar actionBar=getSupportActionBar();
        actionBar.hide();
        //清屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

}

注:可能package不一样自己改动一下即可
以后更新,讲解每个布局和每部分代码功能

更新后的文章: 更新后的链接

  • 7
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
<?xml version="1.0" encoding="UTF-8"?> -<LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView android:layout_height="153dp" android:layout_width="198dp" android:id="@+id/imageView1" android:src="@drawable/xitongji"> </ImageView> -<LinearLayout android:weightSum="1" android:layout_height="32dp" android:layout_width="234dp" android:orientation="horizontal" android:layout_weight="0.23"> <ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/jian" android:src="@drawable/jianjian"> </ImageButton> -<EditText android:layout_height="wrap_content" android:layout_width="39dp" android:id="@+id/jishu" android:layout_weight="0.34"> <requestFocus/> </EditText> <ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/jia" android:src="@drawable/jiajia"> </ImageButton> </LinearLayout> -<LinearLayout android:weightSum="1" android:layout_height="30dp" android:layout_width="83dp" android:orientation="horizontal"> <TextView android:layout_height="match_parent" android:layout_width="wrap_content" android:id="@+id/jiagequding" android:text="单价:"/> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/jiage" android:text="45"/> </LinearLayout> -<LinearLayout android:weightSum="1" android:layout_height="wrap_content" android:layout_width="210dp" android:orientation="horizontal" android:layout_weight="0.09"> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/zongji" android:text="总计:"> </TextView> <EditText android:layout_height="wrap_content" android:layout_width="56dp" android:id="@+id/editTextzongji"> </EditText> </LinearLayout> -<LinearLayout android:weightSum="1" android:layout_height="wrap_content" android:layout_width="190dp" android:orientation="horizontal"> <Button android:layout_height="match_parent" android:layout_width="wrap_content" android:id="@+id/gouwuquding" android:text="确定购买"> </Button> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/gouwuquxiao" android:text="取消购买"> </Button> </LinearLayout> </LinearLayout>
### 回答1: Android RecyclerView购物是一种常见的应用场景,可以通过RecyclerView实现购物列表的展示和管理。在购物中,可以添加、删除、修改商品数量等操作,同时还可以计算商品总价和结算等功能。使用RecyclerView可以方便地实现购物列表的动态更新和优化,提高应用的性能和用户体验。 ### 回答2: Android Recyclerview购物是一个很实用的功能,它具有展示商品列表、添加商品至购物、结算等功能RecyclerviewAndroid官方推出的新一代列表控件,拥有高效的回收重复利用View的能力,能够更好地优化程序性能。 在购物功能中,我们可以通过Recyclerview来展示商品列表,每个商品可以使用一个Recyclerview的item来表示。在Recyclerview的item中可以包含商品的名称、价格、图片等信息,通过合适的布局设计,可以使商品列表看起来更加美观和易于使用。 而对于添加商品至购物功能,可以通过Recyclerview的item中添加一个按钮或者其他交互元素,当用户点击按钮时,就可以将商品添加至购物中。这里需要注意的是,添加至购物中的商品信息可以通过本地数据库或者网络存储来存储,保证购物中的商品信息是持久化的。 最后,当用户需要结算时,我们可以通过Recyclerview中的item来展示购物中的商品信息,并计算出商品总价格。用户可以在购物中选择需要购买的商品,然后点击结算按钮,进入到支付页面来完成购买。 总之,Recyclerview购物是一项实用的功能,它包含商品列表展示、添加商品至购物和结算等功能,可以通过合理的布局和交互设计来优化用户体验,同时通过本地或者网络存储来保证购物中的商品信息是持久化的。 ### 回答3: 随着移动互联网和智能手机的普及,移动购物已经成为了一种普遍的购物方式。而对于一个电商应用来说,购物是不可或缺的功能之一。Android Recyclerview是一种非常适合实现购物功能的控件。下面我们来了解一下如何使用Recyclerview实现购物功能。 1.设计数据模型 购物主要是一个列表,需要设计对应的数据模型。一般的购物数据模型包含商品ID、商品名称、数量、单价、图片等属性。我们需要在开发中定义一个CartItem类来表示一个购物项。 2.实现Recyclerview购物的界面中,我们需要使用RecyclerView控件来展示购物中所有的商品项。我们需要在xml文件中定义RecyclerView控件以及对应的适配器。 3.定义适配器 适配器是连接Recyclerview和数据模型之间的桥梁。我们需要在适配器中定义好ViewHolder,以便对每个列表项进行数据绑定。还需要定义几个方法,如获取Item的数量、获取指定位置上的Item内容以及创建ViewHolder对象等。 4.实现交互功能 购物功能一般包括增加、删除、修改数量等操作。我们需要在适配器中实现这些操作,并且实时更新数据模型和Recyclerview控件。 5.实现结算功能 购物中一般都有结算按钮,用户可以在结算之前对商品进行修改或删除。我们需要在结算功能实现购物总价的计算,并且将商品信息传递给支付页面。 总之,使用Recyclerview实现购物功能可以帮助我们在Android应用中非常快速地完成相关功能,而且具有良好的扩展性。同时我们还可以结合其他的功能进行使用,如异步加载图片、分页加载等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值