0

I'm learning to create application on android and I have a problem with the fragment functionality.

I created a new black activity using eclipse and selecting the "Swipe views+title strip" navigation type.

I ran it and it's fully working displaying Section 1, Section 2, Section 3. What I wanted to do is selecting a different layout for each section so i tweaked the code this way :

package fr.mpsn.networkclient;

import android.R.layout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class HomeActivity extends FragmentActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
     * will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_home, menu);
        return true;
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            Log.i("info", ""+position);
            Fragment fragment = new DummySectionFragment("view_publishmessage");
            switch (position) {
            case 0:
                fragment =null;
                fragment = new DummySectionFragment("view_publishmessage");
            case 1:
                fragment =null;
                fragment = new DummySectionFragment("view_timeline");
            case 2:
                fragment =null;
                fragment = new DummySectionFragment("view_profile");
            }

            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return "Nouvelle publication".toUpperCase();
            case 1:
                return "Timeline".toUpperCase();
            case 2:
                return "Profil".toUpperCase();
            }
            return null;
        }
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";
        public final String fragmentLayoutName;

        public DummySectionFragment(String fragmentLayoutName) {
            this.fragmentLayoutName = fragmentLayoutName;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Create a new TextView and set its text to the fragment's section
            // number argument value.

            return inflater.inflate(
                    this.getResources().getIdentifier(this.fragmentLayoutName,
                            "layout", "fr.mpsn.networkclient"), null);
        }
    }

}

The problem is that all the different section uses the view profile layout even if the case is correctly parsed...

Have you got any idea on how can I improve this code to make it work better?

yumaikas
  • 979
  • 13
  • 24

1 Answers1

1

Didn't you miss some break statement in your switch? without it, the code continue to the next case:

switch(cond) {
case A:
  print("hello!");
  //break;
case B:
  print("hello again!");
  //break;
}
=> 
hello! 
hello again!

Also, it is better to use empty constructor for fragment, otherwise you'll have problem when the fragment is re-created (after a configuration change for example).
See Do fragments really need an empty constructor?

You can use Fragment.setArguments() and Fragment.getArguments() to pass your fragmentLayoutName.

Community
  • 1
  • 1
nicopico
  • 3,606
  • 1
  • 28
  • 30
  • I created a new version of this code a bit better, i'll share it as an answer here as soon as i can answer (low reputation limits) hope it will help people facing this problem – Adel 'Sean' Helal Mar 09 '13 at 17:53
  • If nicopico fixed your problem, you should accept his answer. Also, in new ADT (21.1) anything other than empty constructor results in a lint error and code will not compile. – Matt Mar 09 '13 at 18:22