onTabSelected FragmentTransaction show not showing - android

So when I first start up my activity the code will populate each tab, and the actionBar.addTab(tab,true) will call on TabSelected which will then add each fragment. When I physically click on a tab it should then just call fragmentTransaction.Show() and show the fragment. I just get blank. Any idea?
Here is the onCreate code:
// missing some #override stuff etc.
public void onCreate {
mSectionsPagerAdapter = new SectionsPagerAdapter(this.getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
View tabView = inflater.inflate(R.layout.tab_title, null);
TextView titleTV = (TextView) tabView.findViewById(R.id.action_custom_title);
titleTV.setText(mSectionsPagerAdapter.getPageTitle(i));
titleTV.setSingleLine();
titleTV.setTextSize(13);
Tab t = actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this);
t.setCustomView(tabView);
actionBar.addTab(t, true);
}
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Bundle b = new Bundle();
switch (tab.getPosition()){
case 0:
b.putString("show", getString(R.string.title_section1));
if (mShowFragment1 == null) {
mShowFragment1 = EpisodeTileFragment.instantiate(this, EpisodeTileFragment.class.getName());
mShowFragment1.setArguments(b);
ft.add(android.R.id.content, mShowFragment1);
addFragmentToActiveList(mShowFragment1);
} else {
ft.show(mShowFragment1);
}
ft.show(mShowFragment1);
break;
case 1:
b.putString("show", getString(R.string.title_section4));
if(mShowFragment3 == null) {
mShowFragment3 = EpisodeTileFragment.newInstance(getString(R.string.title_section4));
ft.add(android.R.id.content, mShowFragment3);
}
mShowFragment3.getArguments().putAll(b);
ft.show(mShowFragment3);
break;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if(position == 0 || position == 1) {
return EpisodeTileFragment.newInstance(getString(R.string.title_section1));
} else {
return PlaceholderFragment.newInstance(position + 1);
}
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
return "test";
}
}

you're calling the wrong method. FragmentTransaction.show works only for a fragment that has been already attached and that is just hidden.
What you're looking for is fragmentTransaction.commit()
At the same time you might want to replace() the current shown fragment instead of adding a new one to the existing container.
So, just to summarize:
ft.replace(id, fragment).commit();

Related

how to maintain backstack of fragment using FragmentStatePagerAdapter

