作为一个标准的安卓开发人员,ListView并不陌生。现在,我们就来写一个万能的适配器。
首先:我们要了解几个基本的概念:
1– SparseArray的用法,泛型参数是Object;
代码实现部分:
首先,定义一个泛型的CommAdapter 继承自BaseAdapter
public abstract class CommApapter<T> extends BaseAdapter {
private List<T> list;
private Context context;
private int layoutId;
public CommApapter(List<T> list, Context context, int layoutId) {
this.list = list;
this.context = context;
this.layoutId = layoutId;
}
@Override
public int getCount() {
if (list == null || list.size() == 0) {
return 0;
}
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) {
ViewHolder holder = ViewHolder.getHolder(context, convertView, position, layoutId, parent);
convert(holder, getItem(position));
return holder.getConvertView();
}
public abstract void convert(ViewHolder holder, T data);
}
以上需要注意:
1–getItem的返回类型是T;
2–getView的返回值是一个View,View在ViewHolder里面提供方法获取;
3–提供了一个抽象方法convert;
接下来,实现ViewHolder里面的相关代码编写:
public class ViewHolder {
private Context context;
private int position;
private View convertView;
private SparseArray<View> views;
public View getConvertView(){
return convertView;
}
public ViewHolder(Context context, int position, int layoutId, ViewGroup parent) {
this.position = position;
this.views = new SparseArray<>();
convertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
convertView.setTag(this);
}
public static ViewHolder getHolder(Context context, View convertView, int position, int layoutId, ViewGroup parent) {
if (convertView == null) {
return new ViewHolder(context, position, layoutId, parent);
} else {
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.position = position;
return holder;
}
}
}
以上需要注意:
1–getHolder方法是复用Holder;
2–SparseArray views的初始化;
当然,以上并不是我们想要的结果,因为,在ListView里面,我们可以处理很多事情,所以,我们还需要编写相关方法来简化我们的处理;
1–根据ID获取当前View;
public T getView(int viewId) {
View view = views.get(viewId);
if (view == null) {
view = convertView.findViewById(viewId);
views.put(viewId, view);
}
return (T) view;
}
2–为TextView赋值;
public ViewHolder setText(int resId, String str) {
TextView textView = getView(resId);
textView.setText(str);
return this;
}
3–获取当前点击item的position;
public int getPosition(){
return position;
}
4–设置某一个view隐藏或者显示
public void isVisable(int resId,boolean flag) {
View view = getView(resId);
if (flag) {
view.setVisibility(View.VISIBLE);
} else {
view.setVisibility(View.GONE);
}
}
以上需要注意:
1–getView可以灵活使用。使用中,只要是控件,都可以通过此方法获取到当前控件,进行相关操作;
2–可以和图片的相关操作结合使用,网络获取,加载本地图片显示
现在,我们还缺少一个关键的东西:
点击跳转:item点击跳转,长按跳转,布局内某一个View跳转;
现在,我们需要实现某一个View的跳转:
public interface onViewClickListener {
void onClick();
}
public void setOnViewClick(int resId, final onViewClickListener listener) {
View view = getView(resId);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick();
}
});
}
以上需要注意:
1–定义了一个回掉接口来实现相关的点击监听;
2–处理某些view特定事项,跳转;
现在,我们来看看怎么使用:
一:建一个类:
public class Person {
private String name;
private String sex;
private int old;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getOld() {
return old;
}
public void setOld(int old) {
this.old = old;
}
}
二:编写正式的代码:
public class MainActivity extends AppCompatActivity {
List persons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
ListView listView = (ListView) findViewById(R.id.listView);
//R.layout.listview_item为当前ListView的item布局ID
listView.setAdapter(new CommApapter<Person>(persons, MainActivity.this, R.layout.listview_item) {
@Override
public void convert(ViewHolder holder, Person data) {
holder.setText(R.id.name, data.getName());
holder.setText(R.id.old, data.getOld() + "");
holder.setText(R.id.sex, data.getSex());
//获取当前view对象
TextView tv_sex = holder.getView(R.id.sex);
//单一控件的点击监听
holder.setOnViewClick(R.id.sex, new ViewHolder.onViewClickListener() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this, "sex", Toast.LENGTH_SHORT).show();
}
});
}
});
}
//初始化数据
private void initData() {
persons = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Person p = new Person();
p.setName(i + “name”);
p.setOld(i);
p.setSex(“男”);
persons.add(p);
}
}
}
以上,整个算是完成了相关代码的编写,工作中,需要不断编写方法来丰富自己的代码库。