使用ListView实现网上订餐首页

2022-07-25,,,

本文实例为大家分享了listview实现网上订餐首页的具体代码,供大家参考,具体内容如下

效果图

布局文件

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".simpleadapteractivity">
<include layout="@layout/title"/>
 <imageview
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:src="@drawable/chinaweidao"/>
  <listview
    android:dividerheight="5dp"
    android:id="@+id/lv_listview"
    android:layout_margintop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</linearlayout>
android:layout_width="match_parent"
  android:layout_height="100dp"
  android:layout_margin="10dp">
  <imageview
    android:id="@+id/food_image"
    android:layout_width="80dp"
    android:layout_height="80dp"/>
  <linearlayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3">
    <textview
      android:id="@+id/food_name"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:layout_marginleft="10dp"
      android:text="标题"
      android:textsize="16sp"
      android:gravity="center_vertical"
      android:textcolor="#000000"/>
    <textview
      android:id="@+id/food_content"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:layout_marginleft="10dp"
      android:text="内容"
      android:textsize="12sp"
      android:gravity="center_vertical"/>
    <linearlayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
      <include layout="@layout/welfarestyle"/>
      <textview
        android:id="@+id/tv_welfare"
        android:layout_marginleft="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"/>
    </linearlayout>


  </linearlayout>
  <linearlayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <textview
      android:id="@+id/tv_time"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:text="时间"
      android:textsize="10sp"
      android:gravity="center_vertical"/>

</linearlayout>

title.xml

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_style">
<textview
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="店铺"
    android:textcolor="#ffffff"
    android:textsize="20sp"
    android:textstyle="bold"
    android:gravity="center"/>

设置福利图标的样式

android:layout_width="match_parent"
android:layout_height="match_parent">
<textview
    android:layout_marginleft="10dp"
    android:layout_width="30dp"
    android:layout_height="20dp"
    android:text="福利"
    android:textcolor="#fff"
    android:background="@drawable/welfare"/>

点击listview后跳转的页面布局

android:layout_width="match_parent"
android:layout_height="400dp"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_margin="5dp"
tools:context=".firstactivity">
<textview
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="菜单"
    android:layout_margin="10dp"
    android:textsize="20dp"
    android:textcolor="#000000"/>
<listview
    android:layout_margin="8dp"
    android:id="@+id/lv_food"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="10dp">
<imageview
    android:id="@+id/first_image"
    android:layout_width="80dp"
    android:layout_height="80dp"/>
  <linearlayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3">
    <textview
      android:id="@+id/first_name"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:layout_marginleft="10dp"
      android:text="标题"
      android:textsize="16sp"
      android:gravity="center_vertical"
      android:textcolor="#000000"/>
    <textview
      android:id="@+id/first_ingredient"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:layout_marginleft="10dp"
      android:text="内容"
      android:textsize="12sp"
      android:textcolor="#000000"
      android:gravity="center_vertical"/>
    <linearlayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">/>
      <textview
        android:id="@+id/tv_money"
        android:layout_marginleft="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="价格"/>
</linearlayout>

</linearlayout>
<linearlayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1.5">
    <button
      android:id="@+id/first_btn_join"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="加入购物车"
      android:background="@android:drawable/btn_default"
      android:layout_gravity="center_vertical" />

</linearlayout>
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="10dp">
<imageview
    android:id="@+id/first_image"
    android:layout_width="80dp"
    android:layout_height="80dp"/>
  <linearlayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3">
    <textview
      android:id="@+id/first_name"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:layout_marginleft="10dp"
      android:text="标题"
      android:textsize="16sp"
      android:gravity="center_vertical"
      android:textcolor="#000000"/>
    <textview
      android:id="@+id/first_ingredient"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:layout_marginleft="10dp"
      android:text="内容"
      android:textsize="12sp"
      android:textcolor="#000000"
      android:gravity="center_vertical"/>
    <linearlayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">/>
      <textview
        android:id="@+id/tv_money"
        android:layout_marginleft="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="价格"/>
</linearlayout>

</linearlayout>
<linearlayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1.5">
    <button
      android:id="@+id/first_btn_join"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="加入购物车"
      android:background="@android:drawable/btn_default"
      android:layout_gravity="center_vertical" />

</linearlayout>

java文件

