Adapter 二次封装 一句话实现适配
1 首先 在listview gridview 编写Adatpter时候 每次都自己去写Adapter
public void setListAdapter(List<NewCarPriceInfo> info_) { list_.setAdapter(new AbstracrHolderAdapter<NewCarPriceInfo>( act, info_, R.layout.newcar_price_layout) { @Override public void bindViewAndData(AbstracrHolderAdapter.ViewHolder holder, int position, NewCarPriceInfo item) { ((TextView)holder.getView(R.id.tv_shop_name)).setText( item.getDealer_name()); ((TextView)holder.getView(R.id.new_price)).setText( item.getPrice()+"万"); ((TextView)holder.getView(R.id.tv_address)).setText( item.getDealer_address()); } }); }
现在可以只要一句话就能实现适配
2 接来我们看看 AbstracrHolderAdapter 这个类
package com.che300.price.adapter; import android.content.Context; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import com.sunshine.adapterlibrary.adapter.ViewHolder; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2016/7/15. */ public abstract class AbstracrHolderAdapter<T> extends BaseAdapter { private Context mContext; private List<T> list; protected LayoutInflater mInflater; private int mItemLayoutId; public AbstracrHolderAdapter(Context context, List<T> list, int itemLayoutId) { this.mContext = context; this.mInflater = LayoutInflater.from(mContext); this.mItemLayoutId = itemLayoutId; this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } public List<T> getList() { // TODO Auto-generated method stub return this.list; } public void setList(List<T> list) { // TODO Auto-generated method stub this.list = list; notifyDataSetChanged(); } @SuppressWarnings("unchecked") public void addList(List<T> list2) { // TODO Auto-generated method stub this.list.addAll(list2); notifyDataSetChanged(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return this.list.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) { final ViewHolder viewHolder = ViewHolder.get(mContext,convertView,mItemLayoutId); bindViewAndData(viewHolder, position,(T) getItem(position)); return viewHolder.getConvertView(); } public abstract void bindViewAndData(ViewHolder holder,int position, T item); public static class ViewHolder { private View mConvertView; private final SparseArray<View> mViews = new SparseArray<View>(); private ViewHolder(Context context, int layoutId) { mConvertView = LayoutInflater.from(context).inflate(layoutId, null, false); mConvertView.setTag(this); } public static ViewHolder get(Context context, View convertView, int layoutId) { if (convertView == null) { return new ViewHolder(context, layoutId); } return (ViewHolder) convertView.getTag(); } public <T extends View> T getView(int viewId) { View view = mViews.get(viewId); if (view == null) { view = mConvertView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } public View getConvertView() { return mConvertView; } } }
这样 你只要把布局写好就ok 了 是不是能节约1/2的时间