Android入门之利用Spinner实现弹出选择对话框
package org.mk.android.demospinneradapter;
import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public abstract class GenericAdapter
private List
private int layoutRes;
public GenericAdapter() {
}
public GenericAdapter(List
this.data = data;
this.layoutRes = layoutRes;
}
@Override
public int getCount() {
Log.i("app",">>>>>>data.size: "+data.size());
if(data!=null) {
return data.size();
}
return 0;
}
@Override
public T getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i("app",">>>>>>into getView");
ViewHolder holder = ViewHolder.bind(parent.getContext(), convertView, parent, layoutRes
, position);
bindView(holder, getItem(position));
return holder.getItemView();
}
//添加一个元素
public void add(T item) {
if (data == null) {
data = new ArrayList<>();
}
data.add(item);
notifyDataSetChanged();
}
//往特定位置,添加一个元素
public void add(int position,T item){
if (data == null) {
data = new ArrayList<>();
}
data.add(position, item);
notifyDataSetChanged();
}
public void remove(T item) {
if(data != null) {
data.remove(item);
}
notifyDataSetChanged();
}
public void remove(int position) {
if(data != null) {
data.remove(position);
}
notifyDataSetChanged();
}
public void clear() {
if(data != null) {
data.clear();
}
notifyDataSetChanged();
}
public abstract void bindView(ViewHolder holder, T obj);
public static class ViewHolder {
private SparseArray
private View item; //存放convertView
private int position; //游标
private Context context; //Context上下文
//构造方法,完成相关初始化
private ViewHolder(Context context, ViewGroup parent, int layoutRes) {
mViews = new SparseArray<>();
this.context = context;
View convertView = LayoutInflater.from(context).inflate(layoutRes, parent,false);
convertView.setTag(this);
item = convertView;
}
public static ViewHolder bind(Context context, View convertView, ViewGroup parent,
int layoutRes, int position) {
ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder(context, parent, layoutRes);
} else {
holder = (ViewHolder) convertView.getTag();
holder.item = convertView;
}
holder.position = position;
return holder;
}
public
T t = (T) mViews.get(id);
if(t == null) {
t = (T) item.findViewById(id);
mViews.put(id, t);
}
return t;
}
/**
* 获取当前条目
*/
public View getItemView() {
return item;
}
/**
* 获取条目位置
*/
public int getItemPosition() {
return position;
}
/**
* 设置文字
*/
public ViewHolder setText(int id, CharSequence text) {
View view = getView(id);
if(view instanceof TextView) {
((TextView) view).setText(text);
}
return this;
}
/**
* 设置图片
*/
public ViewHolder setImageResource(int id, int drawableRes) {
View view = getView(id);
if(view instanceof ImageView) {
((ImageView) view).setImageResource(drawableRes);
} else {
view.setBackgroundResource(drawableRes);
}
return this;
}
/**
* 设置标签
*/
public ViewHolder setTag(int id, Object obj) {
getView(id).setTag(obj);
return this;
}
public ImageView iconImg;
public TextView iconText;
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播