Click here to Skip to main content
15,878,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have implemented Navigation Drawer with Tabs. The content of the tabs load correctly when the respective option in the drawer is pressed for the first time. However when I return to an option already pressed, the tabs do not load correctly. How do I solve this?

HomeFragment.Java whose content do not load properly

Java
public class HomeFragment extends Fragment
{

private TabHost mTabHost;
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;

public HomeFragment() {
}

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

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_home, container, false);


    mTabHost = (TabHost) v.findViewById(android.R.id.tabhost);
    mTabHost.setup();

    mViewPager = (ViewPager) v.findViewById(R.id.pager_home);
    mTabsAdapter = new TabsAdapter(getActivity(), mTabHost, mViewPager);

    // Here we load the content for each tab. When I return to one of these options the tab content do not load properly. I think the problem is here.
    mTabsAdapter.addTab(mTabHost.newTabSpec("one").setIndicator("Recently Viewed"), PageOne.class, null);
    mTabsAdapter.addTab(mTabHost.newTabSpec("two").setIndicator("Offers"), PageTwo.class, null);
    //mTabsAdapter.addTab(mTabHost.newTabSpec("two").setIndicator("Experiences"), ExperiencesTab.class, null);

    return v;
}

public static class TabsAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener
{
    private final Context mContext;
    private final TabHost mTabHost;
    private final ViewPager mViewPager;
    private final ArrayList<tabinfo> mTabs = new ArrayList<tabinfo>();

    static final class TabInfo
    {
        private final String tag;
        private final Class        private final Bundle args;

        TabInfo(String _tag, Class        {
            tag = _tag;
            clss = _class;
            args = _args;
        }
    }

    static class DummyTabFactory implements TabHost.TabContentFactory
    {
        private final Context mContext;

        public DummyTabFactory(Context context)
        {
            mContext = context;
        }

        public View createTabContent(String tag)
        {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

    public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager)
    {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mTabHost = tabHost;
        mViewPager = pager;
        mTabHost.setOnTabChangedListener(this);
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(TabHost.TabSpec tabSpec, Class    {
        tabSpec.setContent(new DummyTabFactory(mContext));
        String tag = tabSpec.getTag();

        TabInfo info = new TabInfo(tag, clss, args);
        mTabs.add(info);
        mTabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }

    @Override
    public int getCount()
    {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position)
    {
        TabInfo info = mTabs.get(position);

        return Fragment.instantiate(mContext, info.clss.getName(), info.args);

    }

    public void onTabChanged(String tabId)
    {
        int position = mTabHost.getCurrentTab();
        mViewPager.setCurrentItem(position);

    }

    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
    {
    }

    public void onPageSelected(int position)
    {
        // Unfortunately when TabHost changes the current tab, it kindly
        // also takes care of putting focus on it when not in touch mode.
        // The jerk.
        // This hack tries to prevent this from pulling focus out of our
        // ViewPager.
        TabWidget widget = mTabHost.getTabWidget();
        int oldFocusability = widget.getDescendantFocusability();
        widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        mTabHost.setCurrentTab(position);
        widget.setDescendantFocusability(oldFocusability);

    }

    public void onPageScrollStateChanged(int state)
    {
    }


PageOne.Java which is the content for 1st Tab

Java
public class PageOne extends Fragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState)
{

    return inflater.inflate(R.layout.recentview_tab, container, false);
}

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

@Override
public void onAttach(Activity activity)
{
    super.onAttach(activity);
}

@Override
public void onStart()
{
    super.onStart();
}

@Override
public void onResume()
{
    super.onResume();
}



PageTwo.Java which is the content for 2nd Tab.

Java
public class PageTwo extends Fragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,                       Bundle savedInstanceState)
{

    return inflater.inflate(R.layout.offers_tab, container, false);
}

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

@Override
public void onAttach(Activity activity)
{
    super.onAttach(activity);
}

@Override
public void onStart()
{
    super.onStart();
}

@Override
public void onResume()
{
    super.onResume();
}
Posted
Updated 26-Feb-15 22:17pm
v2
Comments
Richard MacCutchan 27-Feb-15 4:06am    
Please format your code properly: use the code button above the edit box to add <pre> tags around it. you also need to indicate exactly where the problem occurs.
Member 11330032 27-Feb-15 4:19am    
I have intended the code. Also I have added a comment in HomeFragment.java where I create tabs. I think the problem might be there.
Richard MacCutchan 27-Feb-15 4:33am    
The problem"might be there"? Then you should do some debugging to make sure.
Member 11330032 27-Feb-15 4:36am    
No this method work fine when the app is opened for the first time. But when I go back to an option of the drawer item, the tabs do not load correctly. So I am not sure what is exactly causing the problem. If it works for the first time it has to work every time right??
Richard MacCutchan 27-Feb-15 8:47am    
Not at all. When an app works it does not mean that there are no errors (real or potential) in it. It merely means that the circumstances that may cause an error have not occurred. Even with rigorous testing you can never truthfully state that your app is totally error free.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900