During run time add or remove tabs in Tablayout with Fragmentpageradapter? - android

I have implemented TablLayout with FragmentPagerAdapter, I would like to add or remove Tabs but I am unsure how to implement this. I have searched for this but couldn't find it all other examples are similar but not the same as I am not using an arraylist and quite get my head around the tutorials.
This is the FragmentPagerAdapter
public class TabPagerAdapter extends FragmentPagerAdapter {
int tabCount;
public TabPagerAdapter(FragmentManager fm, int numberOfTabs) {
super(fm);
this.tabCount = numberOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1Fragment tab1 = new Tab1Fragment();
return tab1;
case 1:
Tab2Fragment tab2 = new Tab2Fragment();
return tab2;
case 2:
Tab3Fragment tab3 = new Tab3Fragment();
return tab3;
case 3:
Tab4Fragment tab4 = new Tab4Fragment();
return tab4;
default:
return null;
}
}
#Override
public int getCount() {
return tabCount;
}
}
This is the activity
public class TabLayoutDemoActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_layout_demo);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1 Item").setIcon(android.R.drawable.ic_dialog_email));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2 Item").setIcon(android.R.drawable.ic_btn_speak_now));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3 Item").setIcon(android.R.drawable.ic_lock_idle_low_battery));
tabLayout.addTab(tabLayout.newTab().setText("Tab 4 Item").setIcon(android.R.drawable.ic_dialog_alert));
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new TabPagerAdapter(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_tab_layout_demo, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I am using the support TabLayout and Viewpager , from what I know addition and removal of tab is to be done from the adapter class, so i tried to use this code but I can't figure out what is tablItems here referring to and how to set up this variable or is there any other way to do this ?
public void removeTabPage(int position) {
if (!tabItems.isEmpty() && position<tabItems.size()) {
tabItems.remove(position);
notifyDataSetChanged();
}
}

Related

android studio change title using fragment

i have a problem with changing my title. i can change the title using fragment but each time i click or change to another fragment the title is didnt change to the correct title. here is my code:
This is my main tabactivity
public class TabActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// 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);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#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_tab, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
profile_menu tab1 = new profile_menu();
return tab1;
case 1:
group_menu tab2 = new group_menu();
return tab2;
case 2:
world_menu tab3 = new world_menu();
return tab3;
case 3:
prize_menu tab4 = new prize_menu();
return tab4;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile ";
case 1:
return "Group ";
case 2:
return "World ";
case 3:
return "Prize";
}
return null;
}
}
public void setActionBarTitle(String title){
getSupportActionBar().setTitle(title);
}
}
and this is my fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.prize_menu, container, false);
getActivity().setTitle("Prize");
return rootView;
}
I assume that you want to change the title when tab change?
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:
getSupportActionBar().setTitle("Profile ");
break;
case 1:
getSupportActionBar().setTitle("Group");
break;
case 2:
getSupportActionBar().setTitle("World");
break;
case 3:
getSupportActionBar().setTitle("Prize");
break;
default:
break;
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
if (getActivity().getActionBar() != null) {
getActivity().getActionBar().setTitle("YourTitle");
}
Use this line in onViewCreated() of each fragment will solve your problem
call getActivity().setTitle("Prize"); it in onResume of each fragment
In Fragment
Set your title in simple way. Add this method in your common class.
Add your title with back arrow in action bar.
public void setPageTitle(String title) {
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowCustomEnabled(false);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>"+title+ "</font>"));
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstantState){
view = inflater.inflate(R.layout.fragment_name,container,false);
setPageTitle("title");
return view;
}
If you want title alone, use this line alone in your fragment onCreateView(). But this code does not handle back arrow.
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Title");
Quick way to do:
Declare your ToolBar as static and access from the fragment:
MainActivity:
public static Toolbar mToolbar;
in your fragment:
mToolbar.setTitle("your title here");
You set the Fragment title in the mobile_navigation.xml file in res -> navigation.
Use the android:label=""

Trying to update my tab content