MainActivity containing Navigation drawer. Clicking on navigation drawer item,opening a fragment with viewpager. Clicking on tabs fragment item, another fragment is coming but when pressing back button the viewpager fragment coming blank. How to maintain backstack?
ViewPagerAdapter
public class ViewPagerAdapterProfile extends FragmentStatePagerAdapter{
int mNumOfTabs;
public ViewPagerAdapterProfile(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
if (position ==0) {
return new PersonalDetailsFragment();
} else if (position == 1)
{
return new ProfessionalDetailsFragment();
} else return new EmploymentDetailsFragment();
}
#Override
public int getCount() {
return mNumOfTabs;
}
#Override
public int getItemPosition(#NonNull Object object)
{
return PagerAdapter.POSITION_NONE;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void setListViewHeight(ExpandableListView listView, int group) {
if ((listAdapter.getChildrenCount(i) == 0) && (i == group)) {
Fragment fragment = null;
switch (group) {
case 0:
fragment = new DashboardFragment();
tv_name.clearComposingText();
tv_name.setText("Dashboard");
break;
case 1:
fragment = new MyProfileFrament();
tv_name.clearComposingText();
tv_name.setText("My Profile");
break;
default:
break;
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
mDrawerLayout.closeDrawer(Gravity.LEFT);
}
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
int height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();
}
#Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
for (Fragment frag : fm.getFragments()) {
if (frag.isVisible()) {
FragmentManager childFm = frag.getChildFragmentManager();
if (childFm.getBackStackEntryCount() > 0) {
childFm.popBackStack();
return;
}
}
}
super.onBackPressed();
}
}
MyProfileFrament
public class MyProfileFrament extends Fragment{
private ViewPager viewPager;
private TabLayout tabLayout;
private String[] pageTitle = {"Personal Details", "Education/Professional Details", "Employement Details"};
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_myprofile, container, false);
viewPager = (ViewPager) view.findViewById(R.id.view_pager);
tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
for (int i = 0; i < 3; i++) {
tabLayout.addTab(tabLayout.newTab().setText(pageTitle[i]));
}
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
ViewPagerAdapterProfile pagerAdapter = new ViewPagerAdapterProfile(requireFragmentManager(), pageTitle.length);
viewPager.setAdapter(pagerAdapter);
//change Tab selection when swipe ViewPager
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//change Tab selection when swipe ViewPager
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//change ViewPager page when tab selected
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
}
you can maintain the backstack of fragment using :
Fragment fragment=new YourFragmentClass();
FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction ft=fragmentManager.beginTransaction();
ft.add(R.id.content_frame, fragment);
ft.hide(SourceFragment.this);
ft.addToBackStack(SourceFragment.class.getName());
ft.commit();

Dynamically add and remove tabs in TabLayout(material design) android

I have a TabLayout and inside that I have ViewPager. I need to dynamically add and remove tab in tablayout(material design). I can able to add the tabs dynamically but while removing the tab, tab is removing correctly. But viewpager last item is deleted. So tab is not showing specific fragment. FYI i have used FragmentPagerAdapter.
I have followed this tutorial to implement this
https://androidbelieve.com/navigation-drawer-with-swipe-tabs-using-design-support-library
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2;
private MyNewsFragment mMyNewsFragment;
private ArrayList<TabFragmentModelClass> mFragmentArrayList;
private TabLayoutAdapter mTabLayoutAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mMyNewsFragment = new MyNewsFragment();
/**
*Inflate tab_layout and setup Views.i;;a
*/
View x = inflater.inflate(R.layout.tab_layout, null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); // scorllable tab
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); // tab name fill parent
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
mFragmentArrayList = new ArrayList<>();
mFragmentArrayList.add(new TabFragmentModelClass(new MyNewsSelectionFragment(), "", ""));
mFragmentArrayList.add(new TabFragmentModelClass(mMyNewsFragment, "", getResources().getString(R.string.mynews)));
mFragmentArrayList.add(new TabFragmentModelClass(new BreakingNewsFragment(), "", getResources().getString(R.string.breakingnews)));
// Selected news from shared preference
ArrayList<MyNewsSharedPreferenceModelClass> tempSharedPreferenceModelClass = new MyNewsSharedPreferenceClass().loadFavorites(getActivity());
for (int i = 0; i < tempSharedPreferenceModelClass.size(); i++) {
mFragmentArrayList.add(new TabFragmentModelClass(new CategoreyNewsFragment(tempSharedPreferenceModelClass.get(i).getmCatID()), tempSharedPreferenceModelClass.get(i).getmCatID(),
tempSharedPreferenceModelClass.get(i).getmCatName()));
}
}
/**
*Set an Apater for the View Pager
*/
mTabLayoutAdapter = new TabLayoutAdapter(getChildFragmentManager(), mFragmentArrayList);
viewPager.setAdapter(mTabLayoutAdapter);
viewPager.setOffscreenPageLimit(mFragmentArrayList.size());
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
tabLayout.getTabAt(position);
}
#Override
public void onPageScrollStateChanged(int state) {
if (state == 1) {
updateMyNewsFragment();
}
}
});
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.getTabAt(0).setIcon(R.mipmap.mynewselection);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
switch (tab.getPosition()) {
case 1:
System.out.println("000000000000000000000 ");
updateMyNewsFragment();
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
});
return x;
}
/**
* update the mynews selection
* this method trigger from the MainNewsActivity
*/
public void updateMyNewsFragment() {
if (ApplicationController.getInstance().getBoolPref(CommonVariableInterFace.isNewsSelected)) {
mMyNewsFragment.sendMyNewsRequest();
ApplicationController.getInstance().setBoolPref(CommonVariableInterFace.isNewsSelected, false);
}
}
/**
* update the tab values
* this method trigger from the MainNewsActivity
* if value is zero need to add in tab
* if value is one need to remove in tab
*/
public void updateTabvalue(String catId, String catName, int value) {
if (value == 0) { // add the value in tab
mFragmentArrayList.add(new TabFragmentModelClass(new CategoreyNewsFragment(catId), catId,
catName));
} else { // Removing the tab
for (int i = 0; i < mFragmentArrayList.size(); i++) {
if (mFragmentArrayList.get(i).getmCatID().equalsIgnoreCase(catId)) {
Log.i("-----same id ", catId);
mFragmentArrayList.remove(i);
mTabLayoutAdapter = new TabLayoutAdapter(getChildFragmentManager(), mFragmentArrayList);
viewPager.setAdapter(mTabLayoutAdapter);
}
}
}
mTabLayoutAdapter.notifyDataSetChanged();
viewPager.setOffscreenPageLimit(mFragmentArrayList.size());
tabLayout.getTabAt(0).setIcon(R.mipmap.mynewselection);
tabLayout.setupWithViewPager(viewPager);
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.getTabAt(0).setIcon(R.mipmap.mynewselection);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
switch (tab.getPosition()) {
case 1:
updateMyNewsFragment();
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
});
}
public void openSpecificTab() {
// tabLayout.settab
// tabLayout.se
}
}
Adapter code:
public class TabLayoutAdapter extends FragmentPagerAdapter {
private long baseId = 0;
ArrayList<TabFragmentModelClass> fragmentAdapterArrayList;
public TabLayoutAdapter(FragmentManager fm, ArrayList<TabFragmentModelClass> fragmentArrayList) {
super(fm);
fragmentAdapterArrayList = fragmentArrayList;
}
#Override
public Fragment getItem(int position) {
if (position == 0)
return fragmentAdapterArrayList.get(position).getmFragment();
if (position == 1)
return fragmentAdapterArrayList.get(position).getmFragment();
if (position == 2)
return fragmentAdapterArrayList.get(position).getmFragment();
return new CategoreyNewsFragment().newInstance(fragmentAdapterArrayList.get(position).getmCatID());
}
#Override
public int getCount() {
return fragmentAdapterArrayList.size();
}
//this is called when notifyDataSetChanged() is called
#Override
public int getItemPosition(Object object) {
// refresh all fragments when data set changed
return TabLayoutAdapter.POSITION_NONE;
}
#Override
public long getItemId(int position) {
// give an ID different from position when position has been changed
return baseId + position;
}
/**
* Notify that the position of a fragment has been changed.
* Create a new ID for each position to force recreation of the fragment
*
* #param n number of items which have been changed
*/
public void notifyChangeInPosition(int n) {
// shift the ID returned by getItemId outside the range of all previous fragments
baseId += getCount() + n;
}
/**
* // * This method returns the title of the tab according to the position.
* //
*/
#Override
public CharSequence getPageTitle(int position) {
return fragmentAdapterArrayList.get(position).getmCatName();
}
}
Remove tab from TabLayout
...
public void removeTab(int position) {
if (mTabLayout.getTabCount() >= 1 && position<mTabLayout.getTabCount()) {
mTabLayout.removeTabAt(position);
mPagerAdapter.removeTabPage(position);
}
}
...
Add a removeTabPage method to your PagerAdapter
...
public void removeTabPage(int position) {
if (!tabItems.isEmpty() && position<tabItems.size()) {
tabItems.remove(position);
notifyDataSetChanged();
}
}
...
Add a Tab
...
private void addTab(String title) {
mTabLayout.addTab(mTabLayout.newTab().setText(title));
mPagerAdapter.addTabPage(title);
}
...
Add a addTabPage method to your PagerAdapter
...
public void addTabPage(String title) {
tabItems.add(title);
notifyDataSetChanged();
}
...
Check out this sample code for a full example: ...samples/SupportDesignDemos/src/com/example/android/support/design/widget/TabLayoutUsage.java
With the new support library (I'm using 23.2.1) you should only add to the Viewpager and not the TabLayout (otherwise you end up with double tabs and other funky behavior). So to update TouchBoarder's answer:
Add a removeTabPage method to your PagerAdapter
public void removeTabPage(int position) {
if (!tabItems.isEmpty() && position<tabItems.size()) {
tabItems.remove(position);
notifyDataSetChanged();
}
}
Add a addTabPage method to your PagerAdapter
public void addTabPage(String title) {
tabItems.add(title);
notifyDataSetChanged();
}
In addition to existing answers, to remove all tabs from tabLayout.
tabLayout.removeAllTabs();
You can add dynamic tabs like this :
public void addTabs(){
for(YourClassModel tabName: tabsName){
Log.i("ActivityClass","tabName"+tabName);
tabLayout.addTab(tabLayout.newTab().setText(tabName.getName()));
}
Removetab from tablayout
TabActivity and ViewPager code :
`public class ScrollableTabsActivity extends AppCompatActivity {
int NumberOfTab = 5;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
ViewPagerAdapter adapter ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrollable_tabs);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
int length = tabLayout.getTabCount();
for (int i = 0; i < length; i++) {
tabLayout.getTabAt(i).setCustomView(getTabView(i));
}
}
public View getTabView(final int position) {
View view = LayoutInflater.from(this).inflate(R.layout.close_tablayout, null);
TextView title = (TextView) view.findViewById(R.id.title);
ImageView icon = (ImageView) view.findViewById(R.id.close);
title.setText(adapter.getPageTitle(position));
icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
NumberOfTab = NumberOfTab - 1;
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
if (tabLayout.getTabCount()==3)
tabLayout.setTabMode(TabLayout.MODE_FIXED);
int length = tabLayout.getTabCount();
for (int i = 0; i < length; i++) {
tabLayout.getTabAt(i).setCustomView(getTabView(i));
}
}
});
return view;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
final Fragment result;
switch (position) {
case 0:
// First Fragment of First Tab
result = new OneFragment();
break;
case 1:
// First Fragment of Second Tab
result = new TwoFragment();
break;
case 2:
// First Fragment of Second Tab
result = new ThreeFragment();
break;
case 3:
// First Fragment of Second Tab
result = new FourFragment();
break;
case 4:
// First Fragment of Second Tab
result = new FiveFragment();
break;
default:
result = null;
break;
}
// Log.d("RESULT",result.getTag());
return result;
}
#Override
public int getCount() {
return NumberOfTab;
}
#Override
public CharSequence getPageTitle(final int position) {
switch (position) {
case 0:
return "One";
case 1:
return "Two";
case 2:
return "Three";
case 3:
return "Four";
case 4:
return "Five";
default:
return null;
}
}
}
}`
Check out this Links :
https://stackoverflow.com/a/58480151/9223264
I am new in stackoverflow i try my best if you think my answer is useful then please appreciate

Show a detail fragment when clicking an item in a ListFragment in Android Studio

My app has a tab bar that should be visible at all times. The first tab contains a ListFragment. When I click on an item within, it loads a new activity that creates a detail fragment displaying the contents of the object in the list. I would like to display this content without having to start a new activity because it also destroys the tab bar!
Any help would be greatly appreciated!
To illustrate, here are some screenshots and code:
This is the code for the FragmentActivity that creates the tabs:
public class MainFragmentActivity extends FragmentActivity
implements ActionBar.TabListener {
SectionsPagerAdapter sectionsPagerAdapter = null;
ViewPager viewPager = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
sectionsPagerAdapter =
new SectionsPagerAdapter
(
getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
}); // End of sectionPageAdapter.
Tab browseTab = actionBar.newTab();
browseTab.setIcon(R.drawable.search);
browseTab.setTabListener(this);
actionBar.addTab(browseTab);
Tab myStuffTab = actionBar.newTab();
myStuffTab.setIcon(R.drawable.my_stuff);
myStuffTab.setTabListener(this);
actionBar.addTab(myStuffTab);
Tab profileTab = actionBar.newTab();
profileTab.setIcon(R.drawable.profile);
profileTab.setTabListener(this);
actionBar.addTab(profileTab);
Tab settingsTab = actionBar.newTab();
settingsTab.setIcon(R.drawable.settings);
settingsTab.setTabListener(this);
actionBar.addTab(settingsTab);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment browseFragment = new BrowseFragment();
Bundle browseArgs = new Bundle();
browseArgs.putInt(BrowseFragment.sectionNumberKey, position + 1);
browseFragment.setArguments(browseArgs);
return browseFragment;
case 1:
Fragment myStuffFragment = new MyStuffFragment();
Bundle myStuffArgs = new Bundle();
myStuffArgs.putInt(BrowseFragment.sectionNumberKey, position + 1);
myStuffFragment.setArguments(myStuffArgs);
return myStuffFragment;
case 2:
Fragment profileFragment = new ProfileFragment();
Bundle profileArgs = new Bundle();
profileArgs.putInt(BrowseFragment.sectionNumberKey, position + 1);
profileFragment.setArguments(profileArgs);
return profileFragment;
case 3:
Fragment settingsFragment = new SettingsFragment();
Bundle settingsArgs = new Bundle();
settingsArgs.putInt(BrowseFragment.sectionNumberKey, position + 1);
settingsFragment.setArguments(settingsArgs);
return settingsFragment;
}
return null;
}
// There are always 4 tabs
#Override
public int getCount() {
return 4;
}
// Return a CharSequence for the selected tab
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase();
case 1:
return getString(R.string.title_section2).toUpperCase();
case 2:
return getString(R.string.title_section3).toUpperCase();
case 3:
return getString(R.string.title_section4).toUpperCase();
}
return null;
}
}
} // End of class.
This is the code for the first tab:
public class BrowseFragment extends ListFragment {
public static String sectionNumberKey = "sec_num";
private String activityName = "Browse";
int currentPosition = 0;
List<Listing> listings = new ListingData().getListings();
public BrowseFragment() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BrowseArrayAdapter adapter = new BrowseArrayAdapter(getActivity(),
R.layout.browselist_item,
listings);
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_browse,
container, false);
TextView tv = (TextView) rootView.findViewById(R.id.section_label);
int intSectionNumber = getArguments().getInt(sectionNumberKey);
String numAsString = Integer.toString(intSectionNumber);
tv.setText(numAsString);
activityName += " " + numAsString;
return rootView;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
void showDetails(int index) {
currentPosition = index;
Intent intent = new Intent();
intent.setClass(getActivity(), BrowseDetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
And here is the code for the Detail Activity:
public class BrowseDetailsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
BrowseDetailFragment details = new BrowseDetailFragment();
details.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
}
}
}
Basicly you just have to use the content of the onCreate method from your BrowseDetailActivity in the showDetails method of your BrowseFragment. In this way, you can drop your BrowseDetailsActivity.
BrowseFragment.java
void showDetails(int index) {
BrowseDetailFragment details = BrowseDetailFragment.newInstance(index);
getChildFragmentManager().beginTransaction().add(details).commit();
}
And use the static newInstance method inside your BrowseDetailFragment like so:
BrowseDetailFragment.java
public class BrowseDetailFragment extends Fragment {
private int position;
public static BrowseDetailFragment newInstance(int position) {
BrowseDetailFragment fragment = new BrowseDetailFragment();
fragment.position = position;
return fragment;
}
public BrowseDetailFragment() {
//Required empty constructor
}
//Lifecycle methods and logics
}
Make sure to provide some navigation option so users can return to your list.

