Shaik MD Ashiq Shaik MD Ashiq - 5 months ago 19
Android Question

Sliding tab in fragment malfunctions from second time

I have a MainActivity which contains a Sliding drawer for menu and a FragmentContainer to switch fragments.

I have a Fragment called History which has a layout like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/colorPrimary"
android:textColor="#FFFFFF"
app:pstsIndicatorColor="#FFFFFF" />

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabs" />

</RelativeLayout>


And the class looks like this

public class HistoryFragment extends Fragment {

public HistoryFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_history, container, false);


// Initialize the ViewPager and set an adapter
ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
pager.setAdapter(new PagerAdapter(getActivity().getSupportFragmentManager()));

// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
tabs.setViewPager(pager);

return view;
}

class PagerAdapter extends FragmentPagerAdapter {

private final String[] TITLES = {"Last Transaction", "History"};

public PagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}

@Override
public int getCount() {
return TITLES.length;
}

@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new LastTransaction();
case 1:
return new AboutFragment();
}

return null;
}
}


}


This History page works fine the first time when it is called from the NavigationSlider menu. The history page contains two sliding tabs with two fragments. These are displayed the first time and everything works fine.

The problem happens, when they are called the second time or after that.
There is no error shown, the layout is loaded, the sliding tabs are shown, but their fragments are not shown and the sliders malfunction.

What may be the reason for this problem ?
I tried to use a different approach for implementing the sliders in fragments as per this StackOverflow answer. Still the same problem.

Thanks in advance.

Answer

replace

pager.setAdapter(new PagerAdapter(getActivity().getSupportFragmentManager()));

with

pager.setAdapter(new PagerAdapter(getActivity().getChildFragmentmanager()));

Reason:

The CHILD FragmentManager is the one that handles Fragments contained within the Fragment that it was added to.