您的位置:首页 > 移动开发 > Android开发

Android之ListView进阶

2015-07-25 01:12 323 查看
想必大家都知道MVC设计模式吧,其实ListView就是依据这种设计模式来进行视图与数据的交互的。

首先View视图层不用说都知道在 *.xml中定义的ListView控件,定义完控件以后,我们根据编程需要,要不断地去更新整个View层,此时谁来进行这个层面的控制呢,是由控制层(也就是Control层)来进行界面的更新的,此时的控制层也就是ListView的适配器。当然,如果要去更新整个View层,就必须要有相应的数据供我们去更新,这个数据就是我们的数据源,数据源通常需要我们自己来定义,主要是用来匹配ListView中每个Item的数据。到此,这样也就完成了整个ListView的控件流程。

简言之,整个流程如下:

1.在 *.xml布局中定义ListView控件;

2.获取控件对象并为控件对象设置相应的适配器,达到视图层与

数据的映射;

3.为适配器配置相应的数据(我就称数据源吧);

好了,了解了ListView的原理,我们就主要说说ListView适配器的类型吧!

ListView的适配器有以下几种:

1.ArrayAdapter(Context context, int textViewResourceId, List objects)

这个是个简单的适配器,要想装配这个适配器,我们需要一个视图与数组来进行配合使用,该适配器中的三个参数要知道,第一个是this,第二个是布局,第三个是对应的数据源,通常是一个数组或者是个容器(根据自己需要来设定)。

2.SimpleCursorAdapter

从字面上来讲都能看出来,它是用来将游标上的数据映射到View层的,是个映射关系,里面的参数与ArrayAdapter一样。主要用于获取数据库的cursor,将数据库中的数据导出。

3.SimpleAdapter

这个适配器扩展性很好,也是我们最常用的一个,可以扩展自定义的布局,例如在Item中加载图片(ImageView)、按钮(Button)、文字(TextView)等,都是可以自定义的。这里要注意的是该适配器的数据源我们要用到一个HashMap来将我们自定义的控件内容封装在一起组成一个Item的数据源。SimpleAdapter的四个参数要注意下,分别为:

this,容器(数据源), 布局文件(自定义),

HashMap的title(数据源的每条Item中的内容),

将要适配的布局文件的控件的id

4.BaseAdapter

该适配器实现了SpinnerAdapter与ListAdapter的接口,所以顾名思义,他能够为这些控件来适配数据。实际上诸如Spinner、ListView、GridView都有自己的适配器,这个适配器优点就在于都能够适配以上的控件。

以上是我列出来常用的适配器,但对常用的应该还是SimpleAdapter与BaseAdapter。接下来就看看SimpleAdapter的使用。

废话不多说,我们在布局文件中自定义一个ListView,如下:

文件 activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

</ListView>

</RelativeLayout>


然后定义完以后需要注意的是我们此时用的是自定义的item,所以我们自己写了一个布局,如下:

文件 item_layout.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="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/myPhoto"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="@+id/name"
android:layout_toRightOf="@+id/myPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="30dp"
android:layout_marginLeft="120dp"
android:text="50dp"
android:textSize="30dp"
/>

</RelativeLayout>

</LinearLayout>


最后要为这个ListView设置对应的SimpleAdapter适配器了,代码如下:

文件 MainActivity.java

package chat.sunnyboy.lee.listviewdemo;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

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

public class MainActivity extends ActionBarActivity {

private ListView myListView;
private HashMap<String,Object> hashMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
//参数的含义:this,数据源,布局(自定义),HashMap的参数,自定义布局控件的id
myListView = (ListView)findViewById(R.id.myListView);
myListView.setAdapter(new SimpleAdapter(this,getData(),R.layout.item_layout,
new String[]{"name","photo"},new int[]{R.id.name,R.id.myPhoto}));

}

public List<HashMap<String,Object>> getData(){

List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();

//逐个添加数据
hashMap = new HashMap<String,Object>();
hashMap.put("name","小李");
hashMap.put("photo",R.drawable.a);
data.add(hashMap);

hashMap = new HashMap<String,Object>();
hashMap.put("name","小张");
hashMap.put("photo",R.drawable.a);
data.add(hashMap);

hashMap = new HashMap<String,Object>();
hashMap.put("name","小赵");
hashMap.put("photo",R.drawable.a);
data.add(hashMap);

hashMap = new HashMap<String,Object>();
hashMap.put("name","小林");
hashMap.put("photo",R.drawable.a);
data.add(hashMap);

return data;

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


运行一下,结果如下:



以上是SimpleAdapter适配器的使用,紧接着我们将看一下BaseAdapter的使用,时间限制,BaseAdapter适配器下次再说吧!今天就先说到这里!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: