Android SimpleAdapter适配器使用详解
程序员文章站
2023-01-05 21:33:34
android simpleadapter使用详解
holderadapter背景
android的adapterview用的比较多,listview,gridview...
android simpleadapter使用详解
holderadapter背景
android的adapterview用的比较多,listview,gridview,spinner等,原生的baseadapter对viewholder没有支持,每次都要,定义内部类,inflater根布局,对item内部view设置clicklistener并转发到adapter的调用者等。写的次数多了感觉很繁琐,于是写了个简单的封装,简化adapter的编写,
仓库连接http://git.oschina.net/phodev/simpleadapter.
主要文件介绍
1,commonadapter.java从baseadapter继承,添加了常用的函数,通过泛型约定了list数据源,代替子类实现getcount功能,添加bindclick实现adapter的onitemclicklistener。
2,baseviewholder.java和holderadapter结合,实现类似activity的view初始化方方式oncreate->setcontentview->findviewbyid。
3,holderadapter.java从commonadapter继承,主要用来驱动baseviewholder工作。
使用方法
1,adapter定义
public class sampleholderadapter extends holderadapter<object> { //定义item内部的点击事件,如果不需要可以不定义 public static final int click_action_add = 1; public static final int click_action_update = 2; public static final int click_action_delete = 3; //构造函数配置数据源(可选,或者之后通过setdata更新) public sampleholderadapter(list<object> data) { super(data); } @override public baseviewholder<?> createviewholder(int position) { //创建期望的 viewholder return new viewholder(); } class viewholder extends baseviewholder<object> { button btn1, btn2, btn3; @override protected void oncreate(context context, viewgroup parent) { //viewholder创建后oncreate会被执行1次 setcontentview(r.layout.item_test);//setcontent支持view或者layout id btn1 = (button) findviewbyid(r.id.btn1);//findviewbyid btn2 = (button) findviewbyid(r.id.btn2); btn3 = (button) findviewbyid(r.id.btn3); } @override protected void ondatachanged(int position, object data) { //adapter getview的时候ondatachanged会被执行一次 btn1.settext("add " + position); btn2.settext("update " + position); btn3.settext("delete " + position); // bindclick(btn1, position, click_action_add);//绑定itemclick,每次都需bind bindclick(btn2, position, click_action_update); bindclick(btn3, position, click_action_delete); } } }
2,adapter使用
//数据源 private final arraylist<object> mdata = new arraylist<>(); //创建adapter并绑定数据源 private final sampleholderadapter madapter = new sampleholderadapter(mdata); @override protected void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mlistview = (listview) findviewbyid(r.id.listview); //绑定adapter mlistview.setadapter(madapter); //设置click listener(由于commonadapter支持,并通过bindclick声明) madapter.setonitemclicklistener(onitemclicklistener); } //clicklistener实现 private onitemclicklistener onitemclicklistener = new onitemclicklistener() { @override public void onitemclick(commonadapter<?> adapter, view v, int position, int action) { switch (action) {//action就是我们sampleholderadapte中定义并且bindclick的value case sampleholderadapter.click_action_add: toast(getcontext(), position + " add click"); break; case sampleholderadapter.click_action_update: toast(getcontext(),position + " update click"); break; case sampleholderadapter.click_action_delete: toast(getcontext(), position + " delete click"); break; } } };
到此定义和使用的代码完毕。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!