How to hide a TabLayout with BottomNavigationView - java

I have a TabLayout with 3 tabs and a BottonNavigation and I need that at the moment of pressing patients and configuration the tablayout disappears, I do not know what line of code I need?
I also use a ViewPager, everything works well the only thing I need is that the TabLayout disappears when it is in patients. In the Patient Fragment I will use a recycleView and CardView, I want to show the photo of the patient and his data
project image:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
BottomNavigationView mbottomNavigation;
private ViewPager mviewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//bottomNavigation
mbottomNavigation = (BottomNavigationView) findViewById(R.id.bottom_navigation);
mbottomNavigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
//aqui se especifica donde es que se quiere que inicie el bottom
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new AgendaFragment()).commit();
//ViewPager
mviewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(mviewPager);
final BottomNavigationView navigation=(BottomNavigationView)findViewById(R.id.bottom_navigation);
navigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
mviewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
switch (position){
case 0:
navigation.setSelectedItemId(R.id.nav_agenda);
break;
case 1:
navigation.setSelectedItemId(R.id.nav_pacientes);
break;
case 2:
navigation.setSelectedItemId(R.id.nav_configuracion);
break;
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
private OnNavigationItemSelectedListener navigationItemSelectedListener = new OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_agenda:
mviewPager.setCurrentItem(0);
return true;
case R.id.nav_pacientes:
mviewPager.setCurrentItem(1);
return true;
case R.id.nav_configuracion:
mviewPager.setCurrentItem(2);
return true;
}
return false;
}
};
private void setupViewPager(ViewPager mviewPager) {
BottomNavPagerAdapter adapter=new BottomNavPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new AgendaFragment());
adapter.addFragment(new PacientesFragment());
adapter.addFragment(new ConfiguracionFragment());
mviewPager.setAdapter(adapter);
}
}
PatientsFragment.java:
public class AgendaFragment<toolbar> extends Fragment {
private TabLayout tabLayout;
private ViewPager firstViewPager;
public AgendaFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView=inflater.inflate(R.layout.fragment_agenda,container,false);
firstViewPager=(ViewPager)rootView.findViewById(R.id.viewpager_content);
tabLayout=(TabLayout)rootView.findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(firstViewPager);
setupViewPager(firstViewPager);
return rootView;
}
private void setupViewPager(ViewPager viewPager) {
TabViewPagerAdapter adapter = new TabViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new Tab1Fragment(), "Today");
adapter.addFragment(new Tab1Fragment(), "Tomorrow");
adapter.addFragment(new Tab1Fragment(), "Other Date");
viewPager.setAdapter(adapter);
}
}

You could just keep your activity layout with the BottomNavigation and the FrameLayout to show the content of each navigation item. Add the AppBar with the Toolbar and TabBar in your Agenda Fragment layout.

Related

addToBackStack() is not working with getChildFragmentManager()

The new fragment which is displayed on a button click is not closing after pressing the back button,instead it is closing the whole application. The button is present inside one of the three tabs. The code of the mainActivity.java, HeroesFragment(the fragment having the three tabs),StrengthHeroesFragment(one of the three tab having the button) is mentioned below.
MainActivity.java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer=findViewById(R.id.draw_layout);
NavigationView navigationView=findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawer,toolbar,
R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState==null){
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new HeroesFragment()).commit();
navigationView.setCheckedItem(R.id.nav_heroes);}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_heroes:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new HeroesFragment()).commit();
break;
case R.id.nav_items:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new ItemsFragment()).commit();
break;
case R.id.nav_counterpicker:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new CounterPickerFragment()).commit();
break;
case R.id.nav_news:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new NewsFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}
else{
super.onBackPressed();
}
}
}
HeroesFragment.java (The fragment containing the three tabs)
public class HeroesFragment extends Fragment {
private TabLayout tabLayout;
private AppBarLayout appBarLayout;
private ViewPager viewPager;
View view;
RelativeLayout relativeLayout;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_heroes,container,false);
relativeLayout=(RelativeLayout)view.findViewById(R.id.hero_layout);
tabLayout=(TabLayout)view.findViewById(R.id.heroes_tablayout);
appBarLayout=(AppBarLayout)view.findViewById(R.id.hero_appbar);
viewPager=(ViewPager)view.findViewById(R.id.heroes_viewpager);
ViewPageAdapter adapter=new ViewPageAdapter(getChildFragmentManager(),1);
adapter.AddFragment(new StrengthHeroesFragment(),"Strength" );
adapter.AddFragment(new AgilityHeroesFragment(),"Agility");
adapter.AddFragment(new IntelligenceHeroesFragment(),"Intelligence");
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()){
case 0:
Toast.makeText(getContext(),"Tab 1",Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(getContext(),"Tab 2",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getContext(),"tab 3",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
return view;
}
}
StrengthHeroesFragment.java (the fragment contaning the button )
public class StrengthHeroesFragment extends Fragment {
View view;
ImageButton ib1,ib2,ib3,ib4,ib5,ib6;
TextView t1,t2,t3,t4,t5,t6;
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_strength_heroes,container,false);
ib1=(ImageButton)view.findViewById(R.id.abaddon_icon);
ib1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
HeroDetailsFragment heroDetailsFragment=new HeroDetailsFragment();
Bundle data=new Bundle();
data.putString("heroname","Abaddon");
heroDetailsFragment.setArguments(data);
FragmentTransaction transaction=getChildFragmentManager().beginTransaction();
transaction.replace(R.id.strength_hero_layout,heroDetailsFragment).addToBackStack(null);
transaction.commit();
}
});
return view;
}
}
When i use getFragmentManager() instead of getChildFragmentManager() in the StrengthHeroes Fragment it actually works but when i switch to another fragments in the navigation drawer, the content in the tab layout fragments is gone.
I also tried using setPrimaryNavigationFragment() it still didn't worked.
Please help me with it.....
Thanks in Advance.
use below code instead of super.onBackpress()
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}
else{
getSupportFragmentManager().popBackStack();
}
}

