Android打造ListView万能适配器

打造ListView万能适配器

看到这里,想必你已经意识到每次新创建一个适配器需要重复写的代码实在太多,太浪费时间了。因此打造一个万能的listview适配器就显得重要了。在这里只要按照下面步骤创建好适配器类,那么当我们需要使用新的listview的时候,真正要写的代码就单单相当于你的item数据项条目了(这是每个适配器不同的地方),其他重复的代码无需再写了。是不是已经准好了,当然如果你想真正理解万能适配器的原理,前提还是假设你知道了对listview的两个优化点


步骤:

(以下如有不理解的地方可以留言,有写的不好或错误的地方希望能谅解并留言提示,谢谢)

1:创建好布局文件和Listview的data类

listview_common_item.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"
    android:orientation="horizontal" >
    <ImageView android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
	    android:layout_marginLeft="10dp"/>
	<TextView android:id="@+id/tv"
	    android:layout_width="match_parent"
	    android:layout_height="40dp"
	    android:layout_marginLeft="10dp"
	    android:gravity="center_vertical"/>
</LinearLayout>

Data.class
public class Data {
	private int id;
	private String content;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Data(int id, String content) {
		super();
		this.id = id;
		this.content = content;
	}
	
}


2:将CommonHolder、CommonAdapter两个类复制到你的项目包内

CommonAdapter.class


import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class CommonAdapter<T> extends BaseAdapter {
	private Context context;
	private List<T> datas;
	private int layoutId;

	public CommonAdapter(Context context, List<T> datas, int layoutId) {
		super();
		this.context = context;
		this.datas = datas;
		this.layoutId = layoutId;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MyViewHolder holder = MyViewHolder.get(context, position, convertView,
				parent, layoutId);
		curremt(holder, getItem(position));
		return holder.getmConvertView();
	}

	public abstract void curremt(MyViewHolder holder, Object object);
}

CommonHolder.class

SparseArray<View> viewArray :用于存储listview Item的组件,类似于Map容器,不过在这里相对于Map效率更高

package com.qiu.common.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CommonHolder {
	private View mConvertView;
	private SparseArray<View> viewArray = new SparseArray<View>();

	public CommonHolder(int layoutId, LayoutInflater mInflater,
			ViewGroup parent, View mConvertView) {
		super();
		mConvertView = mInflater.inflate(layoutId, parent, false);
		mConvertView.setTag(this);
		this.mConvertView = mConvertView;
	}

	public static CommonHolder getHolder(int layoutId,
			LayoutInflater mInflater, ViewGroup parent, View mConvertView) {
		if (mConvertView == null) {
			return new CommonHolder(layoutId, mInflater, parent, mConvertView);
		} else {
			return (CommonHolder) mConvertView.getTag();
		}
	}

	public View getView(int viewId) {
		View view = viewArray.get(viewId);
		if (view == null) {
			view = mConvertView.findViewById(viewId);
			viewArray.append(viewId, view);
		}
		return view;
	}

	public View getConvertView() {
		// TODO Auto-generated method stub
		return mConvertView;
	}

}

3:创建一个自己的Test1Adapter类继承CommonAdapter,

重写getAndSetData()方法 --》也就是你给你的每个listview item赋值 

package com.qiu.common.adapter;

import java.util.List;

import com.example.myadapter.R;

import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

public class Test1Adapter extends CommonAdapter<Data>{

	public Test1Adapter(int mlayout, List<Data> datas, Context mContext) {
		super(mlayout, datas, mContext);
		// TODO Auto-generated constructor stub
	}

	@Override
	void getAndSetData(CommonHolder mHolder, Object object) {
		// TODO Auto-generated method stub
		Data data = (Data)object;
		((ImageView)mHolder.getView(R.id.iv)).setImageResource(data.getId());
		((TextView)mHolder.getView(R.id.tv)).setText(data.getContent());
	}

}


5:获取自己主页面的listview,并调用mListview.setAdapter()方法。

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"
    tools:context="${relativePackage}.${activityClass}" >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mListview" />

</RelativeLayout>

MainActivity.class
package com.example.myadapter;

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

import com.qiu.common.adapter.Data;
import com.qiu.common.adapter.Test1Adapter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {
	private ListView mListview;
	private List<Data> mDatas;
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initData();// 初始化数据
		initView();// 初始化空间
	}

	private void initData() {
		mDatas = new ArrayList<Data>();
		for (int i = 0; i < 10; i++) {
			Data data = new Data(R.drawable.ic_launcher,""+i);
			mDatas.add(data);
		}
	}

	private void initView() {
		// TODO Auto-generated method stub
		mListview = (ListView) findViewById(R.id.mListview);
		mListview.setAdapter(new Test1Adapter(R.layout.listview_common_item, mDatas, MainActivity.this));
	}
}

有没有发现:相比于之前的方式,这次我们只需要我们改写的代码只有两行 (setAndGetdata()方法里面)!! )

==》运行测试


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值