当前位置:   article > 正文

AndroidFragment仿写美团外卖界面_android仿美团外卖首页

android仿美团外卖首页

使用fragment来实现页面的丝滑切换,为各大程序的界面做出了卓远的贡献!

今天分享的时之前课堂练习fragment的一个小作业,在没有学习接触fragment之前我实现点击图标来完成页面之间的来回切换主要时用intent方法来实现,我们知道intent函数的功能有很多,不仅可以实现页面的切换,数据的传递以及可以条用第三方的api等等,但在实现页面切换上面貌似表现的不太友好,故此需要适当条用fragment,基本上软件的到导航状态栏就是用fragment来实现的。

下面时该实验作业的具体代码,大家代码可以作为参考,如果需要项目的文件的也可以私信我,看到会第一时间回复。

activity_main.xml:主界面布局了顶部的点菜,商家,评论,好友拼单按钮控件,左(推荐和必买)右栏(显示菜的内容)fragment

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity"
  8. android:orientation="vertical">
  9. <RelativeLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="50dp"
  12. android:layout_marginBottom="4dp"
  13. android:gravity="center_vertical"
  14. >
  15. <TextView
  16. android:id="@+id/tv_order"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_alignParentStart="true"
  20. android:layout_alignParentLeft="true"
  21. android:layout_alignParentTop="true"
  22. android:layout_alignParentEnd="true"
  23. android:layout_alignParentRight="true"
  24. android:layout_marginStart="25dp"
  25. android:layout_marginLeft="25dp"
  26. android:layout_marginTop="12dp"
  27. android:layout_marginEnd="346dp"
  28. android:layout_marginRight="346dp"
  29. android:text="点菜"
  30. android:textSize="20sp" />
  31. <TextView
  32. android:id="@+id/tv_business"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_alignParentStart="true"
  36. android:layout_alignParentLeft="true"
  37. android:layout_alignParentTop="true"
  38. android:layout_alignParentEnd="true"
  39. android:layout_alignParentRight="true"
  40. android:layout_marginStart="108dp"
  41. android:layout_marginLeft="108dp"
  42. android:layout_marginTop="12dp"
  43. android:layout_marginEnd="263dp"
  44. android:layout_marginRight="263dp"
  45. android:text="商家"
  46. android:textSize="20sp" />
  47. <TextView
  48. android:id="@+id/tv_discuss"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_alignParentStart="true"
  52. android:layout_alignParentLeft="true"
  53. android:layout_alignParentTop="true"
  54. android:layout_alignParentEnd="true"
  55. android:layout_alignParentRight="true"
  56. android:layout_marginStart="202dp"
  57. android:layout_marginLeft="202dp"
  58. android:layout_marginTop="11dp"
  59. android:layout_marginEnd="169dp"
  60. android:layout_marginRight="169dp"
  61. android:text="评价"
  62. android:textSize="20sp" />
  63. <TextView
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:layout_alignParentTop="true"
  67. android:layout_alignParentEnd="true"
  68. android:layout_alignParentRight="true"
  69. android:layout_marginTop="13dp"
  70. android:layout_marginEnd="28dp"
  71. android:layout_marginRight="28dp"
  72. android:text="好友拼单"
  73. android:textSize="20sp" />
  74. </RelativeLayout>
  75. <LinearLayout
  76. android:layout_width="match_parent"
  77. android:layout_height="match_parent"
  78. android:orientation="horizontal"
  79. >
  80. <fragment
  81. android:id="@+id/left"
  82. android:name="com.example.menu.LeftFragment"
  83. android:layout_width="wrap_content"
  84. android:layout_height="match_parent"
  85. android:layout_weight="1"
  86. tools:layout="@layout/fragment_left"/>
  87. <fragment
  88. android:id="@+id/right"
  89. android:name="com.example.menu.RightFragment"
  90. android:layout_width="wrap_content"
  91. android:layout_height="match_parent"
  92. android:layout_weight="3"
  93. tools:layout="@layout/fragment_right"
  94. />
  95. </LinearLayout>
  96. </LinearLayout>

