Well, I started studying java for android development this year and now I'm interested in browsing tabs that stay on the toolbar. Well, I was able to understand part of the code, but I could not change the content of the fragments. Could someone give me a light?
typelikethis.
MainActivity.java
packagecom.example.gabriel.testenavegaoportab;importandroid.support.design.widget.TabLayout;importandroid.support.design.widget.Snackbar;importandroid.support.v7.app.AppCompatActivity;importandroid.support.v7.widget.Toolbar;importandroid.support.v4.app.Fragment;importandroid.support.v4.app.FragmentManager;importandroid.support.v4.app.FragmentPagerAdapter;importandroid.support.v4.view.ViewPager;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.TextView;publicclassMainActivityextendsAppCompatActivity{privateSectionsPagerAdaptermSectionsPageAdapter;privateViewPagermViewPager;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Toolbartoolbar=(Toolbar)findViewById(R.id.toolbar);setSupportActionBar(toolbar);mSectionsPageAdapter=newSectionsPagerAdapter(getSupportFragmentManager());mViewPager=(ViewPager)findViewById(R.id.container);mViewPager.setAdapter(mSectionsPageAdapter);TabLayouttabLayout=(TabLayout)findViewById(R.id.tabs);mViewPager.addOnPageChangeListener(newTabLayout.TabLayoutOnPageChangeListener(tabLayout));tabLayout.addOnTabSelectedListener(newTabLayout.ViewPagerOnTabSelectedListener(mViewPager));}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){getMenuInflater().inflate(R.menu.menu_main,menu);returntrue;}@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){intid=item.getItemId();if(id==R.id.action_settings){returntrue;}returnonOptionsItemSelected(item);}publicclassSectionsPagerAdapterextendsFragmentPagerAdapter{publicSectionsPagerAdapter(FragmentManagerfragmentManager){super(fragmentManager);}@OverridepublicFragmentgetItem(intposition){returnPlaceholderFragment.newInstance(position+1);}@OverridepublicintgetCount(){//Mostraumtotalde3tabsreturn3;}}publicstaticclassPlaceholderFragmentextendsFragment{privatestaticfinalStringARG_SECTION_NUMBER="section_number";
public PlaceholderFragment(){
}
public static PlaceholderFragment newInstance(int sectionNumber){
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args= new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format,getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
activity_main.xml
<android.support.design.widget.AppBarLayout android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@string/app_name">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="200dp"
app:tabGravity="center"
app:tabSelectedTextColor="@color/colorAccent">
<android.support.design.widget.TabItem
android:id="@+id/tabItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 1" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 2" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 3" />
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.AppBarLayout>
fragment_main.xml - Would that be it?
<TextView android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/constraintlayout"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"/>
</android.support.constraint.ConstraintLayout>