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
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
I have a class that is SherlockFragmentActivity and inside it two SherlockFragment.
When I enter the class it automatically puts me inside the first tab (Tab1 "FragmentTab1"), and I can go to the other tab (Tab2 "FrgmentTab2") by clicking on the tab up. How can I disable a tab?
I want to disable Tab2(bbb) from Tab1(aaa).
my main class code:
public class MainClass extends SherlockFragmentActivity {
ActionBar.Tab Tab1,Tab2;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new FragmentTab2();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mm);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar actionBar = getSupportActionBar();
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setText("aaa");
Tab2 = actionBar.newTab().setText("bbb");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
}
My tabs codes (both classes have same clean-empty code right now)
public class FragmentTab1 extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab2, container, false);
return rootView;
}
}
For further explanation please ask.
For removing tab from tabbar, you can use
getActivity().getSupportActionBar().removeTabAt(index)
If you want to disable touch events on that particular tab, you need to create custom view for this Tab a disable focus on that view.
EDIT
Sory, forgot to mention. If you are using API version>=11, you can use
getActivity().getActionBar()
If you are using support lib for compatability, e.g. ActionBarSherlock or appCompat you need to cast it first, in your case
((SherlockFragmentActivity) getActivity()).getSupportActionBar()
I am studying on the ActionBar and saw this abstract method
public abstract Tab newTab();
However the implementation of newTab is only seen in ActionBarImpl.java.
From android http://developer.android.com/reference/android/support/v4/view/ViewPager.html
It is showed that
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"), CountingFragment.class, null);
So, where is the bar.newTab() actually instantiated?
I saw a related post What class should I extend, AcionBar or ActionBarImpl? but it doesn't seem to answer my question directly.
The ActionBar class is an abstract class, like you already found out. It's actually implemented in the ActionBarImp class. Everything related to the ActionBar will be instantiated inside the Activity class. getActionBar() will return this implementation of the ActionBar:
/**
* Retrieve a reference to this activity's ActionBar.
*
* #return The Activity's ActionBar, or null if it does not have one.
*/
public ActionBar getActionBar() {
initActionBar();
return mActionBar;
}
/**
* Creates a new ActionBar, locates the inflated ActionBarView,
* initializes the ActionBar with the view, and sets mActionBar.
*/
private void initActionBar() {
[...]
mActionBar = new ActionBarImpl(this);
[...]
}
Source.
I'm using SupportLibrary. Here is my MainActivity code:
public class MainActivity extends ActionBarActivity implements android.support.v7.app.ActionBar.TabListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.support.v7.app.ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
android.support.v7.app.ActionBar.Tab tab = bar.newTab();
tab.setText("Tab 1");
tab.setTabListener(this);
bar.addTab(tab);
}
How can I display some activity in the tab?
Thanks.
How can I display some activity in the tab?
You can't. First, that technique was deprecated three years ago. Second, ActionBarActivity does not extend the deprecated ActivityGroup.
You are welcome to use fragments for your tabs, though.
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.