How to fix TabIndicator not moving with selected tab

I am using a tablayout to build my application but the tab indicator is not working when I slide within the tabs. Can anyone help me?I have tried adding the addOnTabSelectedListener but that did not work either.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private PagerAdapter pagerAdapter;
private DrawerLayout drawerLayout;
private String[] pageTitle = {"myPlanner", "News", "Parking"};
private String studyRooms = "http://library2.csumb.edu/mrbs/mobilenow.php";
public static String MYPREFERENCE = "myPref";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
setSupportActionBar(toolbar);
//create default navigation drawer toggle
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i < 3; i++) {
tabLayout.addTab(tabLayout.newTab().setText(pageTitle[i]));
}
//set gravity for tab bar
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//handling navigation view item event
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
assert navigationView != null;
navigationView.setNavigationItemSelectedListener(this);
viewPager = (ViewPager)findViewById(R.id.view_pager);
pagerAdapter = new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(pagerAdapter);
//setting Tab layout (number of Tabs = number of ViewPager pages)
//viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Fragment frag = pagerAdapter.fragments[position];
if(frag != null && frag instanceof MapsFragment){
Toast.makeText(MainActivity.this, "On page Selected", Toast.LENGTH_LONG).show();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
// viewPager.setOffscreenPageLimit(3);
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
}
class PagerAdapter extends FragmentPagerAdapter{
String tabTitles[] = new String[]{"myPlanner", "News", "Parking"};
public Fragment[] fragments = new Fragment[tabTitles.length];
Context context;
public PagerAdapter(FragmentManager fm, Context context){
super(fm);
this.context = context;
}
#Override
public int getCount(){
return tabTitles.length;
}
#Override
public Fragment getItem(int position){
switch (position){
case 0:
return new myPlanner();
case 1:
return new News();
case 2:
return new MapsFragment();
}
return null;
}
#Override
public CharSequence getPageTitle(int position){
//Generate title based on item position
return tabTitles[position];
}
#Override
public Object instantiateItem(ViewGroup container, int position){
Fragment createdFragment = (Fragment)super.instantiateItem(container,position);
fragments[position] = createdFragment;
return createdFragment;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[],int[] grantResults){
if(requestCode == MapsFragment.MY_PERMISSIONS_REQUEST_LOCATION){
MapsFragment mapFragment = (MapsFragment) pagerAdapter.fragments[2];
if(mapFragment != null){
mapFragment.onRequestPermissionsResult(requestCode,permissions,grantResults);
}
}
else{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
remove this:
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
And add:
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Also u should call tabLayout.setupWithViewPager(viewPager); after u set up the viewpager
viewPager = (ViewPager)findViewById(R.id.view_pager);
pagerAdapter = new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);

How to implement void OnFragmentInteractionListener?

When I launch my app I get following exception:
java.lang.RuntimeException: com.demo.mtin.mtin.MainActivity must implement OnFragmentInteractionListener
Can someone help me with this?
What code I need to add in MainActivity and where?
I have two blank fragments: PopularFragment and RatedFragment
MainActivity bellow:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private MoviesAdapter mAdapter;
#Override
protected void onResume() {
SharedPreferences sharedPref = getBaseContext().getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
String order = sharedPref.getString("order", "popular");
if (order.equals("popular")) {
getPopularMovies();
} else {
getTopRatedMovies();
}
super.onResume();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// uci i postaviti order -> popular
Log.d("NESTO", "NESTO-onCreate");
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
}
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PageAdapter adapter = new PageAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#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_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
This is actually a pretty straight-forward error message, go for this:
public class MainActivity extends AppCompatActivity implements PopularFragment.OnFragmentInteractionListener, RatedFragment.OnFragmentInteractionListener { ...
#Override
public void onFragmentInteraction(Uri uri) {
}}
You must indicate that your activity implement the fragment's interface, then implement it (can be void though).
And have the following in your fragments :
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}

PreferenceFragment with AppCompatActivity Android

I'm trying to create a Preferencefragment or a PreferenceActivity using the tabs. Actually i can't do it and my code is simply:
The MainActivity
public class MainActivity extends AppCompatActivity {
private ViewPagerAdapter mSectionsPagerAdapter;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FirstFragment(), "ONE");
adapter.addFragment(new SecondFragment(), "TWO");
adapter.addFragment(new ThirdFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
and one of the fragment
public class FirstFragment extends Fragment{
public FirstFragment () {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
return view;
}
}
As you can see it's empty. Now, i need replace the fragments with the preferencefragment. But i can't understand how implement them with tabs. How can i do it?
Not too long ago, this would not have been possible, due to PreferenceFragments not working well with the support library. With the latest version of the support v7 library, we now have the PreferenceFragmentCompat class, which will work in an AppCompatActivity, and will also work with your ViewPager and FragmentPagerAdapter.
Since you're using support library fragments with android.support.v4.app.FragmentPagerAdapter, this is the only option with your current configuration.
Note that it is possible to use a TabLayout with non-support fragments, however you have to use the android.support.v13.app.FragmentPagerAdapter class, which does allow use of non-support fragments.
So to keep your current configuration and make it work, make your PreferenceFragment extend PreferenceFragmentCompat:
import android.os.Bundle;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.view.View;
public class MySettingsPrefFragment extends PreferenceFragmentCompat {
public MySettingsPrefFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_settings_pref);
}
#Override
public void onCreatePreferences(Bundle bundle, String s) {
}
}
Add a new tab that corresponds to your PreferenceFragment:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FirstFragment(), "ONE");
adapter.addFragment(new SecondFragment(), "TWO");
adapter.addFragment(new ThirdFragment(), "THREE");
adapter.addFragment(new MySettingsPrefFragment(), "SETTINGS"); //added
viewPager.setAdapter(adapter);
}
And, that should be it!