FoodBean:Java文件实现对菜的基本信息(名称,内容,价钱,评论数等)的实例化

  1. package com.example.menu;
  2. import java.io.Serializable;
  3. public class FoodBean implements Serializable {
  4. private static final long serialVersionUID=1L;
  5. private String name;
  6. private String sales;
  7. private String price;
  8. private int img;
  9. public String getName() {
  10. return name;
  11. }
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15. public String getSales() {
  16. return sales;
  17. }
  18. public void setSales(String sales) {
  19. this.sales = sales;
  20. }
  21. public String getPrice() {
  22. return price;
  23. }
  24. public void setPrice(String price) {
  25. this.price = price;
  26. }
  27. public int getImg() {
  28. return img;
  29. }
  30. public void setImg(int img) {
  31. this.img = img;
  32. }
  33. }

MainActivity:java文件,对主页面的数据内容以及控件的点击功能经行一个实现

  1. package com.example.menu;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.fragment.app.FragmentTransaction;
  4. import android.app.Fragment;
  5. import android.app.FragmentManager;
  6. import android.content.Intent;
  7. import android.graphics.Color;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. public class MainActivity extends AppCompatActivity {
  17. private FragmentManager fragmentManager;
  18. private FragmentTransaction fragmentTransaction;
  19. private Fragment leftFragment;
  20. private RightFragment rightFragment;
  21. private TextView tv_recommed,tv_must_buy;
  22. private TextView btn;
  23. private String[]names1={"爆款*肥牛鱼豆腐骨肉相连三荤五素一份米饭","豪华双人套餐","【热销】双人套餐(含两份米饭)","家乡菜爆炒花甲","家乡菜八仙过海","家乡菜迷魂汤","家乡菜小肉丸","家乡菜辣子鸡","家乡菜香辣龙虾"};
  24. private String[]sales1={"月售520 好评度80%","月售520 好评度80%","月售520 好评度80%","月售520 好评度80%","月售520 好评度80%","月售520 好评度80%","月售520 好评度80%","月售520 好评度80%","月售520 好评度80%"};
  25. private String[]prices1={"$23","$41","$32","$23","$41","$32","$23","$41","$32"};
  26. private int []imgs1={R.drawable.recom_one,R.drawable.recom_two,R.drawable.recom_three,R.drawable.recom_four,R.drawable.recom_five,R.drawable.recom_six,R.drawable.recom_seven,R.drawable.recom_eight,R.drawable.recom_nine};
  27. private String[]names2={"素菜主义一人套餐","两人经典套套餐","三人经典套餐"};
  28. private String[]sales2={"月售520 好评度80%","月售520 好评度80%","月售520 好评度80%"};
  29. private String[]prices2={"$23","$41","$32"};
  30. private int []imgs2={R.drawable.must_buy_one,R.drawable.must_buy_two,R.drawable.must_buy_three};
  31. private Map<String, List<FoodBean>>map;
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.activity_main);
  36. setData();
  37. init();
  38. clickEvent();
  39. }
  40. private void init(){//给主页面左侧的fragment界面控件赋值
  41. fragmentManager = getFragmentManager();
  42. leftFragment=fragmentManager.findFragmentById(R.id.left);
  43. tv_recommed=findViewById(R.id.tv_recommend);
  44. tv_must_buy=findViewById(R.id.tv_must_buy);
  45. }
  46. private void setData(){//给数据赋值将其全部放在对应的数据集里
  47. map=new HashMap<>();
  48. List<FoodBean>list1=new ArrayList<>();
  49. List<FoodBean>list2=new ArrayList<>();
  50. for(int i=0;i<names1.length;i++){
  51. FoodBean bean=new FoodBean();
  52. bean.setName(names1[i]);
  53. bean.setPrice(prices1[i]);
  54. bean.setImg(imgs1[i]);
  55. bean.setSales(sales1[i]);
  56. list1.add(bean);
  57. }
  58. map.put("1",list1);
  59. for(int i=0;i<names2.length;i++){
  60. FoodBean bean=new FoodBean();
  61. bean.setName(names2[i]);
  62. bean.setPrice(prices2[i]);
  63. bean.setImg(imgs2[i]);
  64. bean.setSales(sales2[i]);
  65. list2.add(bean);
  66. }
  67. map.put("2",list2);
  68. }
  69. private void clickEvent(){//点击推荐还在必须控件变化颜色,可以增加可适度
  70. tv_recommed.setOnClickListener(new View.OnClickListener() {
  71. @Override
  72. public void onClick(View view) {
  73. switchData (map.get("1"));
  74. tv_recommed.setBackgroundColor(Color.WHITE);
  75. }
  76. });
  77. tv_must_buy.setOnClickListener(new View.OnClickListener() {
  78. @Override
  79. public void onClick(View view) {
  80. switchData (map.get("2"));
  81. tv_must_buy.setBackgroundColor(Color.WHITE);
  82. }
  83. });
  84. switchData (map.get("1"));
  85. }
  86. public void switchData(List<FoodBean> list){
  87. rightFragment=new RightFragment().getInstance(list);//实例fragment
  88. fragmentManager=getFragmentManager();//获取FragmentManager
  89. fragmentTransaction=getSupportFragmentManager().beginTransaction();//开启事务
  90. fragmentTransaction.replace(R.id.right,rightFragment);//添加一个Fragment
  91. fragmentTransaction.commit();//提交事务
  92. }
  93. }

