商品界面的UI和java
时间: 2024-04-27 21:23:33 浏览: 65
以下是一个简单的商品列表展示界面的UI和Java代码:
1. 商品列表展示界面的UI
```xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" />
```
2. 商品列表展示界面的Java代码
```java
public class ProductListActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ProductListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
List<Product> productList = getProductList(); // 获取商品列表数据
mAdapter = new ProductListAdapter(productList);
mRecyclerView.setAdapter(mAdapter);
}
private List<Product> getProductList() {
// 从数据库中获取商品列表数据
// TODO: 实现从数据库获取商品列表数据的逻辑
return new ArrayList<>();
}
private class ProductListAdapter extends RecyclerView.Adapter<ProductViewHolder> {
private List<Product> mProductList;
public ProductListAdapter(List<Product> productList) {
mProductList = productList;
}
@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
Product product = mProductList.get(position);
holder.mNameTextView.setText(product.getName());
holder.mPriceTextView.setText(String.valueOf(product.getPrice()));
holder.mQuantityTextView.setText(String.valueOf(product.getQuantity()));
}
@Override
public int getItemCount() {
return mProductList.size();
}
}
private static class ProductViewHolder extends RecyclerView.ViewHolder {
private TextView mNameTextView;
private TextView mPriceTextView;
private TextView mQuantityTextView;
public ProductViewHolder(@NonNull View itemView) {
super(itemView);
mNameTextView = itemView.findViewById(R.id.tv_name);
mPriceTextView = itemView.findViewById(R.id.tv_price);
mQuantityTextView = itemView.findViewById(R.id.tv_quantity);
}
}
}
```
其中,`Product`是一个商品类,包含商品的名称、价格和数量等信息。`item_product.xml`是商品列表项的布局文件,包含商品名称、价格和数量等控件。在`ProductListAdapter`中,我们将商品列表项展示在了RecyclerView中,并根据需要实现了自定义的ViewHolder。
阅读全文