目录
文章目录
为什么写这篇博客
今天重温了一遍ListView的用法,边看边结合自己手上的两本书写了百来行代码,然后觉得需要归纳总结一波,因此写下来,一方面巩固已有的知识,另一方面也希望看到的人少走弯路。博主也在认真学习AS,仅供参考,如果遇到了什么错误,希望看到的大佬们不要让它留在那里了!可以积极批评指正,一同进步。
正经的正文
内容简介
其实也没什么神秘的,就是一个简单的ListView的展示和响应事件的编写。
工具
- Android Studio
- gradle version : 4.1
- Android Plugin version :3.0.1
代码正文及说明
写在代码前面的小说明
想了想还是稍微说明一下吧。诚如我们所见,ListView不是像Button一样只要写上去就能能直接用的控件,要让一个ListView控件能正常使用,我们需要:一个ListView、每个ListView都包含了几个Item(大多数情况下它们都长得一样,不一样的我还没有去探索过),还需要一个数据适配的类,这里使用的是BaseAdapter,顾名思义,它是个基本的适配器,其他类型的还有SimpleAdapter、ArrayAdapter等。这里的代码并没有涉及到对ListView的优化,后续有时间会做一下更新(挖坑……)。
main_layout.xml
这个文件是进行ListView的布局。
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cryst.first.MainLayoutActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:divider="#d9d9d9"
android:dividerHeight="1dp"
></ListView>