Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1571776
  • 博文数量: 157
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4116
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-14 18:26
文章分类

全部博文(157)

文章存档

2014年(67)

2013年(90)

分类: Android平台

2013-08-26 09:02:38

所谓高级控件就是指不能拖过来直接用的控件,我们需要在对它们进行管理和控制之后才能使用,与之一起使用的还有一些对应的控制器、相关数据集等。这一篇里我们先简单介绍一下高级控件中的AdapterView控件,当然我们也需要先要知道与之对应的控制器——适配器Adapter,以及数据源DataSource,和三者之间的关系。


   我们都应该听过或者了解MVC框架吧,MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用于组织代码用一种业务逻辑和数据显示分离的方法,这个方法的假设前提是如果业务逻辑被聚集到一个部件里面,而且界面和用户围绕数据的交互能被改进和个性化定制而不需要重新编写业务逻辑MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。MVC的结构图如下:

[捕获.JPG]

   到此为止,我们已经明白了这个框架的原理,简单的来说就是利用控制器(C)将数据集(M)与视图(V)对应起来。好吧,我们用这个思想说说Adapter、AdapterView和DataSource之间的关系:

   Adapter——C(控制器)

   AdapterView——V(视图)

   DataSource——M(数据集)

   

   明白了这个关系,我们就可以进入今天的主讲内容了:

(1)Adapter类及其相关类和接口;

(2)AdapterView控件之一ListView;


1.ArrayAdapter类的使用

   通过ArrayAdapter我们只能绑定数据到TextView控件中,其他的不多说,直接看下面的代码,注意的地方我都标记了注释:

(1)MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.example.arrayadapter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
    //声明视图ListView控件
    private ListView lv;
    //声明适配器(控制器)ArrayAdapter
    private ArrayAdapter aAdapter;
    //声明并定义数据源
    private String datas[]={
            "Wireless & networks","Call settings","Sound","Display","Location & security",
            "Applications","Accounts & sync","Privacy","CD card & phone storage","Search","Language & keyboard",
            "Voice input & output","Accessibility","Date & Time","About phone"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实现适配器,包括三个参数
        aAdapter=new ArrayAdapter(this, R.layout.cell,datas);
        lv=(ListView)findViewById(R.id.listView1);
        //通过Adapter的setAdapter方法绑定数据于视图
        lv.setAdapter(aAdapter);
                                                                                                                                                                                            
    }
                                                                                                                                                                                        
}

(2)activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    xmlns:tools=""
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".MainActivity" >
    
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        >
    

(3)引入的资源文件cell.xml

1
2
3
4
5
6
7
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:gravity="center_vertical"
    android:id="@+id/tv"
    android:textColor="#ffffff">

(4)实现效果如下:


2.SimpleAdapter类的使用

   相对于ArrayAdapter而言SimpleAdapter的实现的功能更全面一些,不仅可也实现TextView的绑定,还可以在引用的资源文件(cell.xml)中使用布局,从而绑定ImageView等其他一些控件,接下来我们就来完善上面这个布局:

(1)MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.example.arrayadapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
    //声明视图ListView控件
    private ListView lv;
    //声明适配器(控制器)SimpleAdapter
    private SimpleAdapter sAdapter;
    //声明List和Map存放数据
    private List> lists;
    private Map maps;
    //声明并定义数据源
    private String datas[]={
            "Wireless & networks","Call settings","Sound","Display","Location & security",
            "Applications","Accounts & sync","Privacy","CD card & phone storage","Search",
            "Language & keyboard","Voice input & output","Accessibility","Date & Time","About phone"};
    private int image[]={
            R.drawable.ic_settings_wireless,R.drawable.ic_settings_call,R.drawable.ic_settings_sound,
            R.drawable.ic_settings_display,R.drawable.ic_settings_location,R.drawable.ic_settings_applications,
            R.drawable.ic_settings_sync,R.drawable.ic_bt_config,R.drawable.ic_settings_sim,
            R.drawable.ic_power_system,R.drawable.ic_settings_language,R.drawable.ic_settings_voice_calls,
            R.drawable.ic_settings_accessibility,R.drawable.ic_settings_date_time,R.drawable.ic_settings_about};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView)findViewById(R.id.listView1);
        lists=new ArrayList>();
        //通过for循环添加数据
        for(int i=0;i
            maps=new HashMap();
            maps.put("data", datas[i]);
            maps.put("image", image[i]);
            lists.add(maps);
        }
        //实现SimpleAdapter适配器,有5个参数
        sAdapter=new SimpleAdapter(this, lists, R.layout.cell, new String[]{"data","image"}, new int[]{R.id.tv,R.id.iv});      
        //通过Adapter的setAdapter方法绑定数据于视图
        lv.setAdapter(sAdapter);
                                                                                                 
    }
                                                                                             
}

