Jump to content
  • Advertisement
Sign in to follow this  
b1gjo3

Help with Android dynamic fragments

This topic is 1595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

edit: i'm editing my post to better explain my problem.

 

I have an android application using swipe tabs to navigate between fragments. I have 2 fragments defined in xml file and 1 main activity. Whenever I run my application I'm getting the tabs to show up properly with the correct names "Administration", "Scan", "About"... but the page is blank. When I step through my debugger to check the return value of onCreateView (in my FragmentPagerAdapter code below) it appears to be returning correct value... I can't figure out why I keep getting a blank screen.

 

Here is my activity_main.xml

    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/pager"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="com.atd.giftmonster.MainActivity" />  

Here is my MainActivity.java

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {  
      
    ViewPager mViewPager;  
    DFragmentManager mFragmentManager;  
      
    private static EditText textEmail;  
    private static EditText textPw;  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
      
        List<String> titles = new ArrayList<String>();  
        titles.add("administrator");  
        titles.add("scan");  
        titles.add("about");  
      
        List<Integer> layoutids = new ArrayList<Integer>();  
        layoutids.add(R.layout.fragment_admin);  
        layoutids.add(R.layout.fragment_scan);  
        layoutids.add(R.layout.fragment_scan);  
      
        // Set up the action bar.  
        final ActionBar actionBar = getSupportActionBar();  
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  
      
        mFragmentManager = new DFragmentManager(getSupportFragmentManager(), layoutids, titles);  
      
        // Set up the ViewPager with the sections adapter.  
        mViewPager = (ViewPager) findViewById(R.id.pager);  
        mViewPager.setAdapter(mFragmentManager);  
      
        // When swiping between different sections, select the corresponding  
        // tab. We can also use ActionBar.Tab#select() to do this if we have  
        // a reference to the Tab.  
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {  
            @Override  
            public void onPageSelected(int position) {  
                actionBar.setSelectedNavigationItem(position);  
            }  
        });  
      
        // For each of the sections in the app, add a tab to the action bar.  
        for (int i = 0; i < mFragmentManager.getCount(); i++) {  
            actionBar.addTab(  
                    actionBar.newTab()  
                            .setText(mFragmentManager.getPageTitle(i))  
                            .setTabListener(this));  
        }  
    }  

Here is my FragmentPagerAdapter w/ static fragment class

    public class DFragmentManager extends FragmentPagerAdapter {  
      
    List<String> mTitle;  
    List<Integer> mLayoutID;  
    List<Fragment> mFragments;  
    FragmentManager mFragmentManager;  
      
    public DFragmentManager(FragmentManager fm, List<Integer> layoutIDs, List<String> layoutTitles)  
    {  
        super(fm);  
        mFragments = new ArrayList<Fragment>();  
      
        mLayoutID = layoutIDs;  
        mFragmentManager = fm;  
        mTitle = layoutTitles;  
      
        int size = mLayoutID.size();  
      
        for (int i = 0; i < size; i++)  
        {  
      
            mFragments.add(DFragment.newInstance(mLayoutID.get(i)));  
        }  
    }  
      
    @Override  
    public Fragment getItem(int position)  
    {  
        return mFragments.get(position);  
    }  
      
    @Override  
    public int getCount()  
    {  
        return mFragments.size();  
    }  
      
    @Override  
    public CharSequence getPageTitle(int position) {  
        //return (CharSequence)new String("ss");  
        //return (CharSequence) mFragments.get(position).getTitle();  
        return (CharSequence) mTitle.get(position);  
    }  
      
    public Fragment getFragment(int position)  
    {  
        return mFragments.get(position);  
    }  
      
    public static class DFragment extends Fragment  
    {  
        private View mView;  
        private static final String ARG_LAYOUT_ID = "layout_id";  
      
        public static final DFragment newInstance(int layoutID)  
        {  
            DFragment fragment = new DFragment();  
            Bundle bundle = new Bundle();  
            bundle.putInt(ARG_LAYOUT_ID, layoutID);  
            fragment.setArguments(bundle);  
            return fragment;  
        }  
      
        @Override  
        public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle bundle)  
        {  
            int resourceID = getArguments().getInt(ARG_LAYOUT_ID);  
            mView = inflater.inflate(resourceID, group, false);  
            return mView;  
        }  
      
        public View getView()  
        {  
            return mView;  
        }  
    }  
Edited by sc2bigjoe

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!