i´m using tablayout and i want to update the tab content when user clicks on the tab. basicly i want to do what the tab does when it is created (check what it is in database and put it on my listview).
here is my MainActivity:
public class MainActivity extends AppCompatActivity{
// Declaring Your View and Variables
Toolbar toolbar;
ViewPager viewPager;
ViewPagerAdapter adapter;
TabLayout tabLayout;
CharSequence Titles[]={"Participantes","Torneio","Classificação"};
int Numboftabs =3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
viewPager = (ViewPager) findViewById(R.id.viewPager);
tabLayout = (TabLayout)findViewById(R.id.tabLayout);
// Creating The Toolbar and setting it as the Toolbar for the activity
setSupportActionBar(toolbar);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
// Assigning ViewPager View and setting the adapter
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorTextIcons));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
adapter.notifyDataSetChanged();
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
viewPager = null;
}
#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, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_info) {
Toast.makeText(getApplicationContext(), "Contacto: Juniortalisson16#gmail.com", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my ViewPageradapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
//This method return the fragment for the every position in the View Pager
#Override
public Fragment getItem(int position) {
switch(position) {// if the position is 0 we are returning the First tab
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
case 2:
Tab3 tab3 = new Tab3();
return tab3;
default:
return null;
}
}
// This method return the titles for the Tabs in the Tab Strip
#Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method return the Number of tabs for the tabs Strip
#Override
public int getCount() {
return NumbOfTabs;
}
}
i tried to use the getItemPosition but it didn´t work, my app crash when i select an tab.
Any ideias? please

Android - Controlling the content of each tab in my tab layout with fragments

I've built an Android app which uses fragments and action bar to create a tab layout. The app is made up of:
MainActivity.java
PagerAdapter.java
Tab1Fragment.java
Tab2Fragment.java
activity_main.xml
fragment_tab_1.xml
fragment_tab_2.xml
I have this working and I can switch between tabs. My question is how do I control the content of each tab? Obviously I can add my objects to each xml layout but where should I be identifying these objects in my java code? Should I be controlling the content in MainActivity.java or within each separate Fragment.java?
MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Questions"));
tabLayout.addTab(tabLayout.newTab().setText("Ask a Question"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(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) {
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);
}
}
Tab1Fragment.java:
public class Tab1Fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab_1, container, false);
}
}
PagerAdapter.java:
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1Fragment tab1 = new Tab1Fragment();
return tab1;
case 1:
Tab2Fragment tab2 = new Tab2Fragment();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
You should reference all of the views within the Fragment code. The views within are defined in that specific Fragment's xml layout file, so the Activity will not be able to reference them easily.
Example:
public class Tab1Fragment extends Fragment {
Button button;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab_1, container, false);
button = (Button) view.findViewById(R.id.button);
//Get all views here
return view; //Must return a view
}
}
I am not clearly understand what you ask but i think you are asking where to code for the specific tabview the answer is in Tab1Fragment and Tab2Fragment.

FragmentPagerAdapter GetItem is never called?

I'm trying to initate a simple tab swipe, But when I'm adding the PagerAdapter the getItem method is never called..
I cant find the reason for this.
Activity:
public class ProfileActivity extends Activity implements ActionBar.TabListener
{
ViewPager viewPager;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_acivity);
viewPager = (ViewPager) findViewById(R.id.profile_view_pager);
ProfilePagerAdapter viewPagerAdapter = new ProfilePagerAdapter(getFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(0);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab friendsTab = actionBar.newTab();
ActionBar.Tab postsTab = actionBar.newTab();
ActionBar.Tab tribesTab = actionBar.newTab();
tribesTab.setText("TRIBES");
postsTab.setText("POSTS");
friendsTab.setText("FRIENDS");
tribesTab.setTabListener(this);
postsTab.setTabListener(this);
friendsTab.setTabListener(this);
actionBar.addTab(friendsTab);
actionBar.addTab(postsTab);
actionBar.addTab(tribesTab);
}
#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_profile_acivity, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
}
}
(I also tried with FragmentActivity Though I do not need the V4 and I'm using the V13)
My adapter:
private class ProfilePagerAdapter extends FragmentPagerAdapter
{
Fragment fragment = new ProfileTribeFragment();
public ProfilePagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int i)
{
Log.i("D","NO :{");
if(i==0)
{
fragment = new ProfileFriendsFragment();
}
if(i==1)
{
fragment = new ProfilePostsFragment();
}
if(i==2)
{
fragment = new ProfileTribeFragment();
}
return fragment;
}
#Override
public int getCount()
{
return 0;
}
}
And all the 3 Fragments look like that:
public class ProfileTribeFragment extends Fragment {
public ProfileTribeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_profile_tribe, container, false);
}
}
I Just cant figure what I'm doing wrong.
The Log.i on the getItem is never seen.
you return Zero on your getCount() so there will be no Item inside your PagerAdapter:
#Override
public int getCount()
{
return 0;
}
Page adapter will bind no page as defined page count inside getCount() but in your case you have return static '0' zero count so pager adapter will not bind any page over there and you not able to seen any page data there. You need to set no page count inside getCount() method like 3 as below example :
#Override
public int getCount(){
return 3; // 3 is no of page count
}
For me nothing worked Then I found the solution - Instead of FragmentPagerAdapter, use FragmentStatePagerAdapter.
Return the number of fragments in getCount() method:
int fragmentCount=3;
#Override
public int getCount()
{
return fragmentCount;
}

