2

I can't seem to find a way to give different text to a dynamically created fragment for my pageviewer-supoorted application. I've came to a point of my codding where I got stuck. For my application I want to have 400-500 dynamically created fragments, where you can horizontally slide thru them and every content of the fragment to be the same (repeating the same fragment) and the only different thing to be the text on them. Here's where I got stuck at my codding :

package com.example.testarearg;

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.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity 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_main);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
        }    

    });
}




/**
 * 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.
        Fragment fragment = new DummySectionFragment();
        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;
    }


}

/**
 * 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.
     */ private String mText; // display this text in your fragment

     public static Fragment getInstance(String text) {
       Fragment f = new Fragment();
       Bundle args = new Bundle();
       args.putString("text", text);
       f.setArguments(args);
       return f;
     }

     public void onCreate(Bundle state) {
       super.onCreate(state);
       setmText(getArguments().getString("text"));
       // rest of your code
     }

    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
        TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
        dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }

    public String getmText() {
        return mText;
    }

    public void setmText(String mText) {
        this.mText = mText;
    }
}

}
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • Possible duplicate of http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager – Triode Sep 08 '13 at 12:23

1 Answers1

2

You need to create your own custom Fragment:

import android.support.v4.app.Fragment;

public class YourFragment extends Fragment {

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

        TextView tv = (TextView) v.findViewById(R.id.yourtextview);
        tv.setText(getArguments().getString("text"));

        return v;
    }

    public static YourFragment newInstance(String text) {

        YourFragment f = new YourFragment();
        Bundle b = new Bundle();
        b.putString("text", text);

        f.setArguments(b);

        return f;
    }
}

And then modify your adapter. The basic idea is that the adapter contains the Strings that you want to display and the getItem(...) method will choose the text.

public class MyPagerAdapter extends FragmentPagerAdapter {

        private String [] strings;

        public MyPagerAdapter(FragmentManager fm, String [] stringstodisplay) {
            super(fm);
            this.strings = stringstodisplay;
        }

        @Override
        public Fragment getItem(int pos) {
           return YourFragment.newInstance(strings[pos]);
        }

        @Override
        public int getCount() {
            return 500; // or strings.length to be save
        }       
    }

From your Activity, you hand over the String array containing the different strings to the adapter.

public class MainActivity extends FragmentActivity {

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

        // get the content that your fragments will display
        String [] strings = new String[500];

        for(int i = 0; i < 500; i++) {
            strings[i] = "This is Fragment " + i;
        }

        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), strings));
    }
}
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • I see, so basically the dummy one is out of discussion and can be deleted and the one to use to be the "yourfragment" one? Also, I have to replace the ' red "text" ' or the stringstodisplay with my string array? Or where do I have to input the string array? Got confused. I'm sorry for my leak of knowledge kind sir. – Feciuc Teodor Sep 08 '13 at 12:53
  • 1
    Yes you create your own Fragment, name it whatever you want, create your own layout file for the Fragment and inflate it inside onCreateView(...) method. Then somewhere in your Activity you need to hand the string array that your Fragments should display to the adapter. I added some example code. – Philipp Jahoda Sep 08 '13 at 12:59