java - Dynamically add or remove fragments in tabbed activity FragmentStatePagerAdapter without refreshing existing data -
i have created activity named survey in using tabbed activity fragmentstatepageradapter dynamically add or remove tabs , fragments. including common layout named title.xml in each fragment layout. have created 1 button add fragment , 1 remove fragment in actionbar.
when use fragmentpageradapter , call remove function removes tabs , title not fragment , when use fragmentstatepageradapter getitemposition return position_none removes fragments refreshes title xml in other fragments. same thing occurs on adding fragments, display values of title xml in current fragment. rest fragments values refreshed.
my survey.java
public class survey extends appcompatactivity { private sections_pager_adapter msectionspageadapter; private viewpager mviewpager; private textview mtitle; private string projectname,auditstage,sectiontype; private edittext etsectionname; private spinner spsection; private imagebutton ibsectiongps,ibsectioncamera; private arraylist<latlng> sectionlatlong =new arraylist<>(); private imagebutton ibsectionadd,ibsectiondel,ibgps,ibcamera; tablayout tablayout; private view section_dialog,section_list=null; private sections_pager_adapter adapter; private fragmentmanager manager = getsupportfragmentmanager(); private fragmenttransaction ft = manager.begintransaction(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_survey); // home btn imagebutton home_btn = (imagebutton) findviewbyid(r.id.home); home_btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent = new intent(getapplicationcontext(), home.class); startactivity(i); overridependingtransition(r.anim.enter_from_left, r.anim.exit_to_right); } }); ibgps = (imagebutton) findviewbyid(r.id.survey_gps); ibcamera = (imagebutton) findviewbyid(r.id.survey_camera); ibgps.setvisibility(view.gone); ibcamera.setvisibility(view.gone); ////////////////////// alert box ///////////////////////////////// ibsectionadd = (imagebutton) findviewbyid(r.id.section_add); ibsectiondel = (imagebutton) findviewbyid(r.id.section_del); layoutinflater inflater = this.getlayoutinflater(); section_dialog = inflater.inflate(r.layout.fragment_stage4_section_form, null); etsectionname = (edittext) section_dialog.findviewbyid(r.id.section_name); spsection = (spinner) section_dialog.findviewbyid(r.id.spinnersection); ibsectiongps = (imagebutton) section_dialog.findviewbyid(r.id.sectiongps); ibsectioncamera = (imagebutton) section_dialog.findviewbyid(r.id.sectioncamera); arrayadapter<charsequence> sp_section = arrayadapter.createfromresource(survey.this, r.array.sections, r.layout.spinner_font); sp_section.setdropdownviewresource(r.layout.spinner_font); spsection.setadapter(sp_section); spsection.setonitemselectedlistener(new adapterview.onitemselectedlistener(){ @override public void onitemselected(adapterview<?> adapterview, view view, int position, long row_id) { sectiontype = adapterview.getitematposition(position).tostring(); } @override public void onnothingselected(adapterview<?> adapterview) { } }); ////////////////////// intent , set title //////////////////////////////////////// projectname = getintent().getstringextra("project_name").tostring(); auditstage = getintent().getstringextra("audit_stage").tostring(); mtitle = (textview) findviewbyid(r.id.toolbar_title); mtitle.settext(projectname); ///////////////////// sections page adapter on btn click//////////// msectionspageadapter = new sections_pager_adapter(getsupportfragmentmanager()); adapter = new sections_pager_adapter(getsupportfragmentmanager()); mviewpager = (viewpager) findviewbyid(container_survey); tablayout = (tablayout) findviewbyid(r.id.tabs_survey); // display default fragment adapter.addfragment(new stage4_fragment(), "default"); setupviewpager(mviewpager); tablayout.setupwithviewpager(mviewpager); tablayout.setvisibility(view.gone); } private void setupviewpager(viewpager viewpager){ if (auditstage.equals("construction stage")){ ibsectionadd.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { setsection(section_dialog); } }); ibsectiondel.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if ((adapter.mfragmentlist.size() != 0 && adapter.getpagetitle(0).equals("default")) || adapter.mfragmentlist.size() == 0){ toast.maketext(survey.this, "currently, there no sections.",toast.length_long).show(); } else if (adapter.mfragmentlist.size() != 0){ alertdialog.builder builder = new alertdialog.builder(survey.this); builder.setcancelable(true); builder.settitle(html.fromhtml("<font color='#7592cc'>delete section:</font>")); builder.setmessage("are sure, want delete current section name"); builder.setpositivebutton("confirm", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { (int = 0; < adapter.mfragmentlist.size(); i++) { fragment frag = adapter.getitem(mviewpager.getcurrentitem()); if (frag.equals(adapter.mfragmentlist.get(i))) { adapter.removefragment(i); adapter.notifydatasetchanged(); } } } }); builder.setnegativebutton(android.r.string.cancel, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { } }); alertdialog dialog = builder.create(); dialog.show(); } } }); ibsectiongps.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(survey.this, location.class); intent.putextra("section_gps_point", "sectiongpspoint"); startactivityforresult(intent,1); } }); ibsectioncamera.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { } }); } mviewpager.setadapter(adapter); } @override public void onbackpressed() { super.onbackpressed(); overridependingtransition(r.anim.enter_from_left, r.anim.exit_to_right); } //// create dynamic views in tab layout public void setsection(final view view){ alertdialog.builder builder = new alertdialog.builder(survey.this); builder.setview(view); builder.setcancelable(true); builder.settitle(html.fromhtml("<font color='#7592cc'>section details:</font>")); builder.setpositivebutton("confirm", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { } }); builder.setnegativebutton(android.r.string.cancel, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { } }); final alertdialog dialog = builder.create(); dialog.show(); dialog.getbutton(alertdialog.button_positive).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(ibgps.getvisibility() == view.gone) { ibgps.setvisibility(view.visible); ibcamera.setvisibility(view.visible); } boolean wanttoclosedialog = false; if (!etsectionname.gettext().tostring().isempty()) { if (adapter.mfragmenttitlelist.size()!=0){ if (adapter.mfragmenttitlelist.get(0).equals("default")) { adapter.removefragment(0); adapter.notifydatasetchanged(); } } adapter.addfragment(new stage4_type1_fragment(), etsectionname.gettext().tostring()); adapter.notifydatasetchanged(); if (tablayout.getvisibility() != view.visible) { tablayout.setvisibility(view.visible); } int size = adapter.mfragmentlist.size(); fragment curr_frag = adapter.getitem(size-1); ////////set list data///// textview tvsectionname = (textview) curr_frag.getview().findviewbyid(r.id.section_name_text); textview tvsectiontype = (textview) curr_frag.getview().findviewbyid(r.id.section_type); textview tvsectionstart = (textview) curr_frag.getview().findviewbyid(r.id.section_start_point); textview tvsectionend = (textview) curr_frag.getview().findviewbyid(r.id.section_end_point); textview tvsectiondate = (textview) curr_frag.getview().findviewbyid(r.id.section_date); tvsectionname.settext(etsectionname.gettext().tostring()); tvsectiontype.settext(sectiontype); decimalformat twodform = new decimalformat("#.######"); double slat,slong,elat,elong; if (!sectionlatlong.isempty()) { slat = sectionlatlong.get(0).latitude; slong = sectionlatlong.get(0).longitude; elat = sectionlatlong.get(1).latitude; elong = sectionlatlong.get(1).longitude; tvsectionstart.settext(twodform.format(slat).tostring() + " : " + twodform.format(slong).tostring()); tvsectionend.settext(twodform.format(elat).tostring() + " : " + twodform.format(elong).tostring()); } currentdate(tvsectiondate); ((viewgroup)view.getparent()).removeview(view); etsectionname.settext(""); wanttoclosedialog = true; } else { etsectionname.seterror("field cannot empty."); wanttoclosedialog = false; } if (wanttoclosedialog) { dialog.dismiss(); } } }); dialog.getbutton(alertdialog.button_negative).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { } }); } public void currentdate(textview date){ date curdate = new date(); simpledateformat format = new simpledateformat("yyyy-mm-dd hh:mm a"); string datetostr = format.format(curdate); date.settext(datetostr); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == 1) { if (resultcode == result_ok) { arraylist<latlng> result = data.getparcelablearraylistextra("sendlocation"); sectionlatlong = result; } } } }
my section_pager_adapter.java
public class sections_pager_adapter extends fragmentstatepageradapter{ public final arraylist<fragment> mfragmentlist = new arraylist<>(); public final arraylist<string> mfragmenttitlelist = new arraylist<>(); public void addfragment(fragment fragment,string title){ mfragmentlist.add(fragment); mfragmenttitlelist.add(title); } public sections_pager_adapter(fragmentmanager fm) { super(fm); } @override public charsequence getpagetitle(int position){ return mfragmenttitlelist.get(position); } @override public fragment getitem(int position) { return mfragmentlist.get(position); } @override public int getitemposition(object object) { return position_none; //to make notifydatasetchanged() } @override public int getcount() { return mfragmentlist.size(); } public void removefragment(int value) { mfragmenttitlelist.remove(value); mfragmentlist.remove(value); } }
title.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="90dp" android:padding="6dip"> <imageview android:id="@+id/section_icon" android:layout_width="90dp" android:layout_height="fill_parent" android:layout_alignparentbottom="true" android:layout_alignparenttop="true" android:layout_marginright="@dimen/activity_vertical_margin" android:contentdescription="todo" android:background="@color/colorgrey" android:src="@drawable/ic_menu_camera" /> <textview android:id="@+id/section_name_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_torightof="@id/section_icon" android:gravity="center_vertical" android:text="name" android:textsize="16sp" /> <textview android:id="@+id/section_type_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/section_name_text" android:layout_torightof="@id/section_icon" android:ellipsize="marquee" android:maxlines="1" android:text="type: " android:textsize="12sp" /> <textview android:id="@+id/section_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/section_name_text" android:layout_torightof="@id/section_type_text" android:ellipsize="marquee" android:maxlines="1" android:text="" android:textsize="12sp" /> <textview android:id="@+id/section_start_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/section_type" android:layout_torightof="@id/section_icon" android:ellipsize="marquee" android:maxlines="1" android:text="start point: " android:textsize="12sp" /> <textview android:id="@+id/section_start_point" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/section_type" android:layout_torightof="@id/section_start_text" android:ellipsize="marquee" android:maxlines="1" android:text="" android:textsize="12sp" /> <textview android:id="@+id/section_end_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/section_start_text" android:layout_torightof="@id/section_icon" android:ellipsize="marquee" android:maxlines="1" android:text="end point: " android:textsize="12sp" /> <textview android:id="@+id/section_end_point" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/section_start_point" android:layout_torightof="@id/section_end_text" android:ellipsize="marquee" android:maxlines="1" android:text="" android:textsize="12sp" /> <textview android:id="@+id/section_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:gravity="right" android:text="5:45" android:layout_marginright="5dip" android:textsize="12dip" android:textstyle="bold"/> <imagebutton android:id="@+id/listarrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="4dp" android:layout_centervertical="true" android:layout_alignparentright="true" android:layout_marginright="5dip" android:background="@drawable/ic_assignment_turned_in_grey_400_24dp" /> <view android:layout_alignparentbottom="true" android:layout_height="1dp" android:background="@color/colorgrey" android:layout_width="fill_parent"/> </relativelayout>
Comments
Post a Comment