How to add swipe in tabs

I am using support library to create action bar in my app. I have added actions in action bar thats working perfect. Now I edit tabs below that. But for changing tabs I have to click on tabs. I want to add swipe in this code. But Its difficult for me as I am taking reference from one link thats only show to add tabs and change them with on click on them. So please someone help me to add swipe from screen to change tabs.
Code-
public class Types extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.types);
setupTabs();
}
private void setupTabs() {
android.support.v7.app.ActionBar ab = getSupportActionBar();
ab.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );
Tab tab = ab.newTab()
.setText( R.string.frag1).setTabListener(new MyTabListener(this, Type1.class.getName()));
ab.addTab(tab);
tab = ab.newTab()
.setText( R.string.frag2).setTabListener(new MyTabListener( this, Type2.class.getName() ) );
ab.addTab(tab);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
homeActivity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void homeActivity() {
Toast.makeText(this, "Home Option Selexted", Toast.LENGTH_SHORT).show();
}
private class MyTabListener implements ActionBar.TabListener
{
private Fragment mFragment;
private final Activity mActivity;
private final String mFragName;
public MyTabListener( Activity activity, String fragName )
{
mActivity = activity;
mFragName = fragName;
}
#Override
public void onTabReselected( Tab tab,
FragmentTransaction ft )
{
}
#Override
public void onTabSelected( Tab tab,
FragmentTransaction ft )
{
mFragment = Fragment.instantiate( mActivity, mFragName );
ft.add( android.R.id.content, mFragment );
}
#Override
public void onTabUnselected( Tab tab, FragmentTransaction ft )
{
ft.remove( mFragment );
mFragment = null;
}
}
Create ViewPagerAdapter class-
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final int PAGES = 4;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Type1();
case 1:
return new Type2();
case 2:
return new Type3();
case 3:
return new Type4();
default:
throw new IllegalArgumentException("The item position should be less or equal to:" + PAGES);
}
}
#Override
public int getCount() {
return PAGES;
}
}
Then in your Typle class-
public class Types extends ActionBarActivity {
private ViewPager viewPager;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.pagerad);
viewPager = (ViewPager) findViewById(R.id.pager);
ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowHomeEnabled(true);
viewPager.setOnPageChangeListener(onPageChangeListener);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
addActionBarTabs();
}
private ViewPager.SimpleOnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
ab.setSelectedNavigationItem(position);
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
homeActivity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void homeActivity() {
Toast.makeText(this, "Home Option Selexted", Toast.LENGTH_SHORT).show();
}
private void addActionBarTabs() {
ab = getSupportActionBar();
String[] tabs = { "TYPE 1", "TYPE 2", "TYPE 3", "TYPE 4" };
for (String tabTitle : tabs) {
ActionBar.Tab tab = ab.newTab().setText(tabTitle).setTabListener(tabListener);
ab.addTab(tab);
}
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
private ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
};
}
Finally create pagerad xml-
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
Then create Each Type fragment like-
public class Type1 extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.type, container, false)
return rootView;
}
}
You need to use the ViewPager.
When implementing the viewpager from the android tutorial you simply need to focus on the getitem method of the SectionsPagerAdapter. Here you could pick the right fragment based on the position of the position of the "view".
Your challenge is that the swipe part of the app is now one activity only, and each view now must be implemented as a fragment. But this layout seems compatible with your current code (eg similar to the onTabSelected method).
You types.xml file should be a ViewPager:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.Types" />
Next, declare a ViewPager and SectionsPagerAdapter in you Types activity:
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
Now, in you OnCreate add:
// Create the adapter that will return a fragment for each swipe screen
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
The SectionsPagerAdapter is implemented as:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
if (position == 2) {
return new FragmentA();
} else if (position == 1) {
return new FragmentB();
} else {
// Pattern from you code...
return Fragment.instantiate( mActivity, mFragName );
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
That's all :)

Categories

Resources