一. 关于ListView控件展现数据及原理
它以列表的形式展示具体数据内容,当数据过多时会出现滚动条,并且能够根据数据的长度适应屏幕的显示。
ListView是一个列表视图,由很多Item(条目)组 成,每个Item的布局都是相同的,这个Item会单独使用一个XML进行定义。
为了使ListView有数据显示需要进行数据适配,即数据与视图之间的桥梁,将复杂的数据换成用户可以接受的方式呈现。
Android实验三简单例子
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.hp.listview.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
simple_items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dp"
android:paddingLeft="10dp"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/images"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="0dp"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</LinearLayout>
MainActivity.java
package com.example.hp.listview;
import android.content.Context;
import android.graphics.Color;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private String[] names=new String[]{"Lion","Tiger","Monkey","Dog","Cat","elephant"};
private int[] image=new int[]{R.drawable.lion,R.drawable.tiger,R.drawable.monkey,R.drawable.dog ,R.drawable.cat,R.drawable.elephant};
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //设置当前的Activity无Title并且全屏
setContentView(R.layout.main); //所在的Activity采用R.layout下的main布局文件进行布局
final int color1=0xFFC5B5FF;
final int color2=0xFFFFFFFF;
//创建一个list集合,list集合的元素是Map
List<Map<String,Object>> ListItems=new ArrayList<Map<String, Object>>();
for(int i=0;i<names.length;i++){
Map<String,Object> listItem=new HashMap<String,Object>();
listItem.put("header",na