Android, How to mix ActionBar.Tab + View Pager + ListFragment

I've been trying to test how to mix all these things together and I'm having problems!!
I just want an app with three tabs using the ActionBar.Tab. For example, this tabs can be movies genres Action, Adventure and Animation, the user can swipe through the tabs, so it will use the ViewPager and each tab will show a list of movies of that genre. There's no need to have three different fragments classes because all tabs will be the same format a simple list.
And I'm having problems because when I select the second tab, the position for onPageSelected is 1,
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCollectionPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
});
This causes the call to the method public Fragment getItem(int i) inside the CollectionPagerAdapter class, but then the value of i is 2 NOT 1, so then it calls the createView for the TabFragment class with a value of 2 NOT 1, so tabs are not refreshing successfully.
Any help will be really appreciated!!
Code to create the tabs,
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mCollectionPagerAdapter.getCount(); i++) {
mActionBar.addTab(mActionBar.newTab()
.setText(mGenres.get(i).getName())
.setTabListener(this));
//Let's request the movies for the first three genres
new GetMoviesByGenre().execute(mGenres.get(i).getId());
}
When a tab is selected,
#Override
public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction arg1) {
//Let's update the dataset for the selected genre
TabFragment fragment =
(TabFragment) getSupportFragmentManager().findFragmentByTag(
"android:switcher:"+R.id.pager+":"+tab.getPosition());
if(fragment != null) // could be null if not instantiated yet
{
if(fragment.getView() != null)
{
fragment.updateDisplay(tab.getPosition()); // do what updates are required
}
}
mViewPager.setCurrentItem(tab.getPosition());
}
CollectionPageAdapter class
public class CollectionPagerAdapter extends FragmentPagerAdapter {
final int NUM_ITEMS = 3; // number of tabs
List<Fragment> fragments = new ArrayList<Fragment>();
public Fragment getItem(int pos) {
return fragments.get(pos);
}
public void addFragment(Fragment f) {
fragments.add(f);
}
public CollectionPagerAdapter(FragmentManager fm) {
super(fm);
//Let's add the fragments
for (int i=0;i<NUM_ITEMS;i++)
{
Fragment fragment = new TabFragment();
Bundle args = new Bundle();
args.putInt(TabFragment.ARG_OBJECT, 0);
fragment.setArguments(args);
addFragment (fragment);
}
}
#Override
public int getCount() {
return NUM_ITEMS;
}
}
TabFragment class
public class TabFragment extends ListFragment {
public static final String ARG_OBJECT = "object";
private MoviesAdapter m_Adapter;
private ArrayList <Movie> mMovies = new ArrayList<Movie>();
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// you only need to instantiate these the first time your fragment is
// created; then, the method above will do the rest
if (m_Adapter == null) {
m_Adapter = new MoviesAdapter(getActivity(), mMovies);
}
setListAdapter(m_Adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int position = getArguments().getInt(ARG_OBJECT); // to check is the right fragment
View rootView = inflater.inflate(R.layout.tabs, container, false);
return rootView;
}
public void updateDisplay (int type)
{
GlobalVars gv = (GlobalVars)getActivity().getApplicationContext();
switch (type)
{
case 0:
mMovies = gv.getActionMovies();
break;
case 1:
mMovies = gv.getAdventureMovies();
break;
case 2:
mMovies = gv.getAnimationMovies();
break;
}
m_Adapter.notifyDataSetChanged();
}
}
I don't what I'm doing wrong, I guess that the fragments are messed up, because when I press the second tab, data from the first tab is updated, and so on ...
Thanks!
Instead of using CollectionPageAdapter, I changed to use the TabsAdapter class shown in Android documentation of ViewPager and it works!
public class TabsAdapter extends FragmentPagerAdapter
implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private final List<Fragment> fragments = new ArrayList<Fragment>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public TabsAdapter(FragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mTabs.size();
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
Fragment fr = Fragment.instantiate(mContext, info.clss.getName(), info.args);
//addFragment (fr, position);
return fr;
}
public void addFragment(Fragment f, int location) {
if (fragments.size() == 0)
fragments.add(f);
else
fragments.add(location, f);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i=0; i<mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
updateDatasetMovies (i);
mViewPager.setCurrentItem(i);
}
}
}
#Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void updateDatasetMovies (int pos)
{
//Let's update the dataset for the selected genre
TabFragment fragment =
(TabFragment) ((FragmentActivity)mContext).getSupportFragmentManager().findFragmentByTag(
"android:switcher:"+R.id.pager+":"+pos);
//TabFragment fragment = (TabFragment) getItem(pos);
if(fragment != null) // could be null if not instantiated yet
{
if(fragment.getView() != null)
{
// no need to call if fragment's onDestroyView()
//has since been called.
fragment.updateDisplay(pos); // do what updates are required
}
}
}
}`
I agree with nirvik on this, however there is one cruciual improvement that should be made. This implementation has one flaw - when swiping to a next fragment the onPageSelected method is invoked, which in turn invokes onTabSelected method by calling mActionBar.setSelectedNavigationItem(position);. This causes a flicker in the animation.
This:
viewPager.setCurrentItem(i);
should be replaced by this:
if(i != viewPager.getCurrentItem()) {
viewPager.setCurrentItem(i);
}
resulting in a smooth transition.
You can try including the following code in your FragmentPagerAdapter and see if this addresses the issue.
public int getItemPosition(Object object) {
return POSITION_NONE;
}
You could also try using a FragmentStatePagerAdapter.

Viewpager and FragmentPagerAdapter: Page view removal

I'm using a Viewpager with a the FragmentPagerAdapter to allow adding and removing of pages. Each page displays data obtained from the internet.
As a new page is added, a new Fragment is associated with that page. The data are obtained via AsyncTask and displayed in the Fragment. When the user chooses to remove a page, the idea is to destroy the page and the associated Fragment.
In general, this all works well. The problem I'm seeing is as follows:
You have three pages with data:
[Page 1] [Page 2] [Page 3]
You delete any page other than the last one, say page 2; Page 2 disappears as desired:
[Page 1] [Page 3]
You add a new page; but instead of a blank, new page, the new page shows the data (view) from page 3.
[Page 1] [Page 3] [Page 4, but showing view/data of Page 3, should be blank]
The page removal code in my activity is as follows:
// Destroy fragment for this page
DataListFragment curFrag = getFragmentByName(currentPage);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().remove(curFrag).commit();
fm.executePendingTransactions();
curFrag = null;
// Remove page and update adapter
mPageTitles.remove(position);
mAdapter.notifyDataSetChanged();
Using the debugger, it shows that the fragment is removed from the FragmentManager after the executePendingTransactions() call. But in the FrampePagerAdapters call, mAdapter.notifyDataSetChanged(), the fragment is added back in and then displayed when a new page is created.
I tried using the FrameStatePagerAdapter, since that should allow destroying fragments, but it did not work. In my FragmentPagerAdapter's getItemPosition() method, I use return FragmentAdapter.POSITION_NONE; as pointed out in another SO article I came across.
It seems as if the View for that page is not destroyed, but then added back into the new page. I tried using the removeViewAt() method on the view of the new page, but that did not work.
Being new to this, I'm sure I'm missing something obvious...
You should rather extend FragmentStatePagerAdapter and remove corresponding item from the list of items in that adapter, instead of trying to remove a Fragment from activity. Do not forget to call adapter.notifyDataSetChanged() after you removed an item in adapter. Once done, ViewPager and FragmentStatePagerAdapter will take care for the rest.
See your getCount() method is returning exact number of items in the viewPager. And yes, FragmentStatePagerAdapter counts too.
I've ended up with a solution that mixes the following knowledge based on experience:
You can add a new Fragment at the tail without problems.
You cannot readd a Fragment that was previously removed, as it leads to java.lang.IllegalStateException: Can't change tag of fragment sometimes, so you have to clone it.
For removing a Fragment you have to return PagerAdapter.POSITION_NONE in the method getItemPosition(Object object), and remove the Fragment from the FragmentManager.
If you are adding/removing/replacing in other place different than the tail, you have to remove everything from the position you are changing until the end, do the stuff, and then readd the (cloned) Fragments that you removed.
Here it is a complete FragmentActivity code with a FragmentPagerAdapter that has 3 methods for adding, removing and replacing tabs:
public class TabTestActivity extends FragmentActivity implements
ActionBar.TabListener {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private static int tabCount = 0;
private static String labelString = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
labelString = getString(R.string.title_section);
setContentView(R.layout.activity_tab_test);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(final int position) {
(new Handler()).postDelayed(new Runnable() {
#Override
public void run() {
actionBar.setSelectedNavigationItem(position);
}
}, 1);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.tab_test, menu);
return true;
}
public void addNewTab() {
int position = (mSectionsPagerAdapter.getCount() > 0 ? mViewPager.getCurrentItem() : 0);
mSectionsPagerAdapter.insertFragment(position);
mViewPager.setCurrentItem(position, true);
}
public void removeTab() {
if (mSectionsPagerAdapter.getCount() > 0) {
int position = mViewPager.getCurrentItem();
mSectionsPagerAdapter.removeFragment(position);
}
}
public void replaceTab() {
if (mSectionsPagerAdapter.getCount() > 0) {
int position = mViewPager.getCurrentItem();
mSectionsPagerAdapter.replaceFragment(position);
mViewPager.setCurrentItem(position, false);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_tab:
addNewTab();
return true;
case R.id.action_remove_tab:
removeTab();
return true;
case R.id.action_replace_tab:
replaceTab();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> currentFragments;
private FragmentManager fragmentManager;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
fragmentManager = fm;
currentFragments = new ArrayList<Fragment>();
}
public void insertFragment(int position) {
// Remove fragments from position
List<Fragment> fragmentsToRemove = new ArrayList<Fragment>(currentFragments.subList(position, currentFragments.size()));
int i = currentFragments.size() - 1;
int j = -1;
int k = i;
while (i >= position) {
currentFragments.remove(i);
i--;
j++;
}
notifyDataSetChanged();
final ActionBar actionBar = getActionBar();
while (k >= position) {
actionBar.removeTabAt(k);
k--;
}
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
while (j >= 0) {
Fragment fragmentToRemove = fragmentsToRemove.get(j);
transaction.detach(fragmentToRemove);
transaction.remove(fragmentToRemove);
j--;
}
transaction.commit();
fragmentManager.executePendingTransactions();
notifyDataSetChanged();
// Add new fragment
Fragment fragment = new DummySectionFragment();
currentFragments.add(position, fragment);
notifyDataSetChanged();
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(position))
.setTabListener(TabTestActivity.this), position);
// Readd fragments
if (fragmentsToRemove.size() > 0) {
i = 1;
for (Fragment fragmentToRemove : fragmentsToRemove) {
currentFragments.add(DummySectionFragment.cloneExistingFragment((DummySectionFragment)fragmentToRemove));
notifyDataSetChanged();
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(position + i))
.setTabListener(TabTestActivity.this), position + i);
i++;
}
}
}
public void removeFragment(int position) {
// Remove fragments from position
List<Fragment> fragmentsToRemove = new ArrayList<Fragment>(currentFragments.subList(position, currentFragments.size()));
int i = currentFragments.size() - 1;
int j = -1;
int k = i;
while (i >= position) {
currentFragments.remove(i);
i--;
j++;
}
notifyDataSetChanged();
final ActionBar actionBar = getActionBar();
while (k >= position) {
actionBar.removeTabAt(k);
k--;
}
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
while (j >= 0) {
Fragment fragmentToRemove = fragmentsToRemove.get(j);
transaction.detach(fragmentToRemove);
transaction.remove(fragmentToRemove);
j--;
}
transaction.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
notifyDataSetChanged();
// Readd fragments (except one)
if (fragmentsToRemove.size() > 1) {
i = 0;
for (Fragment fragment : fragmentsToRemove.subList(1, fragmentsToRemove.size())) {
currentFragments.add(DummySectionFragment.cloneExistingFragment((DummySectionFragment)fragment));
notifyDataSetChanged();
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(position + i))
.setTabListener(TabTestActivity.this), position + i);
i++;
}
}
}
public void replaceFragment(int position) {
// Remove fragments from position
List<Fragment> fragmentsToRemove = new ArrayList<Fragment>(currentFragments.subList(position, currentFragments.size()));
int i = currentFragments.size() - 1;
int j = -1;
int k = i;
while (i >= position) {
currentFragments.remove(i);
i--;
j++;
}
notifyDataSetChanged();
final ActionBar actionBar = getActionBar();
while (k >= position) {
actionBar.removeTabAt(k);
k--;
}
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
while (j >= 0) {
Fragment fragmentToRemove = fragmentsToRemove.get(j);
transaction.detach(fragmentToRemove);
transaction.remove(fragmentToRemove);
j--;
}
transaction.commit();
fragmentManager.executePendingTransactions();
notifyDataSetChanged();
// Add new fragment
Fragment fragment = new DummySectionFragment();
currentFragments.add(position, fragment);
notifyDataSetChanged();
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(position))
.setTabListener(TabTestActivity.this), position);
// Readd fragments (except one)
if (fragmentsToRemove.size() > 0) {
i = 1;
for (Fragment fragmentToRemove : fragmentsToRemove.subList(1, fragmentsToRemove.size())) {
currentFragments.add(DummySectionFragment.cloneExistingFragment((DummySectionFragment)fragmentToRemove));
notifyDataSetChanged();
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(position + i))
.setTabListener(TabTestActivity.this), position + i);
i++;
}
}
}
#Override
public Fragment getItem(int position) {
if (currentFragments == null) {
currentFragments = new ArrayList<Fragment>();
}
while (currentFragments.size() <= position) {
currentFragments.add(null);
}
if (currentFragments.get(position) != null) {
return currentFragments.get(position);
}
Fragment fragment = new DummySectionFragment();
currentFragments.set(position, fragment);
return fragment;
}
#Override
public int getCount() {
return currentFragments.size();
}
#Override
public int getItemPosition(Object object) {
int position = currentFragments.indexOf(object);
if (position == -1) {
return PagerAdapter.POSITION_NONE;
}
return position;
}
#Override
public CharSequence getPageTitle(int position) {
return ((DummySectionFragment)getItem(position)).getTitle();
}
}
public static class DummySectionFragment extends Fragment {
private int sectionNumber;
public DummySectionFragment() {
super();
sectionNumber = ++tabCount;
}
public static DummySectionFragment cloneExistingFragment(DummySectionFragment fragment) {
DummySectionFragment cloned = new DummySectionFragment();
// Hack for avoiding autoincrement
--tabCount;
cloned.sectionNumber = fragment.getSectionNumber();
return cloned;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab_test_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(String.format(labelString, sectionNumber));
return rootView;
}
public int getSectionNumber() {
return sectionNumber;
}
public String getTitle() {
return String.format(labelString, sectionNumber);
}
}
}

Categories

Resources