Android万能的Adapter

Posted on Posted in Android

啊哈,今天来分享一个万能AndroidAdapter,想提高工作效率,和懒的程序猿同胞们有福了,嘻嘻。
代码很简单就不做解释了 直接上代码
先来个CommonAdapter继承BaseAdapter


package org.shuaibi.androidexplore.utils;

import java.util.List;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

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

/**
*
* @描述: CommonAdapter
* @作者: ZeroX
* @时间: 2015-8-30
* @参数:
* @接口:
*
*/
public abstract class CommonAdapter<T> extends BaseAdapter {

protected List<T> mList;
protected Context mContext;
protected RequestQueue mQueue;
protected ImageLoader mImageLoader;

public CommonAdapter(){

}
public CommonAdapter(List<T> list,Context context)
{
this.mList=list;
this.mContext=context;
this.mQueue = Volley.newRequestQueue(context);
this.mImageLoader = new ImageLoader(mQueue, new BitmapCache());
}

@Override
public int getCount() {

return mList.size();
}

@Override
public Object getItem(int position) {
return mList.get(position);
}

@Override
public long getItemId(int position) {

return position;
}

@Override
public abstract View getView(int position, View convertView, ViewGroup parent);

}

&nbsp;

&nbsp;

 

然后是ViewHolder 这是重点

 


package org.shuaibi.androidexplore.utils;

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

/**
*
* @描述: ViewHolder
* @作者: ZeroX
* @时间: 2015-8-30
* @参数:
* @接口:
*
*/
public class ViewHolder {

private SparseArray<View> mView;
private View mConvertView;
public int mPosition;

public ViewHolder(Context context,ViewGroup parent,int position,int layoutId)
{
mView=new SparseArray<View>();
mPosition=position;
mConvertView=LayoutInflater.from(context).inflate(layoutId, parent,false);
mConvertView.setTag(this);
}

public    ViewHolder ()
{

}
public static ViewHolder get(Context context,View convertView,ViewGroup parent,int position,int layoutId)
{
if(convertView==null)
{
return new ViewHolder(context, parent, position, layoutId);
}else {
ViewHolder viewHolder=(ViewHolder) convertView.getTag();
viewHolder.mPosition=position;
return viewHolder;
}

}

@SuppressWarnings("unchecked")
public<T extends View> T getView(int viewId)
{
View view=mView.get(viewId);
if(view==null)
{
view=mConvertView.findViewById(viewId);
mView.put(viewId, view);
}
return (T) view;

}

public View getConvertView()
{
return mConvertView;
}

}

下面是使用方法


package org.shuaibi.androidexplore.adapter;

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

import org.shuaibi.androidexplore.R;
import org.shuaibi.androidexplore.config.Config;
import org.shuaibi.androidexplore.utils.CommonAdapter;
import org.shuaibi.androidexplore.utils.ViewHolder;
import com.android.volley.toolbox.NetworkImageView;

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

/**
*
* @描述: 产品列表适配器
* @作者: ZeroX
* @时间: 2015-8-30
* @参数:
* @接口:
*
*/
public class ProductListAdapter extends CommonAdapter<HashMap<String, String>> {

public ProductListAdapter(List<HashMap<String, String>> list,Context context)
{
super(list,context);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder=ViewHolder.get(mContext, convertView, parent, position,R.layout.item_main);

HashMap<String, String> dataHashMap=(HashMap<String, String>) mList.get(position);
((TextView)holder.getView(R.id.main_tv_productName)).setText(dataHashMap.get("ProductName"));
((NetworkImageView)holder.getView(R.id.main_iv_productimg)).setImageUrl(Config.FILE_ADDRESS+dataHashMap.get("ImageUrl"),mImageLoader);

return holder.getConvertView();
}

}

看是不是比以前少写了很多代码。有不明白的地方 可以去这里看视频

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注