D
D
Denis Afonin2014-11-27 21:26:58
Android
Denis Afonin, 2014-11-27 21:26:58

How to connect a third fragment from one fragment to the container of another fragment?

There is a FragmentActivity, to which several Fragments are attached via TabsPagerAdapter. In the first fragment, I created a container so that when the CheckBox is enabled on the second fragment, an additional fragment is displayed in it. I handle the OnCheckedChangeListener() event in the same fragment as the CheckBox itself. When you try to click on the CheckBox, an error occurs: he specified child already has a parent. You must call removeView() on the child's parent first.
MainActivity:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {



  private ViewPager viewPager;
  private TabsPagerAdapter mAdapter;
  private ActionBar actionBar;
    // Tab titles
  private String[] tabs = { "Расчет КВ", "Регулярность движения", "Логи", "Настройки" };

      @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        viewPager.setOffscreenPageLimit(3);
    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);		

    // Adding Tabs
    for (String tab_name : tabs) {
      actionBar.addTab(actionBar.newTab().setText(tab_name)
          .setTabListener(this));
    }

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

      @Override
      public void onPageSelected(int position) {
        // on changing the page
        // make respected tab selected
        actionBar.setSelectedNavigationItem(position);
      }

    });
  }

  @Override
  public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
  }
}

Fragment1_layout
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout">
<LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                <b>android:id="@+id/container"</b>
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true">

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/RealTime"
                android:id="@+id/KVRealTime_tv"
                android:textSize="60dp"
                android:textStyle="bold"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />
</RelativeLayout>

Fragment2
public class Options extends Fragment implements CompoundButton.OnCheckedChangeListener {

    CheckBox EmuEnable;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private FakeSpeed fakespeed;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        manager = getActivity().getFragmentManager();
        fakespeed = new FakeSpeed();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_options, container, false);

        EmuEnable = (CheckBox) v.findViewById(R.id.EmuEnable);
        EmuEnable.setOnCheckedChangeListener(this);
        //EmuEnable.setOnCheckedChangeListener(this);

        return v;
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        transaction = manager.beginTransaction();
        if (EmuEnable.isChecked()){
            transaction.add(R.id.container, fakespeed);

        }
        transaction.commit();
    }
}

Well, the code of the third fragment, which I am trying to dynamically attach to the first one: (I will not attach the layuot code of the file, there are just a few buttons)
public class FakeSpeed extends Fragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_fake_speed, container, true);
        return v;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Afonin, 2014-11-27
@Afdenis

Figured out what was the problem. MainActivity was not connected through the Support library. Changed, now everything works as it should.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question