(2)activity_main.java同上

(3)资源文件cell.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    xmlns:tools=""
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >
    
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:id="@+id/iv"/>
    
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:gravity="center_vertical"
        android:id="@+id/tv"
        android:textColor="#ffffff">
    

(4)测试结果图:


3.自定义的Adapter——BaseAdapter的使用:

   如果系统提供的适配器不能满足开发人员的要求,我们也可以通过BaseAdapter来自定义满足需要的适配器,这里为了说明情况我们就用BaseAdapter自定义的适配器(下面我们叫MyAdapter)来完成和上面相同的功能。

(1)MyAdapter.java(新建的类,实现自定义的适配器)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package myAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.arrayadapter.R;
public class MyAdapter extends BaseAdapter{
    private Context context;
    private String data[];
    private int image[];
    public MyAdapter(Context context,String[] data,int[] image){
        this.context=context;
        this.data=data;
        this.image=image;
    }
    //重写一下四个方法,并修改返回值
    @Override
    public int getCount() {
        return this.data.length;
    }
    @Override
    public Object getItem(int position) {
        return data[position];
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    //这个重写的方法是最重要的,用于加载视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater lif=(LayoutInflater)
                context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout=(LinearLayout)
                lif.inflate(R.layout.cell, null);
        TextView tv=(TextView)layout.findViewById(R.id.tv);
        ImageView iv=(ImageView)layout.findViewById(R.id.iv);
        tv.setText(data[position]);
        iv.setBackgroundResource(image[position]);
                                                    
        return layout;
    }
}

(2)MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.example.arrayadapter;
import myAdapter.MyAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
    //声明视图ListView控件
    private ListView lv;
    //声明适配器(控制器)MyAdapter
    private MyAdapter mAdapter;
    //声明并定义数据源
    private String datas[]={
            "Wireless & networks","Call settings","Sound","Display","Location & security",
            "Applications","Accounts & sync","Privacy","CD card & phone storage","Search",
            "Language & keyboard","Voice input & output","Accessibility","Date & Time","About phone"};
    private int image[]={
            R.drawable.ic_settings_wireless,R.drawable.ic_settings_call,R.drawable.ic_settings_sound,
            R.drawable.ic_settings_display,R.drawable.ic_settings_location,R.drawable.ic_settings_applications,
            R.drawable.ic_settings_sync,R.drawable.ic_bt_config,R.drawable.ic_settings_sim,
            R.drawable.ic_power_system,R.drawable.ic_settings_language,R.drawable.ic_settings_voice_calls,
            R.drawable.ic_settings_accessibility,R.drawable.ic_settings_date_time,R.drawable.ic_settings_about};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView)findViewById(R.id.listView1);
        //实现MyAdapter适配器
        mAdapter=new MyAdapter(this,datas,image);
        //通过Adapter的setAdapter方法绑定数据于视图
        lv.setAdapter(mAdapter);
    }
                                   
}

(3)运行效果:

(4)最后补充一个问题——ListView的缓冲

   让我们来看一下MyAdapter.java代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).
                    inflate(R.layout.cell,null);
            holder = new ViewHolder();
            holder.tv = (TextView) convertView.findViewById(R.id.tv);
            holder.iv = (ImageView) convertView.findViewById(R.id.iv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv.setText(data[position]);
        holder.iv.setBackgroundResource(image[position]);
        return convertView;
    }
    private static class ViewHolder {
        TextView tv;
        ImageView iv;
    }
阅读(1987) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~