fragment_left.xml:布局文件,主页面左侧的推荐和必买fragment界面的布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".LeftFragment"
  7. android:orientation="vertical">
  8. <!-- TODO: Update blank fragment layout -->
  9. <TextView
  10. android:id="@+id/tv_recommend"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="推荐"
  14. android:textSize="25sp"
  15. android:layout_marginTop="10dp"
  16. android:layout_marginLeft="20dp"
  17. />
  18. <TextView
  19. android:id="@+id/tv_must_buy"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="进店必买"
  23. android:textSize="25sp"
  24. android:layout_marginTop="80dp"
  25. android:layout_marginLeft="20dp"
  26. />
  27. </FrameLayout>

fragment_right.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".RightFragment"
  7. android:orientation="vertical">
  8. <!-- TODO: Update blank fragment layout -->
  9. <ListView
  10. android:id="@+id/lv_list"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:divider="@null"
  14. />
  15. </FrameLayout>

list_item.xml:和fragment-right.xml共同结合实现对数据(菜的内容布局)的整体布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal"
  6. android:padding="5dp"
  7. >
  8. <ImageView
  9. android:id="@+id/iv_img"
  10. android:layout_width="70dp"
  11. android:layout_height="70dp"/>
  12. <LinearLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_marginLeft="8dp"
  16. android:layout_marginRight="8dp"
  17. android:orientation="vertical"
  18. >
  19. <TextView
  20. android:textSize="14sp"
  21. android:padding="2dp"
  22. android:id="@+id/tv_name"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"/>
  25. <TextView
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:textColor="#868788"
  29. android:id="@+id/tv_sale"
  30. android:textSize="12sp"/>
  31. <TextView
  32. android:id="@+id/tv_price"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_marginTop="2dp"
  36. android:textSize="12sp"/>
  37. </LinearLayout>
  38. </LinearLayout>

Right Adapter:菜的数据的适配器

  1. package com.example.menu;
  2. import android.content.Context;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.BaseAdapter;
  6. import android.widget.ImageView;
  7. import android.widget.TextView;
  8. import java.util.List;
  9. public class RightAdapter extends BaseAdapter {
  10. private Context mContext;
  11. private List<FoodBean>list;
  12. public RightAdapter(Context context , List<FoodBean>list){
  13. this.mContext=context;
  14. this.list=list;
  15. }
  16. @Override
  17. public int getCount() {
  18. return list.size();
  19. }
  20. @Override
  21. public Object getItem(int position) {
  22. return list.get(position);
  23. }
  24. @Override
  25. public long getItemId(int position) {
  26. return position;
  27. }
  28. @Override
  29. public View getView(int position, View convertView, ViewGroup parent) {
  30. ViewHolder holder=null;
  31. if(convertView==null){
  32. convertView=View.inflate(mContext,R.layout.list_item,null);
  33. holder= new ViewHolder();
  34. holder.tv_name=convertView.findViewById(R.id.tv_name);
  35. holder.tv_sale=convertView.findViewById(R.id.tv_sale);
  36. holder.tv_price=convertView.findViewById(R.id.tv_price);
  37. holder.iv_img=convertView.findViewById(R.id.iv_img);
  38. convertView.setTag(holder);
  39. }else {
  40. holder=(ViewHolder) convertView.getTag();
  41. }
  42. FoodBean bean=list.get(position);
  43. holder.tv_name.setText(bean.getName());
  44. holder.tv_sale.setText(bean.getSales());
  45. holder.tv_price.setText(bean.getPrice());
  46. holder.iv_img.setBackgroundResource(bean.getImg());
  47. return convertView;
  48. }
  49. class ViewHolder{
  50. TextView tv_name,tv_sale,tv_price;
  51. ImageView iv_img;
  52. }
  53. }

 截图:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/738560
推荐阅读
相关标签
  

闽ICP备14008679号