Android关于 ListView知识点

本文详细介绍了Android中的ListView控件,包括如何显示数据,ListView的数据适配原理,以及局部刷新的两种方法。此外,还讨论了如何实现ListView的Item单选和多选效果,涉及到choiceMode属性和Checkable接口的使用。
摘要由CSDN通过智能技术生成

一. 关于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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值