Eladit Eladit - 11 months ago 134
Android Question

FragmentPagerAdapter GetItem is never called?

I'm trying to initate a simple tab swipe, But when I'm adding the PagerAdapter the getItem method is never called..
I cant find the reason for this.

Activity:

public class ProfileActivity extends Activity implements ActionBar.TabListener
{
ViewPager viewPager;
ActionBar actionBar;

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

viewPager = (ViewPager) findViewById(R.id.profile_view_pager);
ProfilePagerAdapter viewPagerAdapter = new ProfilePagerAdapter(getFragmentManager());

viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(0);

actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

ActionBar.Tab friendsTab = actionBar.newTab();
ActionBar.Tab postsTab = actionBar.newTab();
ActionBar.Tab tribesTab = actionBar.newTab();

tribesTab.setText("TRIBES");
postsTab.setText("POSTS");
friendsTab.setText("FRIENDS");

tribesTab.setTabListener(this);
postsTab.setTabListener(this);
friendsTab.setTabListener(this);


actionBar.addTab(friendsTab);
actionBar.addTab(postsTab);
actionBar.addTab(tribesTab);
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{

}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{

}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{

}
}


(I also tried with FragmentActivity Though I do not need the V4 and I'm using the V13)

My adapter:

private class ProfilePagerAdapter extends FragmentPagerAdapter
{

Fragment fragment = new ProfileTribeFragment();

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

@Override
public Fragment getItem(int i)
{
Log.i("D","NO :{");
if(i==0)
{
fragment = new ProfileFriendsFragment();
}
if(i==1)
{
fragment = new ProfilePostsFragment();
}
if(i==2)
{
fragment = new ProfileTribeFragment();
}

return fragment;
}

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


And all the 3 Fragments look like that:

public class ProfileTribeFragment extends Fragment {


public ProfileTribeFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_profile_tribe, container, false);
}


}

I Just cant figure what I'm doing wrong.
The Log.i on the getItem is never seen.

Answer Source

you return Zero on your getCount() so there will be no Item inside your PagerAdapter:

@Override
public int getCount()
{
    return 0;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download