Simple example of ViewPager without tabs - android

I got a xml layout for main activity with some buttons on top of the screens. Below them is placed ViewPager:
<ViewPager android:id="#+id/viewpager">
I also got two fragments in mainactivity.class, each with it's own layout:
frag1 = new Fragment1(); frag2 = new Fragment2(); and viewpager = (ViewPager) findViewById(R.id.viewpager);
Now whats the simplest way to add these two fragments to ViewPager (but without using ActionBar TABS)? I also need to run some code according to which fragment is selected by user, for example make Toast with text "Fragment 2 is selected" when swiped to frag2.
Ok, now I have extended FragmentPagerAdapter: Public class frAdapter extend FragmentPagerAdapter {
public Fragment getItem(int i) { ... }
What I'm supposed to do in getItem() method to add these fragments to viewpager?

Related

Design Issue - Launching new Fragment from TabLayout

I'm not quite sure if there's a "best way" to tackle the following design issue.
I have a Tablayout with 2 Tabs in my MainActivity. Each Tab is a different Fragment. I go to Tab1 and see Fragment1. I need to launch a new Fragment (1A) from Fragment 1 and am not sure the best way to do it? I was thinking about one of these.
A) Take the Tabs out of my MainActivity and place them in a separate MainFragment, which gets launched with the app. That way when the user launches Fragment 1A, it replaces just the 1 MainFragment with the Tabs.
or
B) Keep the Tabs in the MainActivity and find a way to replace Fragment 1 with Fragment 1A when under Tab1.
Any suggestions would be appreciated. Thank you.
I think you shouldn't do both of points... Frag1 visible under Tab1 should contain all the layout (including initialy hidden) and logic for this view. If you need to show smth new it may be smaller (then popup, dialog etc.) or expand some layout, maybe with some animation (you may still use ViewPager inside Fragment inside ViewPager, disable touch events and manipulate scrolling programmatically...).
When Action picked by user is intended to show smth so-much-improtant that previous screen is not needed at all then you should probably open new Activity
PS. If you insist to replace current "screen" (in fact Activitys content) note that title of Tab1 may not representing what contains Frag1A. It very depends what kind of content you have there. You may consider move TabLayout/RecyclerView to e.g. FrameLayout container and add to it you Frag1A covering whole previous view including Tabs. In current design guidelines you can even find suggested solution for way of showing new fragment - with circular reveal animation
I do not understand very well what you want, but possibly this.
Use FragmentPagerAdapter..
public class TabsPagerAdapter extends FragmentPagerAdapter {
private static final int NUM_TABS = 3;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0:
return Tab1Fragment.newInstance();
case 1:
return Tab2Fragment.newInstance();
default:
return Tab3Fragment.newInstance();
}
}
#Override
public int getCount() {
return NUM_TABS;
}
#Override
public CharSequence getPageTitle(int position) {
if (position == 0){
return "Tab 1";
}
if (position == 1){
return "Tab 2";
}
return "Tab 3";
}
In your activity...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Tabs
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
TabsPagerAdapter adapter = new TabsPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabs.setupWithViewPager(pager);
}

Handling the backstack with fragments containing tabs

