本文实例为大家分享了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);

 }
}

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

使用ListView实现网上订餐首页的更多相关文章

  1. ios – 在React Native中设置ListView的高度

    我需要设置ListView的宽度和高度.当设置宽度按预期工作时,设置高度无效,ListView总是伸展到屏幕的几乎底部.我用这种方式在render方法中创建ListView:这是它的风格:我还尝试通过此命令在方法中设置其高度:当我尝试使用此命令设置宽度时,它可以工作.但设定高度什么都不做.如何将ListView的高度设置为所需的值?

  2. Android ListView中的弹出菜单问题

    )方法并把方法在您的适配器类中.注意:act是在创建构造函数适配器时必须绑定的Activity,例如:在Activity中,您可以编写代码:

  3. android – 在listview中添加viewpager作为滚动标题

    我试图在列表标题中添加Viewpager(使用支持库4),但它没有显示任何内容.这是我的代码请帮忙.它将在列表标题中不作为项目,因此它不应该是一个问题.解决方法listadapter之后的listview页眉和页脚显示.如果你尝试setadapter,并且看不到viewpager.检查viewpager的宽度和高度.如果viewpager的宽度或高度值为0.在viewgroup中创建LienarL

  4. android – Listview滚动错误5.1

    刚注意到ListView中一个非常奇怪的错误,似乎只能重现5.1,我真的很想知道没有人提起它.很容易重现:查找具有足够项目的ListView,滚动到列表的3/4,现在快速向上滚动,您会注意到ListView滚动一直到底部!现在IMO这是一个非常严重的错误,我想尽快找到解决方法/修复,所以任何人都有一个?更新:ListView.java没有从SDK21更改,但AbsListView.java没有.解决方法您可以使用回收站视图而不是列表视图,因为Google可能不再支持它.试试YouTube上的幻灯片视频.它

  5. android – 在ListView中禁用项目焦点

    当我点击它时,如何从ListView中的Item禁用焦点,以便没有关注点击?解决方法以下是禁用ListView焦点的答案.在适配器中覆盖isEnabled方法并返回false.

  6. Android – 从ListView中删除项目时的动画

    我在一个活动中有一个ListView.在每个列表项中都有一个文本和一个CheckBox.单击CheckBox,我想从ListView中删除该项目,并希望添加一个动画,如向下滑动或淡出,然后删除.有帮助吗?

  7. android – 如何在ListView中正确使用TextSwitcher?

    ),然后播放动画.text2到text1的更改正确发生.我的理解原因如下–在显示text2之前,itemTime的所有视图都被删除,因此它被重新创建,这就是为什么一些其他值被显示为秒的原因.但为什么它显示其他记录的价值呢?

  8. android – 带复选框的ListView项目 – 如何删除复选框涟漪效应?

    我有一个带有项目的ListView包含一个复选框和一些其他元素.问题是,当我点击Android5设备上的列表项时,我看起来像这样:我不希望在复选框周围产生涟漪效应.我怎么能实现这一点?

  9. android – 如何在ListView中的同一行放置多个项目?

    我有一个链接到自定义适配器的项目的ArrayList.这些项目按字母顺序排列.我希望我的ListView在ListView的每一行上最多有四个项目,并按字母顺序分隔行.例如:[嗨][医院][酒店][热点][屋][不可能][不大可能]因此,多个项目将位于ListView的同一行.有没有办法在自定义Adapter类的getView()方法中使用位置变量来实现这一目的?如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  10. android – 允许用户选择SD卡上的文件夹

    我正在寻找一种方法,允许用户按下按钮浏览SD卡的内容,然后选择一个文件夹,其路径将返回给应用程序.类似于在Windows上启动“浏览…”

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部