Search code examples
androidandroid-activityandroid-fragmentsandroid-studio

Problems with multiple fragments connected to activity


ANSWER: Okey great thx from the master here uDevel convincing me that I was going a very wrong way. I have found a similar problem that saved my ass and took me about 10 to get my shit to gather... here you go: How to implement a ViewPager with different Fragments / Layouts

PROBLEM:

I know there a few question about fragmenst here already but I didn´t find my answer there.

I am trying to connect three fragments to CalendarActivity. I think it's best to explain in code.

THE ERROR I GET IS: http://postimg.org/image/aca9ndfdb/ I have tried many different imports but I can't image how to implement this so would appreciate some help. If I remove android.app when I create a fragmentMananger/fragmentTransaction I get: http://postimg.org/image/dy2av2zp9/

Here is CalendarActicity

package ru.calendar;

import android.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import jBerry.MySugar.R;

public class CalendarActivity extends FragmentActivity implements TabListener  {

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_calendar);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        FragmentA frag = new FragmentA();
        fragmentTransaction.replace(android.R.id.content, frag);

        fragmentTransaction.commit();

        ActionBar actionBar;
        ViewPager viewPager;

        viewPager = (ViewPager) findViewById(R.id.calendarContainer);
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                actionBar.setSelectedNavigationItem(arg0);
                // Intent intent = new Intent(this, CalendarActivity.class);
                // startActivity(intent);
                Log.d("DpoiNT", "onPageSelected at " + " position " + arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                //    Log.d("DpoiNT", "onPageScrolled at "+" position " +arg0+" from " +arg1+" with number of pixels "+arg2);
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if(arg0== ViewPager.SCROLL_STATE_IDLE){
                    Log.d("DpoiNT", "onPageScrollStateChanged Idle");
                }
                if(arg0== ViewPager.SCROLL_STATE_DRAGGING){
                    Log.d("DpoiNT", "onPageScrollStateChanged Dragging");
                }
                if(arg0== ViewPager.SCROLL_STATE_SETTLING){
                    Log.d("DpoiNT", "onPageScrollStateChanged Settling");
                }
            }
        });

    actionBar = getActionBar();
    assert actionBar != null;
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1=actionBar.newTab();
    tab1.setText("Tab 1");
    tab1.setTabListener(this);

    ActionBar.Tab tab2=actionBar.newTab();
    tab2.setText("Tab 2");
    tab2.setTabListener(this);

    ActionBar.Tab tab3=actionBar.newTab();
    tab3.setText("Tab 3");
    tab3.setTabListener(this);

    actionBar.addTab(tab1);
    actionBar.addTab(tab2);
    actionBar.addTab(tab3);
}


    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // Log.d("DpoiNT", "onTabSelected at "+" position " +tab.getPosition()+" name "+tab.getText());
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // Log.d("DpoiNT", "onTabUnselected at "+" position " +tab.getPosition()+" name "+tab.getText());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // Log.d("DpoiNT", "onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());
    }
}


class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        Fragment fragment=null;
        if(arg0 == 0) {
            fragment = new FragmentA();
        }

        if(arg0 == 1) {
        fragment = new FragmentB();
        }

        if(arg0 == 2) {
            fragment = new FragmentC();
        }

        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

Here is the xml for CalendarActivity (activity_calendar):

<android.support.v4.view.ViewPager
<!-- ViewPager for swipe function in CalendarActivity -->

    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"
    tools:context="ru.calendar.CalendarActivity"
    android:id="@+id/calendarContainer">

    <fragment
        android:id="@+id/fragment_a"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="#ffffff"
        class="ru.calendar.FragmentA" />

    <fragment
        android:id="@+id/fragment_b"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        class="ru.calendar.FragmentB" />

    <fragment
        android:id="@+id/fragment_c"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        class="ru.calendar.FragmentC" />

</android.support.v4.view.ViewPager>

And this is how one fragment.xml looks like:

public class FragmentA extends Fragment {

    public FragmentA() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a, container, false);
    }
}

EDIT This a part of CalendarActivity below...

    viewPager = (ViewPager) findViewById(R.id.calendarContainer);
    viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            actionBar.setSelectedNavigationItem(arg0);
            // Intent intent = new Intent(this, CalendarActivity.class);
            // startActivity(intent);
            Log.d("DpoiNT", "onPageSelected at " + " position " + arg0);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            //    Log.d("DpoiNT", "onPageScrolled at "+" position " +arg0+" from " +arg1+" with number of pixels "+arg2);
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            if(arg0== ViewPager.SCROLL_STATE_IDLE){
                Log.d("DpoiNT", "onPageScrollStateChanged Idle");
            }
            if(arg0== ViewPager.SCROLL_STATE_DRAGGING){
                Log.d("DpoiNT", "onPageScrollStateChanged Dragging");
            }
            if(arg0== ViewPager.SCROLL_STATE_SETTLING){
                Log.d("DpoiNT", "onPageScrollStateChanged Settling");
            }
        }
    });


class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        Fragment fragment=null;
        if(arg0 == 0) {
            fragment = new FragmentA();
        }

        if(arg0 == 1) {
            fragment = new FragmentB();
        }

        if(arg0 == 2) {
            fragment = new FragmentC();
        }

        return fragment;
    }

    @Override
    public int getCount() {
    return 3;
    }
}

Solution

  • This is not how you do view pager with fragment; you need use PagerAdapter, and you don't define your fragment in xml, it should be dynamically instantiated in code by the adapter.

    http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html http://developer.android.com/training/animation/screen-slide.html

    This part of code is only used for default layout and single fragment.

    android.app.FragmentManager fragmentManager = getFragmentManager();
    android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    FragmentA frag = new FragmentA();
    fragmentTransaction.replace(android.R.id.content, frag);
    
    fragmentTransaction.commit();
    

    Just follow the guide for view pager.

    Or if you want to have more than 1 fragment in same activity, and on same screen at the same time, then you don't use view pager. You need a root viewgroup with few viewgroups with id in the main xml and use the code you got above.

    ...
    fragmentTransaction.replace([ID of the view group you want your fragment to be in], frag);
    ...
    

    You have make up your mind what you want to do first.