Reputation: 1861
I've been fighting this issue all day long I think, and still can't get it right. I'm building some app for a friend of mine. I'm using ActionBarSherlock so I could use action bar and some other stuff like the pageviewer.
Anyway, I'm tring to get on the main activity: 3 tabs that each show a fragment (3 different fragments) and each of those fragments has to show a fragmentPagerView so I could scroll fragments back and forth (maybe I'll add "ever-scroll" at some point after I get this right)
The thing is that I've got my tabs up and running (I've copied some code from the apiDemos) however I can't get the tabs to show, and I cant find the reason why (it doesn't even crash)
This is my MainActivity that holds the navActionbar (and some options)
public class MainActivity extends SherlockFragmentActivity
{
SherlockFragment yesTabFragment = new YesTabFragment();
SherlockFragment newTabFragment = new NewTabFragment();
SherlockFragment trendingTabFragment = new TrendingTabFragment();
private void createTabs()
{
// put ActionBar to nav. mode
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab yesTab = actionBar
.newTab()
.setText("yes")
.setTabListener(new TabListener<YesTabFragment>(
this, "yes", YesTabFragment.class));
actionBar.addTab(yesTab);
ActionBar.Tab trendingTab = actionBar
.newTab()
.setText("trending")
.setTabListener(new TabListener<TrendingTabFragment>(
this, "trending", TrendingTabFragment.class));
actionBar.addTab(trendingTab);
ActionBar.Tab newTab = actionBar
.newTab()
.setText("new")
.setTabListener(new TabListener<NewTabFragment>(
this, "new", NewTabFragment.class));
actionBar.addTab(newTab);
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
createTabs();
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main_activity_menu, menu);
return true;
}
public static class TabListener<T extends SherlockFragment> implements ActionBar.TabListener
{
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;
private SherlockFragment mFragment;
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz)
{
this(activity, tag, clz, null);
}
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz, Bundle args)
{
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
mFragment = (SherlockFragment)mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached())
{
FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
}
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
if (mFragment == null)
{
mFragment = (SherlockFragment)SherlockFragment.instantiate(mActivity, mClass.getName(), mArgs);
ft.add(android.R.id.content, mFragment, mTag);
}
else
{
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
if (mFragment != null)
{
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
//Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show();
}
}
}
as I've said I copied the TabListener implementation straight from the apiDemos (though the one I wrote was exactly the same)
this is the implementation of one of the tab fragments (they all are the same)
public class YesTabFragment extends SherlockFragment
{
View _fragmentView;
private Vector<stuff> list;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
_fragmentView = inflater.inflate(R.layout.yes_fragment, container, false);
ViewPager pager = (ViewPager)_fragmentView.findViewById(R.id.gag_pager);
API api = API.getInstance();
list = api.get("Yes", 1, 10);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getFragmentManager(), listOfGags);
pager.setAdapter(adapter);
return _fragmentView;
}
}
the implementation of the yesTabFragment class
public class YesTabFragment extends SherlockFragment
{
View _fragmentView;
private Vector<things> list;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
_fragmentView = inflater.inflate(R.layout.yes_fragment, container, false);
ViewPager pager = (ViewPager)_fragmentView.findViewById(R.id.thing_pager);
API api = API.getInstance();
list = api.get("Yes", 1, 10);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getFragmentManager(), list);
pager.setAdapter(adapter);
return _fragmentView;
}
}
and of course the XML for the main and yes tab (again the tabs are almost the same)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/List"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
and the yesTab
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/thing_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
I really can't find the reason why the tab fragments do not replace the framelayout and show me the viewpager.
I could really use some good advice here because I'm definitely missing something big here and can't understand what...
Thanks a head...
Upvotes: 1
Views: 1049
Reputation: 9342
your MainActivity should contain ViewPager defenition, for example:
viewPager=(ViewPager)findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
/** Defining a listener for pageChange */
ViewPager.SimpleOnPageChangeListener pageChangeListener =
new ViewPager.SimpleOnPageChangeListener()
{
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
actionBar.setSelectedNavigationItem(position);
}
};
/** Setting the pageChange listner to the viewPager */
viewPager.setOnPageChangeListener(pageChangeListener);
/** Creating an instance of FragmentPagerAdapter */
fragmentPagerAdapter = new PhotoMugFragmentPagerAdapter(fm);
/** Setting the FragmentPagerAdapter object to the viewPager object */
viewPager.setAdapter(fragmentPagerAdapter);
Upvotes: 3