Android界面开发-Adapter

普通的Android编程需要使用到工程目录下的res/layout目录,需要在这个目录下新建一个layout类型的xml文件,之后我们编辑这个xml,将各种控件的定义及设定信息写入文件,在Activity的onCreate中使用setContentView(R.lauout.xxx)来展示界面,这里的xxx就是你编写的xml。是不是感觉so easy?那么我们接着看。


如果你需要编写的是游戏程序,那么一般情况下可能你需要使用一个自定义的 view,你只要将setContentView的参数换成这个view的实例即可。在这个view的onDraw中完成绘制操作,应用需要通知这个view刷新界面的时候使用 invalidate或者postinvalidate完成。

如果你需要系统帮你实现缓冲机制,那么有一个实现了双缓冲的类SurfaceView可供你使用。

好吧,绘制的操作我们今后单独开章来将,我们今天讲一下怎么使用系统定义好的控件。



普通的TextView、EditText、Button等等这些控件我们不讲了,主要原因是太简单了。我们今天讲Adapter:

Adapter叫适配器,对于一个可以作为Container的Component来说,我们有时无法确定他内部所包含的Compoent个数和展现形式,我们仅能确定他得框架展现形式,所以需要一个Adapter。举例来说,对于一个ListView,我们仅能确定他的每一项肯定是一个一个的ListItem,但我们无法确定ListItem内部的内容如何展现,比如这个Item的宽高,有几个图片控件,几个文本控件。。。


我们使用Adapter来帮我们实现这一切。

  1. getCount帮我们确定了Item的总数;
  2. getItem确定取得每一项Item的方法;
  3. getItemId确定我们每一个Item的Id,不太用到,一般都是position;
  4. getView帮我们确定每一个Item的展现内容;

ok,有了这些,我们每一个item都知道怎么展现,又由于count这些也有了,我们知道如何将Item组合成一个大的Component,语言的设计之美就在这里了。。。


一个简单的例子如下:



PersonGridViewAdapter.java

package com.freesoft.uidemo.uicomponent;

import java.io.IOException;
import java.io.InputStream;

import com.freesoft.uidemo.R;
import com.freesoft.uidemo.entity.Person;
import com.freesoft.uidemo.entity.PersonList;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PersonGridViewAdapter extends BaseAdapter {
	private Context context;
	private LayoutInflater inflater;

	public PersonGridViewAdapter(Context context) {
		super();
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	public int getCount() {
		return PersonList.getPersons().size();
	}

	public Object getItem(int position) {
		return PersonList.getPersons().get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = inflater.inflate(R.layout.gridview, null);

		ImageView img = (ImageView) convertView
				.findViewById(R.id.gv_gv_personpic);
		TextView txt = (TextView) convertView
				.findViewById(R.id.tv_gv_personname);

		Person person = PersonList.getPersons().get(position);

		AssetManager am = context.getAssets();
		InputStream is;
		try {
			is = am.open(person.getPersonIconUri().toString());
			img.setImageDrawable(Drawable.createFromStream(is, null));
			txt.setText(person.getPersonName());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return convertView;
	}

}


Person.java

package com.freesoft.uidemo.entity;

import android.net.Uri;

public class Person {
	private String personName;
	private Uri personIconUri;
	public Person(String personName, String iconFilename) {
		super();
		this.setPersonName(personName);
		this.setPersonIconUri(Uri.parse(iconFilename));
	}
	
	private void setPersonName(String personName) {
		this.personName = personName;
	}
	private void setPersonIconUri(Uri personIconUri) {
		this.personIconUri = personIconUri;
	}
	
	public String getPersonName() {
		return personName;
	}
	public Uri getPersonIconUri() {
		return personIconUri;
	}
}


PersonList.java

package com.freesoft.uidemo.entity;

import java.util.ArrayList;

public class PersonList {
	private static ArrayList<Person> persons = null;
	
	public static ArrayList<Person> getPersons() {
		persons = new ArrayList<Person>();
		initPersons();		
		return persons;
	}
	private static void initPersons() {
		Person person = null;
		
		person = new Person("张三", "QQtxtb/QQtxtb 00.png");
		persons.add(person);
		
		person = new Person("李四", "QQtxtb/QQtxtb 01.png");
		persons.add(person);
		
		person = new Person("王五", "QQtxtb/QQtxtb 02.png");
		persons.add(person);
	}
}


简单的总结一下,发现使用了Adapter的控件还真不少,例如:ListView、GridView、AutoCompleteTextView、Gallery、Spinner等。。。。




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值