我的自定义Adapter

public abstract class AbstractBaseAdapter<T> extends BaseAdapter {

     public LayoutInflater inflater;
     private Context context;
     public List<T> mList;
     protected int mInflaterId;
     BaseViewHolder baseViewHolder;

     public BaseViewHolder getBaseViewHolder() {
          if(this.baseViewHolder==null){
               return null;
          }
          return baseViewHolder;
     }

     public void setBaseViewHolder(BaseViewHolder baseViewHolder) {
          this.baseViewHolder = baseViewHolder;
     }

     public AbstractBaseAdapter(Context context, List<T> mList, int mInflaterId) {

          this.context = context;
          this.mList = mList;
          this.mInflaterId = mInflaterId;
          inflater =LayoutInflater.from(context);
     }

     public AbstractBaseAdapter(Context context, List<T> mList, int mInflaterId,
               BaseViewHolder baseViewHolder) {

          this.context = context;
          this.mList = mList;
          this.mInflaterId = mInflaterId;
          this.baseViewHolder = baseViewHolder;
     }

     @Override
     public int getCount() {
          // TODO Auto-generated method stub
          return mList == null ? 0 : mList.size();
     }

     @Override
     public T getItem(int position) {
          // TODO Auto-generated method stub
          return mList == null ? null : mList.get(position);
     }

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

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
          // TODO Auto-generated method stub
          BaseViewHolder holder = null;
          View view = convertView;
          if (view == null) {
               holder = baseViewHolder.getInstance();
               view = inflater.inflate(mInflaterId, null);
               holder.bindView(view);
               view.setTag(holder);
          } else {
               holder = (BaseViewHolder) view.getTag();
          }
          holder.setData(position);
          return view;
     }

     interface BaseViewHolder {
          /**
          * 得到Viewholder的实例
          *
          * @return
          * @modifiedTime 下午4:14:47
          * @author lzt
          */
          public BaseViewHolder getInstance();

          /**
          * 绑定View
          *
          * @param v
          * @modifiedTime 下午4:15:00
          * @author lzt
          */
          public void bindView(View v);

          /**
          * 设置数据
          *
          * @param pos
          * @modifiedTime 下午4:15:05
          * @author lzt
          */
          public void setData(int pos);

     }
}
package com.example.mylistview.adapter;

import java.util.List;

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

import com.example.mylistview.R;
import com.example.mylistview.ViewModel;

public class MyBaseAdapter extends AbstractBaseAdapter {

     public MyBaseAdapter(Context context, List mList, int layoutId) {
          super(context, mList, layoutId);
          super.setBaseViewHolder(new ViewHolder());
     }

     class ViewHolder implements BaseViewHolder {

          public TextView textView;
          public CheckBox checkBox;
          ViewHolder instance = null;

          @Override
          public BaseViewHolder getInstance() {
               // TODO Auto-generated method stub
               if (instance == null) {
                    synchronized (ViewHolder.class) {
                         if (instance == null) {
                              instance = new ViewHolder();
                         }
                    }
               }
               return instance;
          }

          @Override
          public void bindView(View v) {
               // TODO Auto-generated method stub
               textView = (TextView) v.findViewById(R.id.textView1);
               checkBox = (CheckBox) v.findViewById(R.id.checkBox1);
          }

          @Override
          public void setData(int pos) {
               textView.setText(((ViewModel) mList.get(pos)).getText());
               checkBox.setChecked(((ViewModel) mList.get(pos)).isCheck());
          }
     }

}
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值