Domanda

I want to set an empty view / show a text in the middle of the ViewPager when there are no screens on a FragmentActivity. I could use getCount() with the mPagerAdapter and add a fragment with a layout with the empty string but it seems like an overkill.

I'd like to know if there is a better option. I think setEmptyView() is made for this cases but I haven't been able to implement it. I just want to show a string with a hint of how to add screens to the ViewPager for my users.

How can it be done?

This is what I'm doing now:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
    //...
    if(mPagerAdapter.getCount() == 0)
    {
        setContentView(R.layout.fragment_screen_empty);
    }
È stato utile?

Soluzione

My current implementation:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
    //...
    if(mPagerAdapter.getCount() == 0)
    {
        setContentView(R.layout.fragment_screen_empty);
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top