public class simpleadapteractivity extends appcompatactivity {
  private listview listview;
  private list<map<string,object>> data = new arraylist<>();

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_list_view);
//    actionbar actionbar = getsupportactionbar();
    if (actionbar!= null){
      actionbar.hide();
    }
    initview();
  }
  public void initview(){
    listview = findviewbyid(r.id.lv_listview);
    map<string, object> map = new hashmap<string, object>();
    map.put("icon", r.drawable.shop1);
    map.put("name", "蛋糕房");
    map.put("content", "月售996\n起送¥35|配送¥5");
    map.put("welfare","进店可获得一个香草冰淇淋");
    map.put("time","配送约2.5小时");
    data.add(map);
    map = new hashmap<string, object>();
    map.put("icon", r.drawable.shop2);
    map.put("name", "爪哇咖啡.西餐.酒吧");
    map.put("content", "月售11\n起送¥20|配送¥7");
    map.put("welfare","进店即可送一杯拿铁咖啡");
    map.put("time","配送约40分钟");
    data.add(map);
    map = new hashmap<string, object>();
    map.put("icon", r.drawable.shop3);
    map.put("name", "必胜客");
    map.put("content", "月售10\n起送¥15|配送¥6");
    map.put("welfare","下单即可获得一个¥5优惠券");
    map.put("time","配送约20分钟");
    data.add(map);
    map = new hashmap<string, object>();
    map.put("icon", r.drawable.shop4);
    map.put("name", "艾尚夜宵");
    map.put("content", "月售496\n起送¥20|配送¥13");
    map.put("welfare","下单即可获得一个¥15优惠券");
    map.put("time","配送约42分钟");
    data.add(map);
    map = new hashmap<string, object>();
    map.put("icon", r.drawable.shop5);
    map.put("name", "上岛咖啡");
    map.put("content", "月售800\n起送¥30|配送¥10");
    map.put("welfare","下单即可获得一个¥30优惠券");
    map.put("time","配送约30分钟");
    data.add(map);

    simpleadapter adapter = new simpleadapter(this,
        data,r.layout.listview_item,new string[]{"icon","name","content","welfare","time"},
        new int []{r.id.food_image,r.id.food_name,r.id.food_content,r.id.tv_welfare,r.id.tv_time});
    //设置listview的适配器,这里使用simpleadapter;
    listview.setadapter(adapter);
    listview.setonitemclicklistener(new adapterview.onitemclicklistener() {
      @override
      public void onitemclick(adapterview<?> adapterview, view view, int i, long l) {
        map map = data.get(i);
        /* toast.maketext(simpleadapteractivity.this, "你点击了"+map.get("name")+map.get("content"), toast.length_short).show();*/
        intent intent = new intent(simpleadapteractivity.this,firstactivity.class);
        startactivity(intent);
      }
    });
  }
}
public class firstactivity extends appcompatactivity {
  private button btnjoin;
  private listview listview;
  private list<map<string,object>> data = new arraylist<>();

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_first);
    btnjoin = findviewbyid(r.id.first_btn_join);
    initview();
  }
  public void initview(){
    listview = findviewbyid(r.id.lv_food);
    map<string, object> map = new hashmap<string, object>();
    map.put("icon", r.drawable.food1);
    map.put("name", "招牌丰收硕果12寸");
    map.put("ingredient", "水果、奶油、面包、鸡蛋");
    map.put("money","¥198");
    map.put("btn","加入购物车");
    data.add(map);
    map = new hashmap<string, object>();
    map.put("icon", r.drawable.food2);
    map.put("name", "玫瑰花创意蛋糕");
    map.put("ingredient", "玫瑰花、奶油、鸡蛋");
    map.put("money","¥148");
    map.put("btn","加入购物车");
    data.add(map);
    map = new hashmap<string, object>();
    map.put("icon", r.drawable.food3);
    map.put("name", "布朗熊与可妮");
    map.put("ingredient", "奶油、巧克力、果粒夹层");
    map.put("money","¥98");
    map.put("btn","加入购物车");
    data.add(map);

    simpleadapter adapter = new simpleadapter(this,
        data,r.layout.activity_first_list,new string[]{"icon","name","ingredient","money","btn"},
        new int []{r.id.first_image,r.id.first_name,r.id.first_ingredient,r.id.tv_money,r.id.first_btn_join});
    //设置listview的适配器,这里使用simpleadapter;
    listview.setadapter(adapter);

 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《使用ListView实现网上订餐首页.doc》

下载本文的Word格式文档,以方便收藏与打印。