当前位置:   article > 正文

Android Studio(列表视图ListView)_android studio列表视图

android studio列表视图

前言

前面在适配器章节,已经介绍了ListView的作用(干什么的),这节将主要介绍如何去设计ListView页面视图。

思考

 列表视图需要些什么?

1. 列表项容器(装载各列表项的容器):<ListView/>

2. 列表项布局:my_list_item.xml

3. 列表所需数据:List<Object>、Adapter(桥梁)

 代码示例

下面的代码示例,是从我的项目中copy的是一个很好的例子

 

  1. // 1. 列表项容器:activity_charge_up.xml(仅展示了相关部分)
  2. <ListView
  3. android:id="@+id/cuisine_list"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. >
  7. </ListView>
  8. // 2. 列表项布局:cuisine_list_item.xml
  9. <?xml version="1.0" encoding="utf-8"?>
  10. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. >
  14. <RelativeLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="60dp"
  17. android:background="#FEFCEB"
  18. android:layout_margin="10dp"
  19. >
  20. <TextView
  21. android:id="@+id/cuisineName"
  22. android:layout_width="75dp"
  23. android:layout_height="wrap_content"
  24. android:layout_centerVertical="true"
  25. android:layout_marginLeft="20dp"
  26. android:text="西红柿炒鸡蛋"
  27. android:textColor="@color/black"
  28. android:textSize="12sp">
  29. </TextView>
  30. <TextView
  31. android:id="@+id/cuisinePrice"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:layout_centerVertical="true"
  35. android:layout_marginLeft="5dp"
  36. android:layout_toRightOf="@+id/cuisineName"
  37. android:text="¥99.90"
  38. android:textColor="#FF1100"
  39. android:textSize="12sp"
  40. android:textStyle="bold">
  41. </TextView>
  42. <Button
  43. android:id="@+id/subCuisine"
  44. android:layout_width="30dp"
  45. android:layout_height="30dp"
  46. android:layout_centerVertical="true"
  47. android:layout_toLeftOf="@+id/cuisineQuantity"
  48. android:textSize="5dp"
  49. android:background="@drawable/sub"
  50. >
  51. </Button>
  52. <TextView
  53. android:id="@+id/cuisineQuantity"
  54. android:layout_width="20dp"
  55. android:layout_height="wrap_content"
  56. android:layout_centerVertical="true"
  57. android:layout_toLeftOf="@+id/addCuisine"
  58. android:gravity="center"
  59. android:text="0"
  60. android:textColor="@color/black"
  61. android:textSize="10dp">
  62. </TextView>
  63. <Button
  64. android:id="@+id/addCuisine"
  65. android:layout_width="30dp"
  66. android:layout_height="30dp"
  67. android:background="@drawable/add"
  68. android:layout_alignParentRight="true"
  69. android:layout_centerVertical="true"
  70. android:textSize="5sp">
  71. </Button>
  72. </RelativeLayout>
  73. </RelativeLayout>

适配器的使用很简单:

  1. MyAdapter myAdapter = new MyAdapter(参数); 
  2. ListView cuisineList = findById(R.layout.cuisine_list);
  3. cuisineList.setAdapter(myAdapter);

复杂的是适配器的业务逻辑(适配器的实现):

    对于适配器的实现,可以参考前面适配器章节,这里就不再做具体的代码示例。

    在实际需求中,在适配器中你可能需要接受多个参数,你需要处理列表项的交互(譬如点击),你可能也需要即时地去改变列表项所在页面的相关控件内容(因为修改列表项一般也需要修改其他页面元素)。

 导航栏

其实学习ListView后,当想到设计导航栏后,可能会想到它能否胜任导航栏的设计呢?

        并不好去设计。首先,导航栏不仅有竖直的,还有水平的;其二,导航栏不仅仅是导航栏的变化,更涉及到导航内容的变化。

        所以导航栏的设计,一般有以下两种策略:

        (1) 自己设计:导航(LinearLayout)、导航页面内容(removeAllViews+addView+LayoutInflater)

        (2) 可用控件FragmentViewPager(参考链接:Android四种底部导航栏实现

后言

 下一节,将介绍RecyclerView的相关知识

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

闽ICP备14008679号