ListView的实现步骤
1.新建项目ListViewDemo
2.演示基础款的ListView
2.1 新建ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">
<ListView
android:id="@+id/listviewdemo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
</LinearLayout>
显示这样的轮廓
2.2 新建ListView lv
private ListView lv;
lv=findViewById(R.id.listviewdemo);
2.3 新建演示水果数组
String[] fruit = {
"西瓜","香蕉","猕猴桃","草莓","梨","苹果","芒果"};
2.4 新建adapter
传递一个系统自带的xml,和我们刚建的水果数组
ArrayAdapter arrayAdapter =new ArrayAdapter(this,android.R.layout.simple_list_item_1,fruit);
2.5 对ListView lv使用我们新建的adapter
lv.setAdapter(arrayAdapter);
2.6基础款操作小结
ListView涉及的步骤分三步:
新建ListView,并find LIstView(正常操作步骤)
- 新建数组,用于显示在LIst
- 建立adapter(传递模型和数组)
- 对LIstView 使用adapter
3. 豪华款升级操作
3.1 改变模板
由于基础款演示 用的是系统自带的android.R.layout.simple_list_item_1模板,我们看下自带模板内容
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />