【问题标题】:How to trigger fragment methods through parent activity button onclick listener如何通过父活动按钮onclick监听器触发片段方法
【发布时间】: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


    【解决方案1】:

    我想出了一个到目前为止似乎对我有用的解决方案。

    我设置了 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;
            }
        }
    

    我可以从各自的片段中成功调用每个验证函数。 我希望这将帮助其他像我一样迷失在森林中的人。这肯定让我倒退了几天。

    【讨论】:

    • 你把这个放在哪里了?在 FragmentPage 适配器中?
    • @Demonic218 我在父活动中设置了!希望有帮助
    猜你喜欢
    • 1970-01-01
    • 2015-05-16
    • 2019-05-27
    • 2017-07-13
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多