Domanda

I want to give each of my fragment a unique tag because they don't have id's (I'm using ViewPager) How can I do that?

public class MainActivity extends FragmentActivity {

    ViewPager vp=null;

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

        vp=(ViewPager)findViewById(R.id.viewpager);
        FragmentManager fragmentManager=getSupportFragmentManager();
        vp.setAdapter(new Adapter(fragmentManager));
    }   
    public class Adapter extends FragmentStatePagerAdapter {

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

        @Override
        public Fragment getItem(int a) {
            Fragment fragment = null;

            if(a==0) {
                fragment = new FragmentX();
            }
            if(a==1) {
                fragment = new FragmentY();
            }
                        if(a==2) {
                fragment = new FragmentZ();
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

I'm a newbie on this java/android language. So just go easy on me. It'd be good if you'll answer me with code. Thank you.

Nessuna soluzione corretta

Altri suggerimenti

Edit : if my comments are not enough to put you on the good path, here is my 2 cents for what they are worht as I am also a newbie.

The best thing you should do is to try your hands at demo projects. There are many demo project for Android. I recommend CommonsWare's work, for it is amazing. Here you will find several projects around the viewpager. From the link I gave you will find many many more project that will 99% show you a good implementation of ANY features you will need as a beginner/moderate coder.

To learn Android (this is what I am doing for 8 months now), I found that if you have no teacher, the best way is to find demo project and modify them and twisted them to really learn why and how things work.

Good luck.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top