Reputation: 178
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.
Upvotes: 0
Views: 3044
Reputation: 4632
For me nothing worked Then I found the solution - Instead of FragmentPagerAdapter, use FragmentStatePagerAdapter.
Upvotes: 1
Reputation: 24848
Page adapter will bind no page as defined page count inside getCount() but in your case you have return static '0' zero count so pager adapter will not bind any page over there and you not able to seen any page data there. You need to set no page count inside getCount() method like 3 as below example :
@Override
public int getCount(){
return 3; // 3 is no of page count
}
Upvotes: 2
Reputation: 1321
Return the number of fragments in getCount()
method:
int fragmentCount=3;
@Override
public int getCount()
{
return fragmentCount;
}
Upvotes: 0
Reputation: 10395
you return Zero on your getCount()
so there will be no Item inside your PagerAdapter:
@Override
public int getCount()
{
return 0;
}
Upvotes: 3