I'm trying to create an Android application which contains a single activity with a container and a navigation drawer. The initialy empty container loads fragments which has a ViewPager inside a tab layout in which I load a frgment with a FragmentTransaction:
public static void replaceFragmentInContainer(FragmentManager fragmentManager, Fragment fragmentToShow,
boolean addToBackStack)
{
FragmentTransaction transaction = fragmentManager.beginTransaction();
if (addToBackStack)
{
transaction.addToBackStack(null);
}
transaction.replace(R.id.container, fragmentToShow);
transaction.commit();
}
I'm using v4 fragments with a v7 ActionBar in a v7 ActionBarActivity.
Every loaded fragment is a fragment which only loads tabs with other fragments which they hold the actual usability. An example of such tab loading fragment:
public class MainFragment extends TabsFragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View contentView = inflater.inflate(R.layout.fragment_main, container, false);
initTabsLayout(savedInstanceState, contentView, R.id.pager);
addTab("tabspectag1", "", R.drawable.draw1, Fragment1.class, null);
addTab("tabspectag2", "", R.drawable.draw2, Fragment2.class, null);
addTab("tabspectag3", "", R.drawable.draw3, Fragment3.class, null);
return contentView;
}
The problem I'm facing is with the backstack. When a fragment is added to the backstack and I press the back button, the app does go back to the previous fragment like I want to and I see the tabs layout itself, but the content of the tabs is empty like there was nothing loaded to that tab. When it happens, I manage to reload the tab's content only when choosing that screen again with the navigational drawer.
I've tried overriding the onBackPressed in the activity:
#Override
public void onBackPressed()
{
if (getFragmentManager().getBackStackEntryCount() > 0)
{
getFragmentManager().popBackStack();
} else
{
super.onBackPressed();
}
}
but that like I said, it's like I'm getting back to the previous fragment but the tab inside that fragment is not repainting the fragment it had.
What can be done to solve this issue?
Since I DO see the tabs layout of the original fragment in the backstack but not the fragment inside the tab, is it possible I just need to somehow refresh the tab content meaning repaint it? If I can do such a thing then how can I do it?
You didn't post the entire code but, bear in mind that when using fragments inside fragments, the outer fragment should use the childfragmentmanager instead of the regular fragment manager.
If you have an Activity, then you have a fragment that has a Viewpager and inside that viewpager the views are fragments, those outer fragments must use their own childfragmentmanager instead of the activities fragmentmanager.
Activity uses getFragmentManager() to instantiate and show new fragments. Fragments use getChildFragmentManager() to instantiate and show new inner fragments. (fragments inside fragments).
If you always use the same fragmentmanager to handle the transactions the behaviour will be unpredictable.
Your Viewpager should have an TabsAdapter associated that extends from FragmentStatePagerAdapter to show new fragments(and uses the getChildFragmentManager from the fragment instead of the activity).

Android View Pager Adapter shows empty screen

I am implementing Pager Adapter in a Fragment. When I load the screen First time, it works fine. If i switch to other fragment and goes to previous fragment again the it shows empty screen. If I swipe between different tap and move to first tab again then it shows data.
I think on moving back the tabs which are visible didn't load data but once they are out of view during swipe navigation it loads data.
Here is my Pager Adapter:
public class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
case 3:
return new Fragment4();
case 4:
return new Fragment5();
case 5:
return new Fragment6();
}
return null;
}
#Override
public int getCount() {
return 6;
}
}
I am setting my adapter like this:
viewPager = (ViewPager) rootView.findViewById(R.id.my_pager);
mAdapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Just Check if you are using getFragmentManager() to initialize the adapter , Try using getChildFragmentManager() like this :
mAdapter = new MyPagerAdapter(getChildFragmentManager());
viewPager.setAdapter(mAdapter);
Use this,
public class MyPagerAdapter extends android.support.v4.app.FragmentStatePagerAdapter
I was having the same problem and wasted a whole day on this. So I am posting the solution for my issue so that someone else doesn't have to struggle and waste a lot of time.
My problem was that the Fragments inside viewpager were getting invoked(debugger was getting hit) but I was not able to see it in the view(Even for the 1st time).
Issues were:
The parent of the ViewPager was a Fragment.
I had to use getChildFragmentManager() instead of getFragmentManager().
The parent of the Parent Fragment was a NestedScrollView(The activity in which Fragment was populated).
For some reason, even if we keep the height and width of viewpager as matchparent, it was not getting picked up and was defaulted to 0(Even though it was filled in the preview of the xml).
To fix this, we have to add android:fillViewport="true" in your NestedScrollView
Hopefully someone will find this helpfull :)
Because you are not allowed to have nested fragments.
Fragments within Fragments
Change you class to extends FragmentActivity and it should work
My ViewPager is in a Fragment.
I solve this problem by Change FragmentPagerAdapter to FragmentStatePagerAdapter,and also use getFragmentManager().
it's works fine;
use FragmentStatePagerAdapter instead of FragmentPagerAdapter
and add this line after you set the adapter
viewPager.setOffscreenPageLimit(6);\\ add the number of fragments you have
This will remember fragments in memory and it will never show empty screen. But please keep in mind that this will keep each page's data in memory so use it accordingly.

Android Open Fragment over ViewPager

