设计模式总结之适配器模式
来源:互联网 发布:python编码汉字互转 编辑:程序博客网 时间:2024/06/05 19:05
适配器模式是一种很常见的模式,做过安卓的想必对适配器再熟悉不过了,对于生活中的例子比如你需要在欧洲国家使用美国制造的笔记本电脑,就可能需要一个交流电的适配器,用来让美式插头也可以通过适配器得到电力。
下面这段代码是我稍微模仿ListView的适配器模式,但实际上ListView的适配要比我的麻烦的多的多。。。
public interface Adapter {public int getCount();public Object getItem(int position);public long getItemId(int position);public View getView(int position, View convertView, ViewGroup parent);}
public class AdapterTest {public static void main(String[] args) {ListView listView = new ListView();ArrayList<String> arrayList = new ArrayList<>();arrayList.add("一");arrayList.add("二"); listView.setListAdapter(new MyTestAdapter(arrayList));listView.obtainView();}}
public abstract class BaseAdapter extends ListAdapter{}
public abstract class ListAdapter implements Adapter{}
public class ListView extends View{private ListAdapter listAdapter;private static ViewGroup vp = new ViewGroup(); public ListAdapter getListAdapter() {return listAdapter;}public void setListAdapter(ListAdapter listAdapter) {this.listAdapter = listAdapter;}public void obtainView(){for (int i = 0; i < listAdapter.getCount(); i++) {listAdapter.getView(i, this, vp);//实际上list对此的操作要麻烦的多}}}
public class MyTestAdapter extends BaseAdapter{ArrayList<String> arrayList;public MyTestAdapter(ArrayList<String> arrayList) {super();this.arrayList = arrayList;} @Overridepublic int getCount() {// TODO Auto-generated method stubreturn arrayList.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View view = new View();return view;}}
public class View {public static int i = 0;public View() {i++;System.out.println(i);}}
public class ViewGroup extends View{}
0 0
- 设计模式总结之适配器模式
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式 之 适配器
- 设计模式 之 适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式 之 适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- Android Studio编译NDK app gradle配置
- Codeforces Round #352 (Div. 2)C. Recycling Bottles
- 求自定类型元素的平均
- poj 1064 Cable master(二分搜索)
- I00011 打印包含7的三位数
- 设计模式总结之适配器模式
- BZOJ4554 [Tjoi2016&Heoi2016]游戏
- 【计算机视觉】OpenCV的最近邻开源库FLANN
- OpenCV实践之路——人脸识别之一数据收集和预处理
- HDOJ 1045 Fire Net同 ZOJ 1002
- MFC中Dialog无法输入中文的问题
- 添加LogCat 至 eclipse --2
- 加密
- 图片跟随鼠标移动(jquery)