list view Adapter getView 实现多Type类型的View Holder

ListView 适配器Adapter getView 优化

场景:展示Sever Get到的数据 数据中要展示的Item数据类型不确定 需要两个以上的展示形式

解决办法

  • 1,实现Base Adapter的 getItemViewType() 方法 getViewTypeCount() 方法
  • 2,在getView() 方法中通过getItemViewType()方法获取当前要展示的Item类型是哪一类
    根据不同的Type实例化不同的ViewHolder contentView.setTag() getTag() 复用当前缓存的
    viewHolder
package com.ming.listviewadapterdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;

/**
 * Created by 明正 on 2017/3/26.
 */

public  abstract class DefaultAdapter<T> extends BaseAdapter {

private static final int TYPE_ONE  = 0;
private static  final int TYPE_TWO = 1;

private ArrayList<T> mList;
private Context mContext;

public DefaultAdapter(ArrayList<T> list, Context context) {
    mList = list;
    mContext = context;
}

/**
 * How many items are in the data set represented by this Adapter.
 *
 * @return Count of items.
 */
@Override
public int getCount() {
    return mList!=null?mList.size():0;
}
/**
 * Get the data item associated with the specified position in the data set.
 *
 * @param position Position of the item whose data we want within the adapter's
 *                 data set.
 * @return The data at the specified position.
 */
@Override
public Object getItem(int position) {
    return mList!=null?mList.get(position):null;
}

/**
 * Get the row id associated with the specified position in the list.
 *
 * @param position The position of the item within the adapter's data set whose row id we want.
 * @return The id of the item at the specified position.
 */
@Override
public long getItemId(int position) {
    return position;
}

/**
 * Get a View that displays the data at the specified position in the data set. You can either
 * create a View manually or inflate it from an XML layout file. When the View is inflated, the
 * parent View (GridView, ListView...) will apply default layout parameters unless you use
 * {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
 * to specify a root view and to prevent attachment to the root.
 *
 * @param position    The position of the item within the adapter's data set of the item whose view
 *                    we want.
 * @param convertView The old view to reuse, if possible. Note: You should check that this view
 *                    is non-null and of an appropriate type before using. If it is not possible to convert
 *                    this view to display the correct data, this method can create a new view.
 *                    Heterogeneous lists can specify their number of view types, so that this View is
 *                    always of the right type (see {@link #getViewTypeCount()} and
 *                    {@link #getItemViewType(int)}).
 * @param parent      The parent that this view will eventually be attached to
 * @return A View corresponding to the data at the specified position.
 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolderOne viewHolderOne = null;
    ViewHolerTwo viewHolerTwo = null;
    if(convertView==null){
        //加载xml布局
        switch (getItemViewType(position)){
            case TYPE_ONE:
                viewHolderOne = new ViewHolderOne();
                convertView.setTag(viewHolderOne);
                break;
            case TYPE_TWO:
                viewHolerTwo = new ViewHolerTwo();
                convertView.setTag(viewHolerTwo);
                break;
        }
    }else switch (getItemViewType(position)) {
        case TYPE_ONE:
            viewHolderOne = (ViewHolderOne) convertView.getTag();
            break;
        case TYPE_TWO:
            viewHolerTwo = (ViewHolerTwo) convertView.getTag();
            break;
    }
    
    return convertView;
}


@Override
public int getItemViewType(int position) {
    if(position!=0){
        return TYPE_TWO;
    }else {
        return TYPE_ONE;
    }
}

@Override
public int getViewTypeCount() {
  return 2;
}


class ViewHolderOne {
    public ViewHolderOne(View contentView) {
       
        initView(contentView);
    }

    private void initView(View view) {
    }
}


class ViewHolerTwo {
    public ViewHolerTwo(View contentView) {
        
        initView(contentView);
    }

    private void initView(View view) {
    }
    
}
    原文作者:MrZang
    原文地址: https://www.jianshu.com/p/a45f12314c4b
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