android 仿淘宝购物车list view实例
时间: 2023-09-18 17:10:22 浏览: 164
Android实现的仿淘宝购物车demo示例
好的,以下是一个简单的Android仿淘宝购物车的ListView实例:
1. 布局文件:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/tv_empty_cart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="购物车空空如也~"
android:textSize="20sp"
android:visibility="gone" />
</LinearLayout>
```
2. 条目布局文件item_cart.xml:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_cart_item"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="10dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_cart_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toEndOf="@id/iv_cart_item"
android:text="商品名称"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_cart_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_cart_name"
android:layout_marginTop="10dp"
android:layout_toEndOf="@id/iv_cart_item"
android:text="¥0.00"
android:textColor="#ff0000"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_cart_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/iv_cart_item"
android:layout_marginTop="10dp"
android:layout_toEndOf="@id/iv_cart_item"
android:text="数量:0"
android:textColor="#000000"
android:textSize="14sp" />
<Button
android:id="@+id/btn_cart_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:text="删除" />
</RelativeLayout>
```
3. ListView的Adapter:
```
public class CartAdapter extends BaseAdapter {
private Context mContext;
private List<CartItem> mCartItemList;
public CartAdapter(Context context, List<CartItem> cartItemList) {
mContext = context;
mCartItemList = cartItemList;
}
@Override
public int getCount() {
return mCartItemList.size();
}
@Override
public Object getItem(int position) {
return mCartItemList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_cart, parent, false);
viewHolder = new ViewHolder();
viewHolder.ivCartItem = convertView.findViewById(R.id.iv_cart_item);
viewHolder.tvCartName = convertView.findViewById(R.id.tv_cart_name);
viewHolder.tvCartPrice = convertView.findViewById(R.id.tv_cart_price);
viewHolder.tvCartCount = convertView.findViewById(R.id.tv_cart_count);
viewHolder.btnDelete = convertView.findViewById(R.id.btn_cart_delete);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final CartItem cartItem = mCartItemList.get(position);
viewHolder.ivCartItem.setImageResource(cartItem.getImageId());
viewHolder.tvCartName.setText(cartItem.getName());
viewHolder.tvCartPrice.setText(String.format("¥%.2f", cartItem.getPrice()));
viewHolder.tvCartCount.setText(String.format("数量:%d", cartItem.getCount()));
viewHolder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCartItemList.remove(position);
notifyDataSetChanged();
// 更新本地存储的购物车数据
updateCartData(mCartItemList);
}
});
return convertView;
}
private static class ViewHolder {
ImageView ivCartItem;
TextView tvCartName;
TextView tvCartPrice;
TextView tvCartCount;
Button btnDelete;
}
// 更新本地存储的购物车数据
private void updateCartData(List<CartItem> cartItemList) {
// TODO
}
}
```
4. 在Activity中初始化ListView和Adapter,并绑定数据:
```
public class CartActivity extends AppCompatActivity {
private ListView mListView;
private TextView mTvEmptyCart;
private List<CartItem> mCartItemList;
private CartAdapter mCartAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
mListView = findViewById(R.id.list_view);
mTvEmptyCart = findViewById(R.id.tv_empty_cart);
// 从本地存储中读取购物车数据
mCartItemList = readCartData();
if (mCartItemList.size() > 0) {
mCartAdapter = new CartAdapter(this, mCartItemList);
mListView.setAdapter(mCartAdapter);
} else {
mListView.setVisibility(View.GONE);
mTvEmptyCart.setVisibility(View.VISIBLE);
}
}
// 从本地存储中读取购物车数据
private List<CartItem> readCartData() {
// TODO
return new ArrayList<>();
}
}
```
希望这个实例能够帮到您!
阅读全文