实现功能
操作提示:选中所心仪的商品之后点击右下角支付图标显示总价格
1:增加商品个数,减少商品个数
2:统计所选中商品的价格
3:具有编辑功能(删除)
4:当商品被删除完之后跳转到空界面
效果图如下:
第一步:建立实体类Commodity
代码如下
/*此类用于对商品的
店铺名字
商品名字
价格
是否被选中
数量
进行定义
*/
public class Commodity {
private String StoreName;
private String CommodityName;
private double Price;
private int Number;
private boolean Is_Select;
public Commodity(){
}
public Commodity(String StoreName , String CommodityName , double Price , int Number , boolean Is_Select){
this.StoreName = StoreName;
this.CommodityName = CommodityName;
this.Price = Price;
this.Number = Number;
this.Is_Select = Is_Select;
}
public String getStoreName() {
return StoreName;
}
public void setStoreName(String storeName) {
StoreName = storeName;
}
public String getCommodityName() {
return CommodityName;
}
public void setCommodityName(String commodityName) {
CommodityName = commodityName;
}
public double getPrice() {
return Price;
}
public void setPrice(double price) {
Price = price;
}
public int getNumber() {
return Number;
}
public void setNumber(int number) {
Number = number;
}
public boolean isIs_Select() {
return Is_Select;
}
public void setIs_Select(boolean is_Select) {
Is_Select = is_Select;
}
}
第二步:建立MyRecyclerView活动继承RecyclerView
代码如下:
public class MyRecyclerView extends RecyclerView.Adapter<MyRecyclerView.ViewHolder> {
private List<Commodity> ListCommodity;
public static double Money = 0; //用于返回总金额
private boolean DisplayDeleteControls = false; //用于表示是否显示删除按钮
public static int DelNum = 0;
public MyRecyclerView(List<Commodity> ListCommodity){
this.ListCommodity = ListCommodity;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
//final Commodity shop = ListCommodity.get(position);
holder.StoreName.setText(ListCommodity.get(position).getStoreName());
holder.CommodityName.setText(ListCommodity.get(position).getCommodityName());
holder.Price.setText(ListCommodity.get(position).getPrice() + "");
holder.Number.setText(ListCommodity.get(position).getNumber() + "");
holder.CommodityCheckBox.setChecked(ListCommodity.get(position).isIs_Select());
//显示删除按钮
if (DisplayDeleteControls == true)
{
holder.Del.setVisibility(View.VISIBLE);
}
else
{
holder.Del.setVisibility(View.GONE);
}
//增加商品数量
holder.CommodityNumAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListCommodity.get(position).setNumber(ListCommodity.get(position).getNumber() + 1);
notifyDataSetChanged();
}
});
//减少商品数量
holder.CommodityNumSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ListCommodity.get(position).getNumber() > 1){
ListCommodity.get(position).setNumber(ListCommodity.get(position).getNumber() - 1);
notifyDataSetChanged();
}
}
});
//判断是否选中
holder.CommodityCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox Select = (CheckBox) v;
if (Select.isChecked())
{
ListCommodity.get(position).setIs_Select(true);
}
else
{
ListCommodity.get(position).setIs_Select(false);
}
notifyDataSetChanged();
}
});
//删除对应商品
holder.Del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListCommodity.remove(position);
notifyDataSetChanged();
if (ListCommodity.size() == 0)
{
DelNum = 1;
}
}
});
}
//把隐藏的删除按钮显示出来
public void ShowDelButton(boolean DisplayDeleteControls)
{
if (DisplayDeleteControls == true)
{
this.DisplayDeleteControls = true;
}
else
{
this.DisplayDeleteControls = false;
}
this.notifyDataSetChanged();
}
//是否全选,因为全选按钮不在适配器布局中,所以得用主函数调用
public void SelectAll (boolean IsSelect){
for (int j = 0; j < ListCommodity.size(); j++) {
if (IsSelect == true)
{
ListCommodity.get(j).setIs_Select(true);
}
else
{
ListCommodity.get(j).setIs_Select(false);
}
}
this.notifyDataSetChanged();
}
//获取选中商品的数量和金额,返回总金额
public double TotalMoney()
{
double Money = 0;
for (int i = 0; i < ListCommodity.size(); i++) {
if (ListCommodity.get(i).isIs_Select() == true)
{
Money += (ListCommodity.get(i).getNumber()) * (ListCommodity.get(i).getPrice());
}
}
return Money;
}
@Override
public int getItemCount() {
return ListCommodity.size();
}
static class ViewHolder extends RecyclerView.ViewHolder{
TextView StoreName,CommodityName,Price,Number;
ImageButton CommodityNumSub,CommodityNumAdd;
CheckBox CommodityCheckBox;
ImageView StoreImage,CommodityImage,MoneyImage;
Button Del;
public ViewHolder(@NonNull View itemView) {
super(itemView);
StoreName = (TextView) itemView.findViewById(R.id.StoreName);
CommodityName = (TextView) itemView.findViewById(R.id.CommodityName);
Price = (TextView) itemView.findViewById(R.id.CommodityMoney);
Number = (TextView) itemView.findViewById(R.id.CommodityNumber);
StoreImage = (ImageView) itemView.findViewById(R.id.StoreImage);
CommodityImage = (ImageView) itemView.findViewById(R.id.CommodityImage);
MoneyImage = (ImageView) itemView.findViewById(R.id.CommodityMoneyImage);
CommodityNumSub = (ImageButton) itemView.findViewById(R.id.CommodityNumberSub);
CommodityNumAdd = (ImageButton) itemView.findViewById(R.id.CommodityNumberAdd);
CommodityCheckBox = (CheckBox) itemView.findViewById(R.id.CommodityCheckBox);
Del = (Button) itemView.findViewById(R.id.Del);
}
}
}
MyRecyclerView布局文件代码
效果图如下:
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="@+id/StoreImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:src="@drawable/store"
android:scaleType="centerInside"/>
<TextView
android:id="@+id/StoreName"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="0dp"
android:text="店铺"
android:textColor="#000"
android:textSize="30sp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="60dp"
android:background="#000"/>
<CheckBox
android:id="@+id/CommodityCheckBox"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="130dp"
android:layout_marginLeft="10dp"/>
<ImageView
android:id="@+id/CommodityImage"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/bg"
android:scaleType="centerInside"
android:layout_marginTop="70dp"
android:layout_marginLeft="50dp"/>
<TextView
android:id="@+id/CommodityName"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="商品名字"
android:layout_marginTop="80dp"
android:layout_marginLeft="210dp"
android:textColor="#000"
android:textSize="20sp"/>
<ImageView
android:id="@+id/CommodityMoneyImage"
android:layout_width="30dp"
android:layout_height="20dp"
android:src="@drawable/money"
android:scaleType="centerInside"
android:layout_marginLeft="200dp"
android:layout_marginTop="170dp"/>
<TextView
android:id="@+id/CommodityMoney"
android:layout_height="50dp"
android:layout_width="70dp"
android:text="0.0"
android:layout_marginLeft="235dp"
android:layout_marginTop="167dp"
android:textSize="20sp"
android:textColor="#ff0000"
android:singleLine="true"/>
<ImageButton
android:id="@+id/CommodityNumberSub"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/shoppingcartsub"
android:scaleType="centerInside"
android:background="#ffffff"
android:layout_marginTop="170dp"
android:layout_marginLeft="283dp"/>
<TextView
android:id="@+id/CommodityNumber"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="315dp"
android:layout_marginTop="175dp"
android:textSize="15sp"
android:textColor="#ff0000"
android:text="1"/>
<ImageButton
android:id="@+id/CommodityNumberAdd"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/shoppingcartadd"
android:scaleType="centerInside"
android:background="#ffffff"
android:layout_marginTop="170dp"
android:layout_marginLeft="330dp"/>
<Button
android:id="@+id/Del"
android:layout_width="40dp"
android:layout_height="100dp"
android:text="删除"
android:background="#ff0000"
android:textSize="20sp"
android:layout_alignParentRight="true"
android:layout_marginTop="80dp"
android:visibility="gone"/>
第三步:建立MainActicity代码如下: public class MainActivity extends AppCompatActivity{
private RecyclerView Recycler;
private TextView AllSelectEventWords, TotalMoneyEventWords, Title, TotalMoney;
private ImageView MoneyImage;
private ImageButton PayImageButton;
private CheckBox AllSelect;
private Button Edit;
private MyRecyclerView adapter;
private List<Commodity> ListCommodity = new ArrayList<>();
//自定义信息
//店铺名字,商品名字,价格,数量,是否被选择
private String[][] CommodityData = {
{"儿童店", "奶嘴", "39.9", "2", "false"},
{"青年店", "篮球鞋", "999", "1", "false"},
{"中年店", "假发", "1999", "1", "false"},
{"老年店", "假牙", "59.9", "1", "false"}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Recycler = (RecyclerView) findViewById(R.id.recyclerview);
MoneyImage = (ImageView) findViewById(R.id.MoneyImage);
PayImageButton = (ImageButton) findViewById(R.id.PayImage);
AllSelect = (CheckBox) findViewById(R.id.AllSelectImage);
Title = (TextView) findViewById(R.id.Title);
AllSelectEventWords = (TextView) findViewById(R.id.AllSelectText);
TotalMoneyEventWords = (TextView) findViewById(R.id.TotalMoneyText);
TotalMoney = (TextView) findViewById(R.id.TotalMoney);
Edit = (Button) findViewById(R.id.Edit);
//初始化数据
InitData();
LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
Recycler.setLayoutManager(manager);
adapter = new MyRecyclerView(ListCommodity);
Recycler.setAdapter(adapter);
double Money = MyRecyclerView.Money;
TotalMoney.setText(Money+"");
//全选操作
AllSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapter != null) {
CheckBox Select = (CheckBox) v;
if (Select.isChecked() == true)
{
adapter.SelectAll(true);
}
else
{
adapter.SelectAll(false);
}
}
}
});
//获取所选中商品的总金额
PayImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double Money = adapter.TotalMoney();
TotalMoney.setText(Money+"");
if (Money > 10)
{
Toast.makeText(MainActivity.this,"支付成功!",Toast.LENGTH_SHORT).show();
}
}
});
//点击编辑按钮
Edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapter != null)
{
if (Edit.getText().equals("编辑"))
{
Edit.setText("完成");
adapter.ShowDelButton(true);
if (adapter.DelNum == 1)
{
Intent intent = new Intent(MainActivity.this,EmptyInterface.class);
startActivity(intent);
}
}
else
{
Edit.setText("编辑");
adapter.ShowDelButton(false);
}
}
}
});
}
public void InitData () {
Commodity commodity = null;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 1; j++) {
String StoreName = CommodityData[i][j];
String CommodityName = CommodityData[i][j + 1];
double Money = 0;
int Num = 0;
boolean is_Select = false;
try {
Money = Double.valueOf(CommodityData[i][j + 2]);
Num = Integer.parseInt(CommodityData[i][j + 3]);
is_Select = Boolean.parseBoolean(CommodityData[i][j + 4]);
} catch (NumberFormatException e) {
e.printStackTrace();
}
commodity = new Commodity(StoreName, CommodityName, Money, Num, is_Select);
//adapter.notifyItemChanged(ListCommodity.size()-1);
Recycler.scrollToPosition(ListCommodity.size() - 1);
}
ListCommodity.add(commodity);
}
}
}
<font size="5" color = #ff0000>效果图如下:</font>
MainActivity布局文件代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_height=“wrap_content”
android:layout_width=“match_parent”
android:layout_marginTop=“70dp”/>
第四步:建立一个空活动,不执行任何东西,用于商品被删除完之后跳转到此界面
代码太过于简单,在此不展示,直接上效果图
下面展示一个细节效果图
点击编辑按钮,然后出现删除按钮,删除按钮是被隐藏的