zoukankan      html  css  js  c++  java
  • AdapterView及其子类

    AdapterView是一组重要的组件,AdapterView本身是一个抽象基类,它派生的子类在用法上十分相似,只是显示接麦呢有一定区别。

    Adapter有以一下特征:

     >AdapterView继承了ViewGroup,它的本质是容器。

     >AdapterView可以把包括多个“列表项”,并将多个“列表项”以合适的形式显示出来

     >AdapterView显示的多个“列表项”由Adapter提供。调用AdapterView的setAdapter(Adaptert)方法设置Adapter即可。

    AdapterView派生出三个抽象类:AbsListView,AbsSpinner和AdapterViewAnimator,我们往往使用他们的的子类。

    http://a.cphotos.bdimg.com/timg?image&quality=100&size=b4000_4000&sec=1461657635&di=8c5b89e02fa20b686fa7d954ef124ad8&src=http://images2015.cnblogs.com/blog/499873/201510/499873-20151019191244645-1663409979.png

    先说说容器吧

      (注意:ListView.GridView,Spainner,Gallery等AdapterView都是容器,而Adapter负责提供每个“列表项”组件,AdapterView则负责采用合适的方式显示这些列表项。)

    如ListView

    布局文件中:

    <listView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:entries=@arry/books"
          android: divider="#f00"
          android:dividerHeight="2px"
          android:headerDividersEnabled="false"/>

    在定义的数组资源文件中:

    <?xml Version="1.1" encoding="utf-8"?>
     <resources>
        <item>aaaaaaaaaa</item>
         <item>aaaaaaaaaa</item>
             <item>aaaaaaaaaa</item>
          <item>aaaaaaaaaa</item>
          <item>aaaaaaaaaa</item>
    </resources>

    上面很简单的使用ListView搞出了一个列表,使用的是一个android:entries=@arry/books"指定的资源数组,但这无法改变列表项的外观。

    如果要ListView的外观,行为进行定制,就需要把ListView作为AdapterView使用,通过Adapter控制每个列表项的外观和行为。

    Adapter本身是一个接口,它派生出了ListAdapter和SpinnerAdapter两个接口,其中ListAdapter为AbsListView提供列表项,而SpinnerAdapter为AbsSpinner提供列表项。

    一.来说说常用的三个实现类吧:

    》ArryayAdapter:简单,易用的Adapter,通常用于将数组或List集合的多个值包装成多个列表项。

    》SimpleAdapter:用于将List集合的多个对象包装成多个列表项。

    》BaseAdapter:通常用于被拓展。拓展BaseAdapter可以对各列表项进行最大限度的定制。

    》SimpleCursorAdapter:与SimpleAdapter差不多,只是用来包装Cursor提供的数据。

    1.使用ArrayAdapter创建ListView

    和上面的布局代码差不多,只是不需要使用entries这个属性来确定使用的数组。

    然后我们需要一个TextView供ArrayAdapter使用

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"    
        android:textSize="24dp"
        android:padding="10px"
        android:shadowColor="#f0f"
        android:shadowDx="4"
        android:shadowDy="4"
        android:shadowRadius="2"/>

    我们在js文件中

    ListView list1 = (ListView) findViewById(R.id.list1);
            // 定义一个数组
            String[] arr1 = { "孙悟空", "猪八戒", "牛魔王" };
            // 将数组包装为ArrayAdapter
            ArrayAdapter<String> adapter1 = new ArrayAdapter<String>
                    (this, R.layout.array_item, arr1);
            // 为ListView设置Adapter
            list1.setAdapter(adapter1);

    还是很容易的,先定义数组,通过id找到组件,使用ArrayAdapter

    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.array_item, arr1);
    上面这行代码创建了一个ArrayAdaoter,这需要三个参数
    分别是context,textViewResourceId(该资源的id代表一个TextVie,该textView组件将作为ArrayAdaoter的列表组件。),数组或List

    上面的过程就是Adapter使用定义好的数组去填充我们定义的TextVie,然后把这些东西再给ListView 即 list1.setAdapter(adapter1);


    我们可以设置第二个参数的xml文件来实现不同的组件插入到ListView.但Arraydapter只能使用TextView列表项。如CheckTextView
    <CheckedTextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"    
        android:textSize="24dp"
        android:checkMark="@drawable/ok"
        android:shadowColor="#f0f"
        android:shadowDx="4"
        android:shadowDy="4"
        android:shadowRadius="2"/>
    CheckedTextView
    
    

    2.使用SimpleAdapter创建ListView

           首先,定义一个ListView

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-- 定义一个ListView -->
        <ListView android:id="@+id/mylist"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

          然后定义LitsView的列表项:我们显示一个头像和两句话

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <!-- 定义一个ImageView,用于作为列表项的一部分。 -->
    <ImageView android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:paddingLeft="10dp"/>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <!-- 定义一个TextView,用于作为列表项的一部分。 -->
    <TextView android:id="@+id/name"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textColor="#f0f"
        android:paddingLeft="10dp"/>
    <!-- 定义一个TextView,用于作为列表项的一部分。 -->
    <TextView android:id="@+id/desc"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:paddingLeft="10dp"/>
    </LinearLayout>
    </LinearLayout>

          接下来就是在java文件中对列表项进行填充,然后把列表项放入listView就ok了

    package org.crazyit.ui;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import static android.widget.AdapterView.OnItemClickListener;
    import static android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    
    public class MainActivity extends Activity
    {
        private String[] names = new String[]
                { "虎头", "弄玉", "李清照", "李白"};
        private String[] descs = new String[]
                { "可爱的小孩", "一个擅长音乐的女孩"
                        , "一个擅长文学的女性", "浪漫主义诗人"};
        private int[] imageIds = new int[]
                { R.drawable.tiger , R.drawable.nongyu
                        , R.drawable.qingzhao , R.drawable.libai};
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            // 创建一个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", imageIds[i]);
                listItem.put("personName", names[i]);
                listItem.put("desc", descs[i]);
                listItems.add(listItem);
            }
            // 创建一个SimpleAdapter
            SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems,
                    R.layout.simple_item,
                    new String[] { "personName", "header" , "desc"},
                    new int[] { R.id.name, R.id.header , R.id.desc });
            ListView list = (ListView) findViewById(R.id.mylist);
            // 为ListView设置Adapter
            list.setAdapter(simpleAdapter);
    
            // 为ListView的列表项的单击事件绑定事件监听器
            list.setOnItemClickListener(new OnItemClickListener()
            {
                // 第position项被单击时激发该方法
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id)
                {
                    System.out.println(names[position]
                            + "被单击了");
                }
            });
            // 为ListView的列表项的选中事件绑定事件监听器
            list.setOnItemSelectedListener(new OnItemSelectedListener()
            {
                // 第position项被选中时激发该方法
                @Override
                public void onItemSelected(AdapterView<?> parent, View view,
                                           int position, long id)
                {
                    System.out.println(names[position]
                            + "被选中了");
                }
                @Override
                public void onNothingSelected(AdapterView<?> parent)
                {
                }
            });
    
        }
    }
    mainActivity

       首先 创建一个List集合,List集合的元素是Map
    List<Map<String, Object>> listItems =new ArrayList<Map<String, Object>>();

    创建一个SimpleAdapter
    SimpleAdapter simpleAdapter = new SimpleAdapter(
    this,
    listItems,
    R.layout.simple_item,
    new String[] { "personName", "header" , "desc"},
    new int[] { R.id.name, R.id.header , R.id.desc }
    );
    一共5个参数。
    第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要
     第二个参数表示生成一个Map(String ,Object)列表选项
     第三个参数表示界面布局的id  表示该文件作为列表项的组件
     第四个参数表示该Map对象的哪些key对应value来生成列表项
     第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系
     注意的是map对象的key可以找不到 但组件的必须要有资源填充  因为 找不到key也会返回null 其实就相当于给了一个null资源


     


     

     
  • 相关阅读:
    【leetcode】腾讯精选练习 50 题(更新中)
    将博客搬至CSDN
    【笔记】linux基础(更新中)
    【寒窑赋】写在百篇博客之后
    【笔记】Vim
    【笔记】Git(更新中)
    【笔记】Java基础教程学习(更新中)
    【面试题】Java核心技术三十六讲(更新中)
    【leetcode】shell和sql题目思路汇总(更新中)
    【笔记】MySQL基础及高级特性(更新中)
  • 原文地址:https://www.cnblogs.com/leiyunjie/p/5435837.html
Copyright © 2011-2022 走看看