使用android中的recylerView控件案例
程序员文章站
2023-12-15 10:30:04
使用android中的recylerview控件案例
在使用recylerview的时候,我一直感觉这个控件使用起来比较复杂,所以要总结一下有关于这个控件的相关的知识,其实只要好好剖析一下安卓中有关...
使用android中的recylerview控件案例
在使用recylerview的时候,我一直感觉这个控件使用起来比较复杂,所以要总结一下有关于这个控件的相关的知识,其实只要好好剖析一下安卓中有关于控件的知识会发现还是比较容易使用的。
1.添加依赖关系
首先,根据我个人的经验来感觉的话,如果要使用一个控件,首先要看这个控件是否存在于相关的包中,如果不存在的化就要在依赖中使用这个包,对于recyclerview也是一样的,首先要在和android项目中的app目录下的build.grandle文件里面添加相关的依赖关系:
compile 'com.android.support:recyclerview-v7:26.1.0'
其中依赖关系后面的数字代表使用的版本号,根据具体的使用可以进行调整。
2.在布局文件也就是相关的xml文件中使用recyclerview的控件,然后设置它的相关的属性,具体的属性和其它的ui控件差不多。相关的实例:
3.定义recylerview控件的内部条目的样式,这里就可以有各种多样的设置了,自己可以自己定义相关的控件:
4.然后就是配置recyclerview的相关的适配器了,配置的方法也是很简单的主要是实现recyclerview.adapter类,然后重载其中的oncreateviewholder,onbindviewholder,getitemcount方法,然后因为其中要用到viewholder所以要写一个viewholder的类用来存放临时的数据,其中每一个viewholde就是recyclerview中的一个条目。相关的代码如下:
package com.example.dell_gxr.week10; import android.annotation.suppresslint; import android.content.dialoginterface; import android.support.v7.app.alertdialog; import android.support.v7.widget.recyclerview; import android.text.textutils; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.edittext; import android.widget.textview; import android.widget.toast; import java.util.list; /** * created by dell_gxr on 2017/12/7. */ public class personadapter extends recyclerview.adapter{ private list mlist; private persondatabase mdbhelper; @override public personviewholder oncreateviewholder(viewgroup parent, int viewtype) { final view view = layoutinflater.from(parent.getcontext()) .inflate(r.layout.main_item, parent, false); final personviewholder holder = new personviewholder(view); final int position = holder.getadapterposition(); log.d("personadapter","on create number: " + integer.tostring(position)); return holder; } @override public void onbindviewholder(personviewholder holder, @suppresslint("recyclerview") final int position) { log.d("personadapter","on bind number: " + integer.tostring(position)); person current = mlist.get(position); holder.name.settext(current.getname()); holder.birthday.settext(current.getbirthday()); holder.gift.settext(current.getgift()); holder.itemview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { final view view1 = layoutinflater.from(v.getcontext()) .inflate(r.layout.modify_dialog, null); textview textview1 = view1.findviewbyid(r.id.modify_dialog_name_textview); textview textview2 = view1.findviewbyid(r.id.modify_dialog_number_textview); textview1.settext("姓名:" + mlist.get(position).getname()); textview2.settext("手机号码:" + mlist.get(position).getnumber()); edittext edittext1 = view1.findviewbyid(r.id.modify_dialog_birthday_edittext); edittext edittext2 = view1.findviewbyid(r.id.modify_dialog_gift_edittext); edittext1.settext(mlist.get(position).getbirthday()); edittext2.settext(mlist.get(position).getgift()); alertdialog.builder builder = new alertdialog.builder(v.getcontext()); builder.settitle("快乐每天!"); builder.setview(view1); builder.setcancelable(true); builder.setpositivebutton("保存修改", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { edittext edittext1 = view1.findviewbyid(r.id.modify_dialog_birthday_edittext); edittext edittext2 = view1.findviewbyid(r.id.modify_dialog_gift_edittext); if(textutils.isempty(edittext1.gettext().tostring())) { mlist.get(position).setbirthday(""); } else { mlist.get(position).setbirthday(edittext1.gettext().tostring()); } if(textutils.isempty(edittext2.gettext().tostring())) { mlist.get(position).setgift(""); } else { mlist.get(position).setgift(edittext2.gettext().tostring()); } notifydatasetchanged(); toast.maketext(view1.getcontext(), "修改成功", toast.length_short).show(); mdbhelper.update(mlist.get(position)); } }); builder.setnegativebutton("放弃修改", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { } }); builder.show(); } }); holder.itemview.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(final view v) { alertdialog.builder builder = new alertdialog.builder(v.getcontext()); builder.settitle("是否删除?"); builder.setpositivebutton("是", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { mdbhelper.delete(mlist.get(position)); mlist.remove(position); notifydatasetchanged(); toast.maketext(v.getcontext(), "成功删除", toast.length_short).show(); } }); builder.setnegativebutton("否", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(v.getcontext(), "取消删除", toast.length_short).show(); } }); builder.show(); return false; } }); } @override public int getitemcount() { return mlist.size(); } personadapter(list tlist, persondatabase dbhelper) { super(); mlist = tlist; mdbhelper = dbhelper; } class personviewholder extends recyclerview.viewholder { textview name; textview birthday; textview gift; public personviewholder(view itemview) { super(itemview); name = itemview.findviewbyid(r.id.item_name_main); birthday = itemview.findviewbyid(r.id.item_birthday_main); gift = itemview.findviewbyid(r.id.item_gift_main); } } }
5.最后就是在主活动为相应的recyclerview使用这个适配器了,注意要在使用适配器的过程中设置相应的layout,这样才能使得recylerview正常显示:
public void inititem() { recyclerview recyclerview = findviewbyid(r.id.person_recyclerview); personadapter personadapter = new personadapter(alllist, dbhelper); linearlayoutmanager layoutmanager = new linearlayoutmanager(this); recyclerview.setlayoutmanager(layoutmanager); recyclerview.setadapter(personadapter); recyclerview.additemdecoration(new divideritemdecoration(this,divideritemdecoration.vertical)); }