android自定义ListView详解
在这里我就对自定义ListView进行讲解下吧,直入真题吧,首先我新创建一个项目:listDemo;
其次,我们需要建立一个XML文件,在这里我就直接写在main.xml里:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
然后我们需要定义一个ITEM.XML来显示其数据:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/images_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="default_001"
/>
<TextView
android:id="@+id/text_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="default_info"
/>
<TextView
android:id="@+id/text_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="default_url"
/>
</LinearLayout>
</LinearLayout>
现在我们把XML文件做好了,就是对其适配数据的布局进行定义了,首先我们需要创建一个类:
package com.jsd;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;
import com.jsd.adapter.AppAdapter;
import com.jsd.model.AppInfos;
/**
* ListView customer
* @author jiangshide
*
*/
public class Demo extends Activity {
private Context mContext;
private ListView listView;
private ArrayList<AppInfos> appInfos;
private AppAdapter appAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getComponenets();
}
/**
* componenet
*/
public void getComponenets(){
this.mContext = this;
listView = (ListView) findViewById(R.id.listView);
appInfos = new ArrayList<AppInfos>();
loadDatas();
appAdapter = new AppAdapter(mContext,appInfos);
listView.setAdapter(appAdapter);
}
/**
* load datas
*/
public void loadDatas(){
for (int i = 0; i < 10; i++) {
AppInfos ai = new AppInfos();
if(i == 1){
ai.setImages(R.drawable.icon);
}else if(i == 2){
ai.setImages(R.drawable.item1);
}else if(i == 3){
ai.setImages(R.drawable.item2);
}else if(i == 4){
ai.setImages(R.drawable.item3);
}else if(i == 5){
ai.setImages(R.drawable.item4);
}else if(i == 6){
ai.setImages(R.drawable.item5);
}
ai.setText_id("001");
ai.setText_infos("这是我们都需要注意的...");
ai.setText_url("http://jiangshide.com");
appInfos.add(ai);
}
}
}
这位主线程的UI类,
其实需要穿件一个适配器需要继承BaseAdapter:
package com.jsd.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.jsd.model.AppInfos;
import com.jsd.view.AppView;
/**
* Adapter
* @author jiangshide
*
*/
public class AppAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<AppInfos> appInfos;
public AppAdapter(Context c) {
this.mContext = c;
}
public AppAdapter(Context c,ArrayList<AppInfos> appInfos) {
this.mContext = c;
this.appInfos = appInfos;
}
public int getCount() {
return appInfos.size();
}
public Object getItem(int position) {
return appInfos.get(position);
}
public long getItemId(int position) {
return position;
}
/**
* get view
*/
public View getView(int position, View convertView, ViewGroup parent) {
if(appInfos == null){
return null;
}
if(convertView == null){
AppView appView = new AppView(mContext);
appView.updateView(appInfos.get(position));
convertView = appView;
}else{
((AppView)convertView).updateView(appInfos.get(position));
}
return convertView;
}
}
为了对其数据进行适配,我们需要对数据进行持久话:使用BEAN的方式进行保存
package com.jsd.model;
/**
* datas
* @author jiangshide
*
*/
public class AppInfos {
private int images;
private String text_id;
private String text_infos;
private String text_url;
public int getImages() {
return images;
}
public void setImages(int images) {
this.images = images;
}
public String getText_id() {
return text_id;
}
public void setText_id(String textId) {
text_id = textId;
}
public String getText_infos() {
return text_infos;
}
public void setText_infos(String textInfos) {
text_infos = textInfos;
}
public String getText_url() {
return text_url;
}
public void setText_url(String textUrl) {
text_url = textUrl;
}
}
再次就是对View显示布局的控件进行定义来对数据参数的获取
package com.jsd.view;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.jsd.R;
import com.jsd.model.AppInfos;
/**
* View
* @author jiangshide
*
*/
public class AppView extends LinearLayout {
private Context mContext;
private ImageView image_id;
private TextView text_id,text_info,text_url;
/**
*
* @param context
*/
public AppView(Context context) {
super(context);
this.mContext = context;
initilize(context);
}
/**
*
* @param c
*/
public void initilize(Context c){
this.mContext = c;
View view = LayoutInflater.from(this.mContext).inflate(R.layout.item, null);
image_id = (ImageView) view.findViewById(R.id.images_id);
text_id = (TextView) view.findViewById(R.id.text_id);
text_info = (TextView) view.findViewById(R.id.text_info);
text_url = (TextView) view.findViewById(R.id.text_url);
addView(view);
}
/**
*
* @param ai
*/
public void updateView(AppInfos ai){
image_id.setImageResource(ai.getImages());
text_id.setText(ai.getText_id());
text_info.setText(ai.getText_infos());
text_url.setText(ai.getText_url());
}
}
OK,以上就是完整的代码实现对ListView的自定义的实现,
下个小结我开始讲GridView的自定义显示方式,同样会以完整的代码显示方式来实现。。。。。。