I am new to Android development. I have a an Activity that utilizes FragmentPagerAdapter to create tabs. Everything works fine except I want each tab to have its own unique fragment.xml file and right now all tabs display the same fragment (fragment_home.xml).
I have a suspicion the problem lies in this code:
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
TextView textView = (TextView) rootView
.findViewById(R.id.section_label);
textView.setText(Integer.toString(getArguments()
.getInt(ARG_SECTION_NUMBER)));
return rootView;
}
EDIT 1
I have added another Fragment class to represent another Fragment. It has its own OnCreateView() method which inflates an XML file called fragment_overview.xml and that works fine.
Now I am still stuck with my problem of wanting both fragments to be present in different tabs.
The code below creates an instance of OverViewFragment in its getItem() method. There is only one fragment returned so how do I make it multiple fragments?
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.
// Return a PlaceholderFragment (defined as a static inner class
// below).
return OverviewFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 5;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.tab1).toUpperCase(l);
case 1:
return getString(R.string.tab2).toUpperCase(l);
}
return null;
}
}
Hi you have to create seprate fragments for each tab and link with your tab position. You can refer following repository for learn how to do it.
FragmentPageAdapter With Tabs
Related
So currently my code can move between objects from the same fragment, but I want to move between different fragments that have different layouts.What code do I need to add to viewpager to make it work? Do I need to make use of a FragentManager? Can anyone guide me on how to go about it? Thanks.
Below if my code:
ScreenSlidePagerActivity.java
public class ScreenSlidePagerActivity extends FragmentActivity {
private static final int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter pagerAdapter;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slide_screen_viewpager);
//declare viewpager and pageradapter
mPager = findViewById(R.id.ViewPageSlide);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(pagerAdapter);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0){
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
}
else {
mPager.setCurrentItem(mPager.getCurrentItem() -1 );
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position) {
return new ScreenSlidePageFragment();
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
ScreenSlidePageFragment.java
public class ScreenSlidePageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.slide_content_page, container, false
);
return rootView;
}
}
You have only one class i.e.,ScreenSlidePageFragment that extends fragments. If you want different layouts for that, its better if you create different classes that inflates different layouts. eg: if you want two layouts, create two classes and both classes should inflate different layouts. The changes need to be done are :
//inside ScreenSlidePagerAdapter
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ScreenSlidePageFragment();
case 1:
return new NewClass();
//and so on
}
}
You have to create the new Class similar to ScreenSlidePageFragment. The only change is inflate a different layout.
public class NewClass extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.new_layout, container, false
);
return rootView;
}
}
You can create a new_layout similar to slide_content_page and customize it as you want. You can also increase the no of fragment objects and layout as you wish.
But a new way of doing this things has come. Its better if you extend FragmentStateAdapter instead of FragmentStatePagerAdapter. This is more easy and efficient. You have to override createFragment in this case instead of getItem. Ignore of you are okay with it.
Hope this is the question you have asked and this helps. Thankyou.
In Dashboard Activity there is a Fragment on top which contains 3 fragments as view pagers inside it.The view pagers become blank and unresponsive (as shown in 2nd screenshot) in these cases:
When app is loaded from backstack
When font setting is changed while app is in background and called again from app stack
When the notification of my app is clicked and the click action redirects to the Dashboard Activity
The code for Fragment for tabs is:
public class MainActivityGraphViewPager extends Fragment {
public MainActivityGraphViewPager() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_main_activity_graph_pager, container, false);
ViewPager viewPager = v.findViewById(R.id.fgraph_pager);
TabLayout tabx = v.findViewById(R.id.tab_layout_dashboard);
tabx.setupWithViewPager(viewPager, true);
viewPager.setAdapter(new SectionPagerAdapterFeaturedgraphs(getActivity().getSupportFragmentManager()));
viewPager.setCurrentItem(1, false);
viewPager.setOffscreenPageLimit(3);
return v;
}
private class SectionPagerAdapterFeaturedgraphs extends FragmentPagerAdapter {
public SectionPagerAdapterFeaturedgraphs(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ExpenseManagerPieChart();
case 1:
return new BudgetandFitness();
case 2:
return new ExpendsFragments();
default:
return null;
}
}
#Override
public int getCount() {
return 3;
}
}}
The MainActivityGraphViewPager.class is called in Dashboard Activity's onCreate():
MainActivityGraphViewPager mainActivityGraphPager=new MainActivityGraphViewPager();
this.getSupportFragmentManager().beginTransaction().replace(R.id.graph_pagers, mainActivityGraphPager).commit();
What can be the possible solutions to remove this unresponsiveness?
I have an app that uses SectionsPagerAdapter to show 3 fragments within an activity (this part was automatically generated by Android Studio after selecting a tabbed activity). Right now, this is how I select what fragment to show:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position == 0) {
return new Fragment1();
} else if (position == 1) {
return new Fragment2();
} else {
return new Fragment3();
}
}
#Override
public int getCount() {
return 3;
}
}
I would like my app to start on Fragment 2 (i.e. position 1), to allow the user to either swipe right to go back to the first fragment, or to swipe left to move on to the next fragment.
How can I set the position to start at? I couldn't find any method to set the position number, is there one or should I do this another way?
If it helps, this is my onCreateView method of my MainActivity class:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
I don't think an Activity has onCreateView method. Its a method of a fragment. Your activity will have public void onCreate(Bundle savedInstanceState)
If my assumptions are not wrong, in the layout of your main activity you will have a android.support.v4.view.ViewPager.
and in your code you might declare your view pager variable and set the SectionsPagerAdapter to it. After that just do viewPager.setCurrentItem(1);
How can I set the position to start at?
Call setCurrentItem() on the ViewPager.
I'm trying to create tabs with swipe view using fragment. I found a solution but it provides a single dummy text layout for each tabs instead of using separated layout. I'd like to create three different classes with three different layouts and use them in tabs and if possible then using a custom background for each tabs (while selected/not selected). I can do all these things without fragment but using fragment it seems challenging.
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* 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) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#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;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
To achieve your target you must use ActionBarSherlock library, and to call other fragments you should not need to use Intent,
I have written same kind of program few months ago to call different Fragments i used below code
#Override
public Fragment getItem(int arg0) {
switch (arg0) {
// Open FragmentTab1.java
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
FragmentTab3 fragmenttab3 = new FragmentTab3();
return fragmenttab3;
}
return null;
}
and to call show different - different layouts for each and every Fragment you should call different - different Fragments and also need to write XMLs too .....
FragmentTab1.java :-
public class FragmentTab1 extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.fragment1, container, false);
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
}
fragment1.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Your Layout Goes Here for Fragment1" />
</RelativeLayout>
in a same way you have to write classes and xmls for all needed Fragments....
for more detailed information or to try out an example, use this :
http://www.androidbegin.com/tutorial/android-actionbarsherlock-viewpager-tabs-tutorial/
in the following link that help you how to create Tab fragment in sperated intents:
Tab Fragments
I want to create a screen with 4 tabs. But initially only first tab have to be seen.
Based on an action in the content ( which is a fragment) I need to create a new tab with different layout. Consider this an application as a form form applying to something. So tabs represents Steps 1,2,3,4. So once I complete the Step 1 I will click on a button which creates a new tab Step2.
I don't want to implement it by launching activities because I need to maintain previous tabs. So how do I catch a button click in a fragment and add a tab at runtime?
create a new tab with different layout
See this Example
How do you set Android ViewPager to encompass only one View or Layout?
The question is not related to you but Example is Valid and related to One part of your question
EDITED
This is how you can link fragments with your tabs.
class TestFragmentAdapter extends FragmentPagerAdapter {
private int mCount = TABS.length;
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
MainSummary f1 = new MainSummary();
MainActivity f2 = new MainActivity();
if(TABS[position].equals("Summary")){
return f1;
}else if(TABS[position].equals("Activity")){
return f2;
}else{
return null;
}
}
#Override
public int getCount() {
return mCount;
}
#Override
public CharSequence getPageTitle(int position) {
return TABS[position];
}
}
And for each fragment you can have separate class and layout (R.layout.main_summary) like this
public final class MainSummary extends ListFragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.main_summary, container, false);
return view;
}
}
There are some useful sample projects here https://github.com/github/android
You can find all answers of your question if you study these samples