Adapter 超简洁写法

EasyListView

每次用到 ListView 时候都需要写一个Adapter;令人恶心的是需要在getView() 方法中添加一堆代码,无论是维护还是从头开始写都非常不便。该项目能够极大的简化代码,使得代码更加模块化,且只需要三步就可以实现一个 Adapter。
GitHub : https://github.com/jqzhang/EasyListView

实现思路:

  1. 根据不同的 viewType 创建 View
  2. 绑定(项目中用Buttetknife 实现)
  3. 更新View

主要代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) { 
  ViewHolder vh;   
  // 如果view为null 或者 类型不符则重新创建
  if (null == convertView || ((convertView.getTag() != null) && (((ViewHolder) convertView.getTag()).getType() != getItemViewType(position))))    {      
    vh = onCreateViewHolder(parent, position);          
    vh.setType(getItemViewType(position));   
  } else {      
    vh = (ViewHolder) convertView.getTag();   
  }   
  if (null == vh) {      
    return null;   
  }   
  vh.updateView(vh.getConvertView(), getItem(position), position);   
  return vh.getConvertView();
}

项目地址

注意:项目中用到 butterknife

单一样式

  1. 实现 Adapter

     class SingleTypeAdapter extends EasyBaseAdapter {
         List list = new ArrayList<>();
    
         @Override
         protected List getList() {
             if (list.size() > 0) {
                 return list;
             }
             // 构造数据
             for (int i = 0; i < 60; i++) {
                 SingleTypeActivity.Result result = new SingleTypeActivity.Result();
                 list.add(result);
             }
             return list;
         }
    
         // 构造 Item ViewHolder
         @Override
         protected EasyBaseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
             // 创建 ItemView
             View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_1, null);
             return new SingleTypeViewHolder(view);
         }
     }
    
  2. 实现ViewHolder
    class SingleTypeViewHolder extends EasyBaseAdapter.ViewHolder {

         @Bind(R.id.textView)
         TextView textView;
    
         public SingleTypeViewHolder(View view) {
             super(view);
         }
    
         @Override
         protected void updateView(View convertView, Result data, int position) {
             // 更新View 
             textView.setText(data.txt + position + "  " + (null == convertView));
         }
     }
    
  3. 完成
    listView.setAdapter(new SingleTypeAdapter());

多样式

  1. 实现Adapter
    class MutiTypeAdapter extends EasyBaseAdapter {

         // 一定要实现该方法
         @Override
         public int getViewTypeCount() {
             return 2;
         }
    
         // 一定要实现该方法
         @Override
         public int getItemViewType(int position) {
             if (0 == position % 2) {
                 return 0;
             } else {
                 return 1;
             }
         }
    
         List list = new ArrayList<>();
    
         @Override
         protected List getList() {
    
             if (list.size() > 0) {
                 return list;
             }
             // 构造数据
             for (int i = 0; i < 60; i++) {
                 MutiTypeActivity.Result result = new MutiTypeActivity.Result();
                 list.add(result);
             }
             return list;
         }
    
         // 构造 Item ViewHolder
         @Override
         protected EasyBaseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
             // 根据 ViewType 创建 ItemView
             if (0 == getItemViewType(position)) {
                 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_1, null);
                 return new MutiTypeViewHolder1(view);
             } else {
                 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_2, null);
                 return new MutiTypeViewHolder2(view);
             }
         }
     }
    
  2. 实现ViewHolder
    class MutiTypeViewHolder1 extends EasyBaseAdapter.ViewHolder {

         @Bind(R.id.textView)
         TextView textView;
    
         public MutiTypeViewHolder1(View view) {
             super(view);
         }
    
         @Override
         protected void updateView(View convertView, Result data, int position) {
             // 更新View 
             textView.setText("刺激 + " + position + "   " + (null == convertView));
         }
     }
    
     class MutiTypeViewHolder2 extends EasyBaseAdapter.ViewHolder {
    
         @Bind(R.id.imageView)
         ImageView imageView;
    
         public MutiTypeViewHolder2(View view) {
             super(view);
         }
    
         @Override
         protected void updateView(View convertView, Result data, int position) {
             // 更新View 
             imageView.setImageResource(R.mipmap.rr);
           }
     }
    
  3. 完成
    listView.setAdapter(new SingleTypeAdapter());

注意:多样式ListView 的 Adapter 中一定要实现 getViewTypeCount() 和 getItemViewType()这两个方法。其余照搬就好。

你可能感兴趣的:(Adapter 超简洁写法)