Android fragment FragmentTransaction hide/show title -
my problem following, have fragments , titles viewpageradapter. when use fragmenttransaction.hide fragment went empty (blank), title stays. how can hide title fragment, can swipe shown fragments.
heres code:
private void setupviewpager(customviewpager viewpager) { madapter = new viewpageradapter(getsupportfragmentmanager()); fraglog = new frglog(); fraginfo = new frginfo(); fragtext = new frgtext(); fragfirm = new frgfirm(); fraghist = new frghist(); madapter.addfragment( fraglog, getstring(r.string.frag_title_log)); madapter.addfragment( fraginfo, getstring(r.string.frag_title_info)); madapter.addfragment( fragtext, getstring(r.string.frag_title_text)); madapter.addfragment( fragfirm, getstring(r.string.frag_title_firm)); madapter.addfragment( fraghist, getstring(r.string.frag_title_hist)); viewpager.setadapter(madapter); viewpager.setcurrentitem(1); viewpager.setpagingenabled(true); } class viewpageradapter extends fragmentpageradapter { private final list<fragment> myfragmentlist = new arraylist<>(); private final list<string> myfragmenttitlelist = new arraylist<>(); public viewpageradapter(fragmentmanager manager) { super(manager); } @override public fragment getitem(int position) { return myfragmentlist.get(position); } @override public int getcount() { return myfragmentlist.size(); } public void addfragment(fragment fragment, string title) { boolean found = false; (int = 0; < myfragmentlist.size(); i++) { if (myfragmentlist.get(i).getclass().getname().equalsignorecase(fragment.getclass().getname())) { found = true; } } if (!found) { myfragmentlist.add(fragment); myfragmenttitlelist.add(title); } } @override public charsequence getpagetitle(int position) { return myfragmenttitlelist.get(position); } } protected void displayfirst() { fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); ft.hide(fragtext); ft.show(fragfirm); ft.show(fraghist); ft.commit(); } protected void displaysecond() { fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); ft.hide(fragfirm); ft.hide(fraghist); ft.show(fragtext); ft.commit(); } <xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="eu.angermayr.test.mainactivity"> <eu.angermayr.test.customviewpager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.pagertabstrip android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:paddingbottom="10dp" android:paddingtop="0dp" /> </eu.angermayr.test.customviewpager> </linearlayout>
you can add title using onpagechangelistener:
viewpager.setonpagechangelistener(new onpagechangelistener() { public void onpagescrollstatechanged(int state) {} public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {} public void onpageselected(int position) { // use position set title accordingly. settitle("you fragment specific title"); } });
Comments
Post a Comment