When I try to run the example with https://developer.android.com/reference/android/support/v4/view/ViewPager.html on Eclipse,the emulator was display a error dialog .
Here is the LogCat:
I just don't know why it can't run.I build the project and copy the example code in Eclipse,I also set the Android API level with 11.And it seem all is ok.
When I double click the last but one line of the LogCat:
at com.lanz.xbp2v4.ActionBarTabsPage.onCreate.java:37
it redirect to the code which included in onCreate method :
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
So I think it may something wrong in this part of code,hope that someone can help me!XD
the code in onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(mViewPager);
// Full Screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("Simple").setTabListener((TabListener) this),
IndexTab.class, null);
mTabsAdapter.addTab(bar.newTab().setText("List").setTabListener((TabListener) this),
Tab2.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Cursor").setTabListener((TabListener) this),
Tab3.class, null);
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
}
}
I has fixed the problem above,but it still can't work.At this time I run the example app,the LogCat show:
it redirect to the code which included in getItem() method :
#Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
Fragment fragment=Fragment.instantiate(mContext, info.clss.getName(), info.args);
return fragment;
}
Is anyone can run the example code with Android developer ViewPager?
your getActionBar() is returning null.
From this link, you need to make the window title visible for this.
Related
I have developed an Android application which has 4 TabHost's which are in fragments.
I Know how to customize the ActionBar in MainActivity.
But problem is how can I customize my ActionBar according to the 4 different TabHost's in different Fragments?
Here is my tabHost code -
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
/** Creating ANDROID Tab */
Tab tab = actionBar.newTab()
//.setText("Android")
.setTabListener(new CustomTabListener<PlayFragment01>(this, "play",
PlayFragment01.class))
.setIcon(R.drawable.playtabhosticon);
actionBar.addTab(tab);
/** Creating APPLE Tab */
Tab tab1 = actionBar.newTab()
//.setText("Apple")
.setTabListener(new CustomTabListener<VenueFragment01>(this, "venu",
VenueFragment01.class))
.setIcon(R.drawable.venutabhosticon);
actionBar.addTab(tab1);
/** Creating APPLE Tab */
Tab tab3 = actionBar.newTab()
//.setText("Apple")
.setTabListener(new CustomTabListener<SocialFragment01>(this, "social", SocialFragment01.class))
.setIcon(R.drawable.socialtabhosticon);
actionBar.addTab(tab3);
/** Creating APPLE Tab */
Tab tab4 = actionBar.newTab()
//.setText("Apple")
.setTabListener(new CustomTabListener<ActivityFragment01>(this, "activity",
ActivityFragment01.class))
.setIcon(R.drawable.actionbartabhosticon);
actionBar.addTab(tab4);
}
}
Here is my first fragment code -
package in.wptrafficanalyzer.actionbarnavtab;
/** This is a listfragment class */
public class PlayFragment01 extends Fragment {
/** An array of items to display in ArrayList */
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
/** Creating array adapter to set data in listview */
View rootView = inflater.inflate(R.layout.fragment_play, container, false);
//new DownloadJSON().execute();
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
getActivity().getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#0077d1")));
ActionBar mActionBar = getActivity().getActionBar();
getActivity().getActionBar().setIcon(
new ColorDrawable(getResources().getColor(android.R.color.transparent)));
mActionBar.setDisplayShowHomeEnabled(true);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar2, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
return rootView;
}
}
The question your asking is not very clear but you can get the ActionBar instance from within a Fragment using getActivity().getActionBar().
ActionBar.setNavigationMode() and TabHost is now deprecated.
The new ToolBar introduced in API-21 is more powerfull.
But since you asked for a solution, here is a simple one from Google SlidingTabLayout:
This is like a PagerTabStrip that you can easily customize and works like great.
It works with ViewPager and Fragments of course.
In my point of view ,you should read this ,so that you can get the basic idea for creating a fragment.
http://developer.android.com/guide/components/fragments.html
Try this sample code..this sample code includes 3 tab ,first try to understand this code ,then you can make your own app with 4 tabs
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
Also try to modify your question ,It is unclear and also it will be good that you post your errors
In tab it looks like below.
But in device it looks fine
..
I am looking like below what the google play contained in phones and tabs as same
this is my oncreate method. I had done scroll tabs with view pager .In devices it fits good but in tabs ?.I know this is action bar design pattern .
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_directory_activity);
final ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.drawable.logo);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(true);
myDirectoryPagerAdapter = new MyDirectoryAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(myDirectoryPagerAdapter);
mViewPager.setOnPageChangeListener(new
ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < myDirectoryPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(myDirectoryPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
It will look below the actionbar in the device because the device does not have much space to show all the tabs so the actionbar will manage on its own to show the tabs and adjust it according to the device size. As you have also displayed a logo in actionbar so it will always switch to below.
That is by default for Tabs from Action bar. It is good thing bcoz it saves some screen space for you.
If you dont want it to be present in the action bar. You need to implement it separately. Use PagerTitleStrip.
Two implementations here:
http://developer.android.com/training/implementing-navigation/lateral.html#PagerTitleStrip
https://github.com/astuetz/PagerSlidingTabStrip
I couldn't manage to change the action bar title for every swipe action, there's always a problem. Either they get mixed or just some of them does not show up. Here are my tab codes:
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
SectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
ViewPager = (ViewPager) findViewById(R.id.viewpager);
ViewPager.setAdapter(mSectionsPagerAdapter);
ViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
Tab tab = actionBar.newTab()
.setIcon(R.drawable.home)
.setTabListener(this);
.
.
.
see answer below before going further into the question.
You can try using this in your Fragment's onResume method :
if(getUserVisibleHint()){
getActionBar().setTitle(title);
}
defining String title as a variable inside your fragment.
You should read the documentation on it: http://developer.android.com/training/implementing-navigation/lateral.html#horizontal-paging
and
http://developer.android.com/training/implementing-navigation/lateral.html#swipe-tabs
Download the source code and use it. It worked for me. I had the same problem.
I am fairly good in fragments API, I know how to create master/detail pattern on phone/tablet.
But how would I create a different style of navigation in phones and tablets?
The most obvious example would be swiping tabs on the phone and somekind tablet layout with all the fragments layed out inside, no swiping tabs.
Is there some elegant way of doing this other than giant if statement?
//EDIT
So what would you do, as I presumed, is to create create two layouts (phone - view pager / tablet - three static fragments) and in activity's onCreate check for something in the layout to figure out wether youre in phone or tablet. Then just initialize the tabs navigation / or not if youre in tablet. That should work.
CODE SNIPPETS FROM IO 2012 APP
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
FragmentManager fm = getSupportFragmentManager();
mViewPager = (ViewPager) findViewById(R.id.pager);
if (mViewPager != null) {
// Phone setup
mViewPager.setAdapter(new HomePagerAdapter(getSupportFragmentManager()));
mViewPager.setOnPageChangeListener(this);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab()
.setText(R.string.title_my_schedule)
.setTabListener(this));
actionBar.addTab(actionBar.newTab()
.setText(R.string.title_explore)
.setTabListener(this));
actionBar.addTab(actionBar.newTab()
.setText(R.string.title_stream)
.setTabListener(this));
homeScreenLabel = getString(R.string.title_my_schedule);
} else {
//Tablet setup
mExploreFragment = (ExploreFragment) fm.findFragmentById(R.id.fragment_tracks);
mMyScheduleFragment = (MyScheduleFragment) fm.findFragmentById(R.id.fragment_my_schedule);
mSocialStreamFragment = (SocialStreamFragment) fm.findFragmentById(R.id.fragment_stream);
}
}
Any of you guys that already have tabs navigation on phone, please consider implementing this on the tablet, if it works for you design-wise. You already have fragments api ready, so there's no excuse, really.
You could take a look at the 2012 I/O app from Roman Nurik, which switches navigation/layout pretty much with every possible combination of size and orientation;
http://code.google.com/p/iosched/
I want to replace a SherlockListFragment with other SherlockListFragment but I donĀ“t know how to accomplish this
I use this code to create the fragment tabs:
public class MyTabsViewPager extends SherlockFragmentActivity
{
....
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setTitle("TAB");
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(
bar.newTab().setIcon(R.drawable.icon),
Fragment1.class, null);
mTabsAdapter.addTab(
bar.newTab().setIcon(R.drawable.icon2),
FragmentList.class, null);
}
}
What i need is to replace in the same tab the FragmentList.class with other fragment Class
Thanks in advance!
Well actually you can just replace the name of the class there. Is that what you are trying achieve?
Don't know if this is too late but you can replace fragments dynamically. There's only one catch, you cannot add a fragment to the layout statically in this case. For more details - refer the solution to this StackOverflow post