写一篇android仿美团外卖加上代码
时间: 2023-08-07 13:02:09 浏览: 95
很抱歉,由于android仿美团外卖的实现涉及到多个模块和功能,无法在此篇回答中完全覆盖。以下是一个简单的示例代码,用于实现一个美团外卖的商家列表页面。
MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private BusinessListAdapter adapter;
private List<Business> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化RecyclerView
recyclerView = findViewById(R.id.business_list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
// 初始化商家列表数据
dataList = initData();
// 初始化商家列表适配器
adapter = new BusinessListAdapter(dataList);
recyclerView.setAdapter(adapter);
}
private List<Business> initData() {
List<Business> dataList = new ArrayList<>();
// 添加商家数据
dataList.add(new Business("1", "商家1", "地址1", "描述1", R.drawable.business_logo_1));
dataList.add(new Business("2", "商家2", "地址2", "描述2", R.drawable.business_logo_2));
dataList.add(new Business("3", "商家3", "地址3", "描述3", R.drawable.business_logo_3));
return dataList;
}
}
```
BusinessListAdapter.java
```java
public class BusinessListAdapter extends RecyclerView.Adapter<BusinessListAdapter.ViewHolder> {
private List<Business> dataList;
public BusinessListAdapter(List<Business> dataList) {
this.dataList = dataList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_business, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Business business = dataList.get(position);
holder.businessName.setText(business.getName());
holder.businessAddress.setText(business.getAddress());
holder.businessDesc.setText(business.getDesc());
holder.businessLogo.setImageResource(business.getLogo());
}
@Override
public int getItemCount() {
return dataList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView businessName;
public TextView businessAddress;
public TextView businessDesc;
public ImageView businessLogo;
public ViewHolder(View itemView) {
super(itemView);
businessName = itemView.findViewById(R.id.business_name);
businessAddress = itemView.findViewById(R.id.business_address);
businessDesc = itemView.findViewById(R.id.business_desc);
businessLogo = itemView.findViewById(R.id.business_logo);
}
}
}
```
Business.java
```java
public class Business {
private String id;
private String name;
private String address;
private String desc;
private int logo;
public Business(String id, String name, String address, String desc, int logo) {
this.id = id;
this.name = name;
this.address = address;
this.desc = desc;
this.logo = logo;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getLogo() {
return logo;
}
public void setLogo(int logo) {
this.logo = logo;
}
}
```
activity_main.xml
```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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/business_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
```
item_business.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:elevation="8dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/business_logo"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding="8dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/business_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/business_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
<TextView
android:id="@+id/business_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
```
以上示例代码实现了一个简单的商家列表页面,包括商家名称、地址、描述和商家logo。您可以根据自己的需求进行修改和扩展。
阅读全文