ListView和简单适配器

ListView

作为手机APP的最广泛的数据显示视图,ListView的使用显得非常重要。在实际的开发过程中,ListView是一个重头戏。

1.Listview 的结构

下面这幅图大概就是ListView的结构,直接上图。

这里写图片描述

2.Listview 的常用属性

divider:设置分割线的颜色;
dividerHeight:设置分割线的高度;
scrollbars:设置滚动条的隐藏或显示;
fadeScrollbars:自动隐藏滚动条;
entries:静态的文字内容,如下;
:

实现ListView的一般步骤

  1. 编写布局文件的代码(XML)
  2. 编写java代码:
    (1) 获取ListView对象
    (2)准备数据源
    (3)配置适配器
    (4)关联适配器和ListView

使用ArrayAdapter实现文字列表

XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

java代码

package com.example.dreamcreationman.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取ListView对象
        ListView listView= (ListView) findViewById(R.id.lv);
        //准备数据源,这里多写点数据,以便于观看滚动条
        String[] data={"1","2","3","4","5","6","7","1","2","3","4","5","6","7"};
        //配置适配器
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                data
        );
       //关联适配器和ListView
        listView.setAdapter(arrayAdapter);

    }
}

效果就是下面:
这里写图片描述

使用SimpleAdapter实现图文结合的列表

activity_main.xml和上面一样。我在这里新建一个名叫ti的XML文件。实现下图的效果。
XML代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<--注意一下控件一定要填写ID,以便于Adapter填充内容-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"/>

        <LinearLayout
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="android"
                android:textSize="15sp"/>
            <TextView
                android:id="@+id/version"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="v1.0"
                android:textSize="10sp"/>
            <TextView
                android:id="@+id/size"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="大小:11M"
                android:textSize="13sp"/>
        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>

java代码

package com.example.dreamcreationman.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView= (ListView) findViewById(R.id.lv);

//定义一个list接口集合
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
//用哈希表实现Map集合
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("logo",R.mipmap.ic_launcher);
        map.put("name","Android");
        map.put("version","v1.0");
        map.put("size","大小:11M");
        list.add(map);


//new一个新的对象,开辟新的内存空间
        map=new HashMap<String, Object>();
        map.put("logo",R.mipmap.ic_launcher);
        map.put("name","Java");
        map.put("version","v2.0");
        map.put("size","大小:12M");
        list.add(map);

        map=new HashMap<String, Object>();
        map.put("logo",R.mipmap.ic_launcher);
        map.put("name","C++");
        map.put("version","v3.0");
        map.put("size","大小:13M");
        list.add(map);

        map=new HashMap<String, Object>();
        map.put("logo",R.mipmap.ic_launcher);
        map.put("name","C");
        map.put("version","v4.0");
        map.put("size","大小:14M");
        list.add(map);
//新建一个简单适配器,参数:上下文,数据源,资源id,key键(控件名),值(传到点)
        SimpleAdapter simpleAdapter=new SimpleAdapter(
                this,
                list,
                R.layout.ti,
                new String[]{"logo","name","version","size"},
                new int[]{R.id.logo,R.id.name,R.id.version,R.id.size}
        );
        listView.setAdapter(simpleAdapter);
    }
}

效果图就是这样

这里写图片描述

PS:button会在布局里优先获取焦点,所以当在行布局里含有Button时,item项不能被选中,解决办法是在items上加上android:descendantFocusability=blocksDescendants属性,使得item获取焦点的优先级最高。

好了有关Android Studio里ListView和简单适配器的分享就到这里了,欢迎在评论区留言指出错误,希望和大家一起进步!

谢谢观看。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值