Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


Help with Android dynamic fragments


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 sc2bigjoe   Members   -  Reputation: 208

Like
0Likes
Like

Posted 14 May 2014 - 05:36 PM

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, 15 May 2014 - 04:12 PM.


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS