I have a FragmentActivity and some fragment in my app.
i need to have Swip ActionBar-Tabs Just inside one fragment , but when i use ActionBar and ViewPager in my project to make Swip ActionBar Tab , make ActionBar in all of fragment No just in one,
hove i can make ActionBar just inside one fragment that not shown in other fragment?
actionabar = getActionBar();
actionabar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
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);
actionabar.setSelectedNavigationItem(position);
}
};
/** Setting the pageChange listner to the viewPager */
viewpager.setOnPageChangeListener(pageChangeListener);
/** Creating an instance of FragmentPagerAdapter */
MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);
viewpager.setAdapter(fragmentPagerAdapter);
actionabar.setDisplayShowTitleEnabled(true);
Tab tab = actionabar.newTab().setText("My Friends").setTabListener(tabListener);
actionabar.addTab(tab);
tab = actionabar.newTab().setText("Android Version").setTabListener(tabListener);
actionabar.addTab(tab);
tab = actionabar.newTab().setText("Android Phones").setTabListener(tabListener);
actionabar.addTab(tab);
Use TabHost (http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html) instead of ActionBar navigation mode.
Related
in an android app, I use a TabLayout to display multiple Fragments in a single activity.
I want the tabs to be labeled with icons and a two line heading.
In the Activity's onCreate I initialise the SectionsPageAdapter and the TabLayout like:
private SectionsPagerAdapter mSectionsPagerAdapter;
protected void onCreate(Bundle savedInstanceState) {
...
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new
SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
// add a PageChangeListener to be able to detect which tab is activated
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
...
});
...
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
//Set icons for tabs
tabLayout.getTabAt(0).setIcon(R.drawable.ic0);
tabLayout.getTabAt(1).setIcon(R.drawable.ic1);
tabLayout.getTabAt(2).setIcon(R.drawable.ic2);
}
And in the SectionpagerAdapter I define the PageTitles using:
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "First \nTab";
case 1:
return "Second \nTab";
case 2:
return "Third \nTab";
}
return null;
}
But when i run the app, the PageTitles aren't broken in two lines, but shortened with ... to fit the screen.
How to enable multiline PageTitles for the Tabs?
tabLayout.getTabAt(i).setCustomView(yuorTabView);
And you can customize your own TabView
Right now, I have five fragments (tabs) in my app as the image shows. And they are all listed as members of the tab bar in the MainActivity.java
public class MainActivity extends AppCompatActivity implements InputTab.SendMessage, FollowingTab.SendMessage, FollowerTab.SendMessage, ProfileTab.SendMessage {
private SectionsPageAdapter pageAdapter;
ViewPager viewPager;
public static String currentUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new ProfileTab());
fragmentTransaction.commit();
pageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
// Sets up the ViewPager with the sections adapter
viewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager((viewPager));
}
// Adds fragments to SectionsPageAdapter and gives names for the corresponding tab
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new InputTab(), "Search");
adapter.addFragment(new ProfileTab(), "Profile");
adapter.addFragment(new GithubTab(), "Github Repos");
adapter.addFragment(new FollowerTab(), "Followers");
adapter.addFragment(new FollowingTab(), "Followings");
viewPager.setAdapter(adapter);
}
Since I implemented a search tab, I was wondering if it's possible to create another page(or fragment) that displays the result of the search, and when I click the result, I want to make it direct to the corresponding tabs (Profile for users, and Github Repos for repo search).
In short, how can I create a page that is not part of the tab and still display it when I press the 'search' button?
Implement your TabLayout and ViewPager inside a fragment instead of the activiry and replace that fragment with the results fragment.
I'm working in an application that uses SlidingTabLayout and SlidingTabStrip and what I want to do is handle when I select a tab and the fragment is changed.
I have read something about ViewPager.OnPageChangeListener and ViewPager.SimpleOnPageChangeListener and tried both, but none of them seems to work and don't know why.
Did you set the ViewPager for your SlidingTabLayout?
//Get reference to SlidingTabLayout and ViewPager from layout
mSlidingTabLayout.setViewPager(mPager);
ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
pageSelected = position;
}
};
mSlidingTabLayout.setOnPageChangeListener(pageChangeListener);
mPager.setCurrentItem(pageSelected);
mPager.setAdapter(mAdapter); //Set your FragmentPagerAdapter
I implemented view pager in my activity like :
class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "FIRST", "SECOND", "THIRD" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
getActionBar().setHomeButtonEnabled(false);
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
getActionBar().addTab(getActionBar().newTab().setText(tab_name)
.setTabListener(this));
}
but when I swipe between tabs it never call the oncreateview of THIRD fragment but it actually call the oncreateview of THIRD fragment when I swipe to the SECOND fragment (From first to Second) and the oncreate view of second fragment get call along with the first fragment.
I don't understand what's wrong with the flow.
There is nothing wrong with this. when you swipe to new page in viewpager, it automatically loads the two neighbor fragments of current visible fragment. why? because of having better UX.
Can I prevent it from doing that?
No.
Can I extend it to load more neighbor fragments?
yes, setOffscreenPageLimit
I want to create a feed like the one in the facebook app, and I want to have actionbarsherlock navigation tabs that will be used to filter the feed - a tab for people, a tab for places and a tab for items.
Right now I've set it up so that each tab opens up a fragment. How can I make them open up a list view instead?
ActionBar.Tab itemsFeedTab = actionBar.newTab();
ActionBar.Tab peopleFeedTab = actionBar.newTab();
ActionBar.Tab placesFeedTab = actionBar.newTab();
Fragment itemsFeedFragment = new FeedItems();
Fragment peopleFeedFragment = new FeedPeople();
Fragment placesFeedFragment = new FeedPlaces();
itemsFeedTab.setTabListener(new MyTabsListener(itemsFeedFragment));
peopleFeedTab.setTabListener(new MyTabsListener(peopleFeedFragment));
placesFeedTab.setTabListener(new MyTabsListener(placesFeedFragment));
actionBar.addTab(itemsFeedTab, 0, true);
actionBar.addTab(peopleFeedTab, 1, false);
actionBar.addTab(placesFeedTab, 2, false);
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment){
this.fragment = fragment;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.replace(R.id.home, fragment);
}
If your fragments contain only ListViews then its better to use ListFragment
Well the answer is hidden in your question , you are trying to open ListView instead of Fragment.
Why don't you open a ListFragment and you are done.