【发布时间】:2018-09-11 05:04:48
【问题描述】:
我很确定我已经尝试了所有方法。我有一个选项卡式活动,在 viewpager 中使用 6 个片段。部分布局包括父 Activity 中的 FINISH 按钮,单击该按钮后应调用 6 个方法,每个片段中一个,从 1 到 6。对于我的生活来说,似乎没有什么可以工作。 我尝试过的:
1) 在每个片段中为完成按钮创建一个按钮 onclicklistener 并调用方法。它只调用第一个片段中的方法,似乎没有到达其他片段。
2) 实现从父活动到每个片段的接口,该接口似乎也只到达第一个片段,其他五个片段方法未执行。
3) 使用 this 从父活动调用片段方法
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>
【问题讨论】:
标签: java android android-studio android-fragments interface