when i select the contacts tab the tab bar is vanished

i have four tabs in the tab bar when i select the contact tab from the tab bar the tab bar is disappeared.i want to show the tab bar when i select the contact tab.
Here is code
TabActivity.java
public class Tab_Activity extends TabActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources obj_res = getResources();
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec m_tab1 = tabHost.newTabSpec("First Tab");
TabSpec m_tab2 = tabHost.newTabSpec("Second Tab");
Intent intent1 = new Intent().setClass(this, Phone.class);
Intent intent3 = new Intent().setClass(this, Settings.class);
Intent intent4 = new Intent().setClass(this, Contact.class);
m_tab4.setIndicator("Tab4");
m_tab4=tabHost.newTabSpec("tab4").setIndicator("", obj_res.getDrawable(R.drawable.pic1)).setContent(intent3);
m_tab5.setIndicator("Tab4");
m_tab5=tabHost.newTabSpec("tab4").setIndicator("", obj_res.getDrawable(R.drawable.pic3)).setContent(intent4);
tabHost.addTab(m_tab2);
tabHost.addTab(m_tab4);
tabHost.addTab(m_tab5);
}}
Contact.java
public class Contact extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
//setContentView(R.layout.contact);
Intent Contacts = new Intent(Intent.ACTION_GET_CONTENT);
Contacts.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Contacts.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(Contacts, 1);
}}
This is your activity in which u can put how many tab you want to display.
public class GuestListAdminActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Bride", "Groom", "Create Gueset List" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guest_list_admin);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
getActionBar().setHomeButtonEnabled(true);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
This is pager adapter which will show your all fragments.
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new BrideGuestList();
case 1:
return new GroomGuestList();
case 2:
return new NewGuestListActivity();
default:
break;
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
And make your fragment activity like following
public class BrideGuestList extends android.support.v4.app.Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bride_guest_list, container, false);
return rootView;
}

Categories