android - RecyclerView content is disappearing -
after minimizing or opening other app content loosing , need fill recycler view again
p.s. in manifest have
android:configchanges="orientation|keyboardhidden|screensize"
recyclerview
<android.support.v7.widget.recyclerview android:id="@+id/booking_rec" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginbottom="10dp" android:background="#eceff1"> </android.support.v7.widget.recyclerview>
adapter
public class recadapterbooking extends recyclerview.adapter<recadapterbooking.customviewholder> { private list<booking> bookinglist; private context context; public recadapterbooking(list<booking> bookinglist, context context) { this.bookinglist = bookinglist; this.context = context; } @override public recadapterbooking.customviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(context).inflate(r.layout.booking_view_item, parent, false); customviewholder viewholder = new customviewholder(view); return viewholder; } @override public void onbindviewholder(recadapterbooking.customviewholder holder, int position) { final booking booking = bookinglist.get(position); string name = booking.getuser().getfirstname() + " " + booking.getuser().getlastname(); string date = booking.getstarttime().tostring() + " - " + booking.gettotime().tostring(); holder.name.settext(booking.getname()); holder.user.settext(name); holder.datetime.settext(date); holder.type.settext(booking.gettype().tostring()); } @override public int getitemcount() { return bookinglist.size(); } public class customviewholder extends recyclerview.viewholder { protected textview name; protected textview user; protected textview datetime; protected textview type; public customviewholder(view view) { super(view); this.name = (textview) view.findviewbyid(r.id.name); this.user = (textview) view.findviewbyid(r.id.user); this.datetime = (textview) view.findviewbyid(r.id.date_time); this.type=(textview) view.findviewbyid(r.id.booking_type); } } }
mainactivity
public class mainactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { list<room> roomlist = new arraylist<>(); static list<booking> bookinglist = new arraylist<>(); static recyclerview recyclerview; static recadapterbooking recadapterbooking; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle toggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close); drawer.setdrawerlistener(toggle); toggle.syncstate(); final navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this); helper.initlist(roomlist); menu menu = navigationview.getmenu(); (room r : roomlist) { menu.add(r.getname()); } linearlayoutmanager layoutmanager = new linearlayoutmanager(this); layoutmanager.setorientation(linearlayoutmanager.vertical); recyclerview = (recyclerview) findviewbyid(r.id.booking_rec); recyclerview.setlayoutmanager(layoutmanager); final calendar c = calendar.getinstance(); final simpledateformat df = new simpledateformat("mmm-dd-yyyy"); final textview textview = (textview) findviewbyid(r.id.time_view); settime(c, df, textview); imagebutton leftbutton = (imagebutton) findviewbyid(r.id.left); imagebutton rightbutton = (imagebutton) findviewbyid(r.id.right); leftbutton.setonclicklistener(new button.onclicklistener() { @override public void onclick(view v) { c.add(calendar.date, -1); textview.settext(df.format(c.gettime())); } }); rightbutton.setonclicklistener(new button.onclicklistener() { @override public void onclick(view v) { c.add(calendar.date, +1); textview.settext(df.format(c.gettime())); } }); if (preferencemanager.getdefaultsharedpreferences(mainactivity.this).getboolean("pref", true)) { intent intent = new intent(mainactivity.this, singleroomactivity.class); intent.setflags(intent.flag_activity_new_task | intent.flag_activity_task_on_home); startactivity(intent); this.finish(); } } public void settime(calendar c, simpledateformat df, textview textview) { final string formatteddate = df.format(c.gettime()); textview.settext(formatteddate); } @override public void onbackpressed() { drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); if (drawer.isdraweropen(gravitycompat.start)) { drawer.closedrawer(gravitycompat.start); } else { super.onbackpressed(); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } //settings-ზე დაჭერის activity @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_settings) { arraylist<string> roomnamelist = new arraylist<>(); (room r : roomlist) { roomnamelist.add(r.getname()); } intent intent = new intent(mainactivity.this, settingsactivity.class); intent.putextra("roomlist", roomnamelist); startactivity(intent); } if (id == r.id.action_log_out) { intent intent = new intent(mainactivity.this, loginactivity.class); startactivity(intent); } return super.onoptionsitemselected(item); } //მენიუდან არჩეული ოთახის განრიგის გამოტანა @suppresswarnings("statementwithemptybody") @override public boolean onnavigationitemselected(menuitem item) { (room r : roomlist) { if (item.gettitle().equals(r.getname())) { bookinglist = r.getbookinglist(); recadapterbooking = new recadapterbooking(bookinglist, mainactivity.this); recyclerview.setadapter(recadapterbooking); settitle(r.getname()); break; } } drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); drawer.closedrawer(gravitycompat.start); return true; }
}
Comments
Post a Comment