实现多效果ListView

某些时候我们需要实现在一个列表中有不同的类型的行,也就是不同项需要有不同的效果,为了实现这个效果,主要在适配器中进行操作例如其中的getItemViewType ,getViewTypeCount

实现

首先我们要记住虽然没项表现不同但是需要有一个基本的modle,因此第一布就是创造一个足够灵活的modle来支持各种类型,以及内置一个机制来确定各种类型,下面是一个不同颜色的例子

  1. 创建一个SimpleColor 的类,需要包含显示的颜色和内容的属性
public class SimpleColor {
    public enum ColorValues {RED, BLUE, GREEN}//枚举
    public SimpleColor(String label, ColorValues color) {
        super();
        this.label = label;
        this.color = color;
    }
    public String label;
    public ColorValues color;
}

我们利用枚举类限定了每项的颜色,用label表示每项的标签,然后我们需要一个适配器

public class ColorArrayAdapter extends ArrayAdapter<SimpleColor> {
    public ColorArrayAdapter(Context context, ArrayList<SimpleColor> objects) {
        super(context, 0, objects);
    }
    //返回view的类型数量,创造者是getView(int, View, ViewGroup)
    @Override
    public int getViewTypeCount() {
        //每种类型都代表了可以被转换view的集合
        //values()方法反悔了一个数组
        return SimpleColor.ColorValues.values().length;
    }
    //得到将会由getView创造的view的种类

    @Override
    public int getItemViewType(int position) {
        //返回代表vie种类的整数,注意:范围是0~getViewTypeCount()-1
        //通过获取枚举类型的序列返回代表类型的整数
        return getItem(position).color.ordinal();
    }
    //得到在指定位置展示指定数据的view

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //view应该是基于getItemViewType的类型创造的,并且convertView要保证回收再利用的类型
        //是正确的
        SimpleColor color = getItem(position);
        if (convertView == null) {
            //得到数据项的类型
            int type = getItemViewType(position);
            //基于类型加载XML文件
            convertView = getInflatedLayoutForType(type);
        }
        //加载控件
        TextView tvLabel = (TextView) convertView.findViewById(R.id.tvLable);
        if (tvLabel != null) {
            tvLabel.setText(color.label);
        }
        return convertView;

    }
    //根据项的类型,返回相应的布局文件
    private View getInflatedLayoutForType(int type) {
        if (type == SimpleColor.ColorValues.BLUE.ordinal()) {
            return LayoutInflater.from(getContext()).inflate(R.layout.item_blue_color, null);
        } else if (type == SimpleColor.ColorValues.RED.ordinal()) {
            return LayoutInflater.from(getContext()).inflate(R.layout.item_blue_color, null);

        } else if (type == SimpleColor.ColorValues.GREEN.ordinal()) {
            return LayoutInflater.from(getContext()).inflate(R.layout.item_green_color, null);

        } else {
            return null;
        }
    }
}

一旦viewType确定了,converyView 对象就会保证是相对应的布局,在主活动中

public class MainActivity extends AppCompatActivity {
    private ListView lvColors;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lvColors = (ListView) findViewById(R.id.lvColors);
        ArrayList<SimpleColor> aColors = new ArrayList<>();
        //像数组中存入数据
        aColors.add(new SimpleColor("Blue", SimpleColor.ColorValues.BLUE));
        aColors.add(new SimpleColor("Green", SimpleColor.ColorValues.GREEN));
        //绑定适配器
        ColorArrayAdapter adapter = new ColorArrayAdapter(this,aColors);
        lvColors.setAdapter(adapter);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值