package com.mcivicm.editablespinner; import android.content.Context; import android.support.annotation.NonNull; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import java.util.List; /** * 可编辑下拉框适配器 */ public abstract class EditableSpinnerAdapter extends BaseAdapter { private List list; private Context context; private int itemLayout; public EditableSpinnerAdapter(Context context, List list, int itemLayout) { this.context = context; this.list = list; this.itemLayout = itemLayout; } @Override public int getCount() { return list.size(); } @Override public T getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = createItemLayout(parent); } this.bindView(position, convertView, parent, getItem(position)); return convertView; } /** * 获取视图 * * @param position * @param convertView * @param parent * @param t * @return */ public abstract View bindView(int position, @NonNull View convertView, ViewGroup parent, T t); /** * 获取显示在输入框的字符串 * * @param position * @return */ public abstract String getDisplayString(int position); public Object getViewHolder(View view) { return view; } private View createItemLayout(ViewGroup parent) { View v = LayoutInflater.from(this.context).inflate(itemLayout, parent, false); v.setTag(getViewHolder(v)); return v; } }