我很确定我已经尝试了一切。我有一个标签活动,利用一个viewpager中的6个片段。部分布局包括父活动中的FINISH按钮,一旦点击,应该调用6个方法,每个片段一个,从1-6开始。对于我的生活似乎没有任何工作。 我尝试过:
1)为每个片段中的完成按钮创建一个onclicklistener按钮并调用方法。它只调用第一个片段中的方法,似乎没有到达其他片段。
2)实现从父活动到每个片段的接口,这个接口似乎只到达第一个片段,而其他五个片段方法仍未执行。
3)使用此
从父活动调用fragment方法 ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.<specific_function_name>();
因为我使用的是viewpager,所以我无法真正访问我的片段ID,因为我无法在任何地方设置它。
4)使每个片段方法都是静态的,没有意义,并且表明我变得绝望。任何可以帮助我的人都会非常感激。
这是父活动的预览:
<?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>
答案 0 :(得分:0)
我找到了一个似乎对我有用的解决方案。
我设置了viewpager.setOffScreenOageLimit(6)//我正在使用的片段数
然后我将片段数设置为整数值(6)
然后我用它来运行以下内容:
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;
}
}
我可以从各自的片段成功调用每个验证函数。 我希望这能帮助像我一样在森林里迷失的其他人。它肯定让我回来了几天。