列表视图
为实现各种排列组合类的视图(包括但不限于Spinner、ListView、GridView等等),Android提供了五花八门的适配器用于组装某个规格的数据,常见的适配器有:数组适配器ArrayAdapter、简单适配器SimpleAdapter、基本适配器BaseAdapter、翻页适配器PagerAdapter。适配器的种类虽多,却个个都不好用,以数组适配器为例,它与Spinner配合实现下拉框效果,其实现代码纷复繁杂,一直为人所诟病。故而在下拉框一小节之中,干脆把ArrayAdapter连同Spinner一股脑都摒弃了,取而代之的是Kotlin扩展函数selector。
到了列表视图ListView这里,与之搭档的一般是基本适配器BaseAdapter,这个BaseAdapter更不简单,基于它的列表适配器得重写好几个方法,还有那个想让初学者撞墙的ViewHolder。总之,每当要实现类似新闻列表、商品列表之类的页面,一想到这个难缠的BaseAdapter,心里便发怵。譬如下图所示的六大行星的说明列表,左侧是图标,右边为文字说明,很普通的一个页面。
可是这个行星列表页面,倘若使用Java编码,就得书写下面一大段长长的代码:
public class PlanetJavaAdapter extends BaseAdapter { private Context mContext; private ArrayList<Planet> mPlanetList; private int mBackground; public PlanetJavaAdapter(Context context, ArrayList<Planet> planet_list, int background) { mContext = context; mPlanetList = planet_list; mBackground = background; } @Override public int getCount() { return mPlanetList.size(); } @Override public Object getItem(int arg0) { return mPlanetList.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_view, null); holder.ll_item = (LinearLayout) convertView.findViewById(R.id.ll_item); holder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon); holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name); holder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } Planet planet = mPlanetList.get(position); holder.ll_item.setBackgroundColor(mBackground); holder.iv_icon.setImageResource(planet.image); holder.tv_name.setText(planet.name); holder.tv_desc.setText(planet.desc); return convertView; } public final class ViewHolder { public LinearLayout ll_item; public ImageView iv_icon; public TextView tv_name; public TextView tv_desc; } }
上面Java实现的适配器类PlanetJavaAdapter,果真又冗长又晦涩,然而这段代码模版基本上是列表视图的标配,只要用Java编码,就必须依样画瓢。如果用Kotlin实现这个适配器类会是怎样的呢?马上利用Android Studio把上述Java代码转换为Kotlin编码,转换后的Kotlin代码类似以下片段:
class PlanetKotlinAdapter(private val mContext: Context, private val mPlanetList: ArrayList<Planet>, private val mBackground: Int) : BaseAdapter() { override fun getCount(): Int { return mPlanetList.size } override fun getItem(arg0: Int): Any { return mPlanetList[arg0] } override fun getItemId(arg0: Int): Long { return arg0.toLong() } override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { var view = convertView var holder: ViewHolder? if (view == null) { holder = ViewHolder() view = LayoutInflater.from(mContext).inflate(R.layout.item_list_view, null) holder.ll_item = view.findViewById(R.id.ll_item) as LinearLayout holder.iv_icon = view.findViewById(R.id.iv_icon) as ImageView holder.tv_name = view.findViewById(R.id.tv_name) as TextView holder.tv_desc = view.findViewById(R.id.tv_desc) as TextView view.tag = holder } else { holder = view