filter - Recycler View with Header and Edit Text -
i have recyclerview header achieved using 2 different element types. in header there edit text want use filtering nonheader elements of list. below current implementation, have 1 concern , 1 problem it.
my concern doing in publishresults notifyitemrangeremoved , notifyiteminserted wrong way update recycler view. doing notifydatasetchanged cause header row refreshed , edit text lose focus. want way refresh item rows , leave header row untouched.
my current problem existing code if scroll down edit text looses focus. want edit text keep focus if scroll bottom of list.
the code used use listview setheaderview , worked somehow there must someway of achieving goal not sure trick recycler view is. appreciated.
public class sidelistadapter extends recyclerview.adapter<recyclerview.viewholder> implements filterable { private static final int type_header = 0; private static final int type_item = 1; private final list<string> data; public list<string> filtereddata; private headeractionlistener headeractionlistener; public sidelistadapter(context context, arraylist<string> data, headeractionlistener headeractionlistener) { this.data = data; filtereddata = new arraylist<>(data); this.context = context; this.headeractionlistener = headeractionlistener; } @override public filter getfilter() { return new testfilter(); } static class sidelistitem extends recyclerview.viewholder { linearlayout baselayout; public sidelistitem(view itemview) { super(itemview); baselayout = (linearlayout) itemview.findviewbyid(r.id.settings_defaultcolor); } } class sidelistheader extends sidelistheader { edittext sort; public sidelistheaderloggedin(view itemview) { super(itemview); sort = (edittext) itemview.findviewbyid(r.id.sort); } } @override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { if (viewtype == type_item) { view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.list_item, parent, false); return new sidelistitem(v); } else if (viewtype == sidelistheader) { view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.header, parent, false); return new sidelistheader(v); } throw new runtimeexception("there no type matches type " + viewtype + " + make sure using types correctly"); } public interface headeractionlistener { boolean onsorteditoraction(textview arg0, int arg1, keyevent arg2); } @override public void onbindviewholder(recyclerview.viewholder holder, final int position) { if (holder instanceof sidelistheader) { final sidelistheader sidelistheader = (sidelistheader) holder; sidelistheader.sort.setoneditoractionlistener(new textview.oneditoractionlistener() { @override public boolean oneditoraction(textview v, int actionid, keyevent event) { } }); sidelistheader.sort.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence charsequence, int i, int i2, int i3) { } @override public void ontextchanged(charsequence charsequence, int i, int i2, int i3) { } @override public void aftertextchanged(editable editable) { string result = sidelistheader.sort.gettext().tostring().replaceall(" ", ""); getfilter().filter(result); } }); } if (holder instanceof sidelistitem) { // inflate normal item // } } // need override method @override public int getitemviewtype(int position) { if (ispositionheader(position)) { return type_header; } return type_item; } private boolean ispositionheader(int position) { return position == 0; } //increasing getitemcount 1. row of header. @override public int getitemcount() { return filtereddata.size() + 1; } private class testfilter extends filter { @override protected filterresults performfiltering(charsequence constraint) { filterresults results = new filterresults(); string prefix = constraint.tostring().tolowercase(); if (prefix.isempty()) { arraylist<string> list = new arraylist<>(data); results.values = list; results.count = list.size(); } else { final arraylist<string> list = new arraylist<>(data); final arraylist<string> nlist = new arraylist<>(); (int = 0 ; < list.size(); i++) { string item = list.get(i); if (item.contains(prefix)) { nlist.add(item); } } results.values = nlist; results.count = nlist.size(); } return results; } @suppresswarnings("unchecked") @override protected void publishresults(charsequence constraint, filterresults results) { notifyitemrangeremoved(1, getitemcount()-1); filtereddata.clear(); filtereddata.addall((list<string>)results.values); for(int = 1; < getitemcount() - 1; i++){ notifyiteminserted(i); } } } }
Comments
Post a Comment