I'm new to android programming and I'm trying to make an app that uses tabs in a viewpager from one main fragmentactivity.
The viewpager and tabs work fine but I want to have an options menu that when an item is selected, opens a completely new fragment, but I don't seem to be able to remove the view pager. I would like to just be able to put the new fragment over the viewpager on the main screen but trying to do that with a fragmenttransaction doesn't seem to work
Any ideas?
Thank you for your time
Ensure that:
1) your Fragment (e.g.: ViewPagerFragment1), which will be selected by the viewpager, has a FrameLayout as root layout with the id "container" e.g:
<FrameLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/container"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout.... and so on
</FrameLayout>
2) Inside the ViewPagerFragment1 class, you have to replace/add the new fragment to the FrameLayout after an action has been triggered. E.g.:
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.selection:
// Create new fragment and transaction
NewFragment newFragment = new NewFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, newFragment, "NewFragment");
transaction.addToBackStack(null);
transaction.commit();
break;
default:
break;
}
}
Well, you will be adding the Fragment to some FrameLayout using the ID of the FrameLayout I assume. Just make sure the FrameLayout you are adding the Fragment to is on top of the ViewPager.
For example if both the ViewPager and FrameLayout are in a RelativeLayout container then make sure the FrameLayout is declared below the ViewPager in the XML. This will stack the FragmeLayout on top of the ViewPager. When the Fragment is added to the FrameLayout it will be drawn on top.

FragmentPagerAdapter getItem is not being triggered

Currently, with a FragmentActivity, I toggle among 2 type of Fragments using the following code.
private void toggle() {
Fragment oldFragment = getSupportFragmentManager().findFragmentById(R.id.content);
Fragment fragment = null;
if (oldFragment instanceof ColorFragment) {
fragment = new ViewPagerFragment();
} else {
fragment = new ColorFragment(android.R.color.black);
}
getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commitAllowingStateLoss();
}
2 Fragments are being toggle.
ColorFragment - A simple fragment which fill up its background with solid black color.
ViewPagerFragment - A fragment contains ViewPager. User can swipe between a purple color fragment, and a blue color fragment.
The code which responsible for swiping purple and blue color fragments are as below.
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 2;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ColorFragment(android.R.color.holo_purple);
default:
return new ColorFragment(android.R.color.holo_blue_bright);
}
}
}
However, I encounter the weird behavior during toggling.
Black color fragment was shown.
Toggling.
View pager, which can swipe between purple and blue fragments shown.
Toggling.
Black color fragment was shown.
Toggling.
Nothing shown, as MyFragmentPagerAdapter's getItem is not being triggered.
I think my situation is similar to FragmentPagerAdapter getItem is not called
However, I prefer not to use FragmentStatePagerAdapter, because of the cost of potentially more overhead when switching between pages.
Any workaround to overcome this problem?
I include a complete workable source code to demonstrate this problem : https://www.dropbox.com/s/jok9tz5ktvfcteo/viewpagerbug.zip
Any workaround to overcome this problem?
I've downloaded your code and the problem appears because you don't handle those Fragments right. Most precisely you use nested Fragments in the ViewPager based Fragment and for that ViewPager you create the adapter like this:
MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(this.getFragmentManager());
Instead, you should be using getChildFragmentManager() to bind the nested fragments:
MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(this.getChildFragmentManager());
Also, you shouldn't pass data through a constructor to a Fragment as that data will not survive a configuration change and bad things will start to appear. Use a Bundle instead.
Global working tested solution.
getSupportFragmentManager() keeps the null reference some times and View pager does not create new fragment instance.Since it finds reference to same fragment. So to over come this use getChildFragmentManager() solves problem in simple way.
Don't
new PagerAdapter(getSupportFragmentManager(), fragments);
Do
new PagerAdapter(getChildFragmentManager() , fragments);
Simple use FragmentStatePagerAdapter instead of FragmentPagerAdapter
or
you can use new MyFragmentPagerAdapter(this.getChildFragmentManager())
Hope it will help you :)
In my case I was correctly calling
MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(this.getChildFragmentManager());
but then in the nested fragment I was trying to replace the container fragment with another one by using:
getFragmentManager()
You need to go to the activity and call
getActivity().getSupportFragmentManager();
In my cases it worked after add this to my FragmentPagerAdapter:
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
and I also used getChildFragmentManager() like Luksprog said

Categories

Resources