安卓(android)订餐菜单【Android移动开发基础案例教程(第2版)黑马程序员】
一、实验目的(如果代码有错漏,可查看源码)
1.掌握Activity生命周的每个方法。
2.掌握Activity的创建、配置、启动和关闭。
3.掌握Intent和IntentFilter的使用。
4.掌握Activity之间的跳转方式、任务栈和四种启动模式。
5.掌握在Activity中添加和使用Fragment。
二、实验条件
1.熟悉Java编程语言;了解Android应用程序的基本结构;掌握XML布局文件的编写;了解Activity和Fragment的基本概念。
2.其他准备:准备好菜单项的图片资源;设计好菜单的数据结构和内容。
三、实验内容
1.搭建菜单界面布局:搭建左侧菜单栏界面布局;搭建右侧菜单列表界面布局;搭建菜单列表界面的条目布局。
2.实现菜单界面功能:封装菜品信息的实体类;加载左侧菜单栏界面布局;编写菜单列表的适配器;加载右侧菜单栏界面布局;实现显示菜单的效果。
3.运行程序:运行程序,显示推荐选项的界面效果;点击“进店必买选项”,显示其对应的界面效果。
4实验过程使用的图片如下:
四、实验指导
1.搭建菜单界面布局
(1)订单菜单界面主要包含三部分:顶部导航栏、左侧菜单栏和右侧菜单列表。搭建界面布局的步骤:
①创建项目程序,导入图片资源素材;
②在res→values目录下创建colors.xml文件,添加颜色定义代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="dark_gray">#626262</color>
<color name="gray">#f7f8f9</color>
</resources>
③在res→values目录下创建styles.xml文件,定义文本样式代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="tvNavigationBarStyles">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginLeft">25dp</item>
<item name="android:layout_marginTop">4dp</item>
<item name="android:textSize">16sp</item>
</style>
<style name="tvleftStyles">
<item name="android:layout_width">100dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:gravity">center</item>
<item name="android:textColor">@color/dark_gray</item>
<item name="android:textSize">16sp</item>
</style>
</resources>
④在res→layout目录下,在activity_main.xml中添加界面控件;创建left_layout.xml完成搭建左侧菜单栏布局;创建right_layout.xml文件搭建右侧菜单栏布局;创建list_item.xml完成创建listview的条目布局界面,并修改默认标题栏。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="4dp"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_order"
style="@style/tvNavigationBarStyles"
android:textColor="@color/black"
android:text="点菜"
/>
<TextView
android:id="@+id/tv_discuss"
android:layout_toRightOf="@id/tv_order"
style="@style/tvNavigationBarStyles"
android:textColor="@color/dark_gray"
android:text="评价"
/>
<TextView
android:id="@+id/tv_business"
style="@style/tvNavigationBarStyles"
android:textColor="@color/black"
android:layout_toRightOf="@id/tv_discuss"
android:text="商家"
/>
<TextView
android:layout_width="70dp"
android:layout_height="30dp"
android:textSize="12sp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:background="@drawable/left"
android:gravity="center"
android:textColor="#ef842c"
android:text="好友拼单"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<fragment
android:id="@+id/left"
android:name="cn.itcast.menu.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/left_layout"/>
<fragment
android:id="@+id/right"
android:name="cn.itcast.menu.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
tools:layout="@layout/right_layout"
/>
</LinearLayout>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="@+id/iv_img"
android:layout_width="70dp"
android:layout_height="70dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:orientation="vertical"
>
<TextView
android:textSize="14sp"
android:padding="2dp"
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#868788"
android:id="@+id/tv_sale"
android:textSize="12sp"/>
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
left_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_recommend"
style="@style/tvleftStyles"
android:background="@color/white"
android:text="推荐" />
<TextView
android:id="@+id/tv_must_buy"
style="@style/tvleftStyles"
android:text="进店必买"/>
</LinearLayout>
right_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null"
/>
</LinearLayout>
2.实现菜单界面功能
(1)封装菜品信息实体类FoodBean:
package cn.itcast.menu;
public class FoodBean {
private static final long serialVersionUID=1L;
private String name;
private String sales;
private String price;
private int img;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSales() {
return sales;
}
public void setSales(String sales) {
this.sales = sales;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
}
(2)加载左侧菜单栏界面布局,在LeftFragment.java中代码实现如下:
package cn.itcast.menu;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class LeftFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.left_layout,container,false);
return view;}
@Override
public void onPause() {super.onPause();}
}
(3)通过ListView实现菜单列表,编写其对应的适配器RightAdapter.java。
package cn.itcast.menu;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class RightAdapter extends BaseAdapter {
private Context mContext;
private List<FoodBean> list;
public RightAdapter(Context context , List<FoodBean>list){
this.mContext=context;
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if(convertView==null){
convertView=View.inflate(mContext,R.layout.list_item,null);
holder= new ViewHolder();
holder.tv_name=convertView.findViewById(R.id.tv_name);
holder.tv_sale=convertView.findViewById(R.id.tv_sale);
holder.tv_price=convertView.findViewById(R.id.tv_price);
holder.iv_img=convertView.findViewById(R.id.iv_img);
convertView.setTag(holder);
}else {
holder=(ViewHolder) convertView.getTag();
}
FoodBean bean=list.get(position);
holder.tv_name.setText(bean.getName());
holder.tv_sale.setText(bean.getSales());
holder.tv_price.setText(bean.getPrice());
holder.iv_img.setBackgroundResource(bean.getImg());
return convertView;
}
class ViewHolder{
TextView tv_name,tv_sale,tv_price;
ImageView iv_img;
}
}
(4)创建右侧Fragment来显示菜单栏列表信息,具体实现代码如下:
package cn.itcast.menu;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.io.Serializable;
import java.util.List;
public class RightFragment extends Fragment {
private ListView lv_list;
public RightFragment() {
}
public RightFragment getInstance(List<FoodBean> list) {
RightFragment rightFragment = new RightFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("list", (Serializable) list);
rightFragment.setArguments(bundle);
return rightFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_layout, container, false);
lv_list = view.findViewById(R.id.lv_list);
if (getArguments() != null) {
List<FoodBean> list = (List<FoodBean>) getArguments().getSerializable("list");
RightAdapter adapter = new RightAdapter(getActivity(), list);
lv_list.setAdapter(adapter);
}
return view;
}
}
(5)实现显示菜单的效果,在MainActivity.java中定义init()方法、setData()方法、clickEvent()方法和switchData()方法,分别实现初始化界面控件、实现界面控件点击事件和切换右侧菜单列表数据的功能,代码如下:
package cn.itcast.menu;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import android.app.Fragment;
import android.app.FragmentManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
private Fragment leftFragment;
private RightFragment rightFragment;
private TextView tv_recommed,tv_must_buy;
private String[]names1={"爆款*肥牛鱼豆腐骨肉相连三荤五素一份米饭","豪华双人套餐","【热销】双人套餐(含两份米饭)"};
private String[]sales1={"月售520 好评度80%","月售520 好评度80%","月售520 好评度80%"};
private String[]prices1={"$23","$41","$32"};
private int []imgs1={R.drawable.food1,R.drawable.food2,R.drawable.food3};
private String[]names2={"鸿运当头-油焖大虾","金肥牛跃龙门","香甜菠萝咕噜肉"};
private String[]sales2={"月售1314 好评度90%","月售999 好评度85%","月售520 好评度80%"};
private String[]prices2={"$36","$45","$29"};
private int []imgs2={R.drawable.foodd1,R.drawable.foodd2,R.drawable.foodd3};
private Map<String, List<FoodBean>> map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setData();
init();
clickEvent();
}
private void init(){//给主页面左侧的fragment界面控件赋值
fragmentManager = getFragmentManager();
leftFragment=fragmentManager.findFragmentById(R.id.left);
tv_recommed=findViewById(R.id.tv_recommend);
tv_must_buy=findViewById(R.id.tv_must_buy);
}
private void setData(){//给数据赋值将其全部放在对应的数据集里
map=new HashMap<>();
List<FoodBean>list1=new ArrayList<>();
List<FoodBean>list2=new ArrayList<>();
for(int i=0;i<names1.length;i++){
FoodBean bean=new FoodBean();
bean.setName(names1[i]);
bean.setPrice(prices1[i]);
bean.setImg(imgs1[i]);
bean.setSales(sales1[i]);
list1.add(bean);
}
map.put("1",list1);
for(int i=0;i<names2.length;i++){
FoodBean bean=new FoodBean();
bean.setName(names2[i]);
bean.setPrice(prices2[i]);
bean.setImg(imgs2[i]);
bean.setSales(sales2[i]);
list2.add(bean);
}
map.put("2",list2);
}
private void clickEvent(){//点击推荐还在必须控件变化颜色,可以增加可适度
tv_recommed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switchData (map.get("1"));
tv_recommed.setBackgroundColor(Color.WHITE);
}
});
tv_must_buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switchData (map.get("2"));
tv_must_buy.setBackgroundColor(Color.WHITE);
}
});
switchData (map.get("1"));
}
public void switchData(List<FoodBean> list){
rightFragment=new RightFragment().getInstance(list);//实例fragment
fragmentManager=getFragmentManager();//获取FragmentManager
fragmentTransaction=getSupportFragmentManager().beginTransaction();//开启事务
fragmentTransaction.replace(R.id.right,rightFragment);//添加一个Fragment
fragmentTransaction.commit();//提交事务
}
}
五、代码下载地址:
android: 实现注册界面、实现注册界面、饭堂小广播、音乐播放器、记事本、读取手机通讯录、学生管理系统 - Gitee.com