问题
I'm pretty sure I have tried everything. I have a tabbed activity utilizing 6 fragments within a viewpager. Part of the layout includes a FINISH button within the parent activity, that once clicked should call 6 methods, one in each fragment, from 1-6. For the life of me nothing seems to be able to work. What I have tried:
1) Creating a button onclicklistener for the finish button within each fragment and calling the methods. It only calls the method in the first fragment and does not seem to reach the other fragments.
2) implementing an interface from the parent activity to each fragment, that also only seems to reach the first fragment, leaving the other five fragment methods unexecuted.
3) calling the fragment method from the parent activity using this
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.<specific_function_name>();
because I am using the viewpager I have no way of actually accessing my fragment's ID since I have not been able to set one anywhere.
4) making each fragment method static, that did not make sense and was indicative of me becoming desperate.
Anyone who can help me here I would deeply be grateful.
here is a preview of the parent activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.jff.CreateAccess">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/create_access_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/create_access_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/create_access_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:weightSum="2"
android:layout_marginBottom="8dp">
<Button
android:id="@+id/cancel_create_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
<Button
android:id="@+id/finish_create_delegate_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Finish"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
回答1:
I figured out a solution that seems to work for me so far.
I set the viewpager.setOffScreenOageLimit(6) // # of fragments that I have in use
I then set the number of fragments to an integer value (6)
which I then used to run the following:
for (int i = 0; i < steps; i++)
{
Fragment fragment = getSupportFragmentManager().getFragments().get(i);
switch (i)
{
case 0:
AccessStepOneFragment stepOneFragment = (AccessStepOneFragment) fragment;
stepOneFragment.validate();
break;
case 1:
AccessStepTwoFragment stepTwoFragment = (AccessStepTwoFragment) fragment;
stepTwoFragment.validate();
break;
case 2:
AccessStepThreeFragment stepThreeFragment = (AccessStepThreeFragment) fragment;
stepThreeFragment.validate();
break;
case 3:
AccessStepFourFragment stepFourFragment = (AccessStepFourFragment) fragment;
stepFourFragment.validate();
break;
case 4:
AccessStepFiveFragment stepFiveFragment = (AccessStepFiveFragment) fragment;
stepFiveFragment.validate();
break;
case 5:
AccessStepSixFragment stepSixFragment = (AccessStepSixFragment) fragment;
stepSixFragment.validate();
break;
}
}
and I can successfully call each validate function from their respective fragments. I hope that this will help anyone else who was as lost in the forest about this as I was. It definitely set me back a couple of days.
来源:https://stackoverflow.com/questions/49603506/how-to-trigger-fragment-methods-through-parent-activity-button-onclick-listener