【问题标题】:How to get data from fragments in the parent activity?如何从父活动中的片段中获取数据?
【发布时间】:2019-09-20 19:53:10
【问题描述】:

这是我的父 Activity,包含选项卡布局和视图分页器以及底部的按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context=".module.addcontact.AddContactActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.PopupOverlay">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabTextColor="@color/colorDarkBlue"
            app:tabSelectedTextColor="@color/colorDarkBlue"
            android:background="@color/white"/>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/save"
        android:text="SAVE"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:layout_margin="10dp"
        android:background="@color/colorDarkBlue"/>

</LinearLayout>

片段 1:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:fillViewport="true"
    android:padding="@dimen/add_contacts_padding"
    tools:context=".module.addcompany.AddCompanyFragment">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="1">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:hint="Name"
        android:textSize="24sp"
        android:padding="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/edit_text_border"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/phone"
        android:hint="Phone"
        android:inputType="phone"
        android:textSize="24sp"
        android:padding="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/edit_text_border"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/address"
        android:hint="Address"
        android:textSize="24sp"
        android:padding="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/edit_text_border"/>

    </TableLayout>

</ScrollView>

片段 2 类似于片段 1(具有不同的编辑文本,例如名称和电子邮件)

当我的父活动中的保存按钮被按下时,我想从父活动中的这两个片段中获取所有数据,如果缺少某些字段,那么我想为该字段设置错误。

如何从片段中获取数据到父活动? 或者 有什么方法可以获取父活动中片段的所有编辑文本(字段)?

【问题讨论】:

  • 你在Activity的布局文件中包含Fragment的布局在哪里?
  • @SteliosPapamichail 先生,我正在用 java 代码中的 viewpager 附加我的片段
  • 活动启动时,您已经拥有所有片段的实例。现在,您必须在片段中创建用于访问数据的方法,并在片段中创建验证方法,并使用当前选定片段的实例在父活动中调用该方法,因为您已经拥有片段实例。您不需要再次创建片段实例。
  • @Piyush 先生,我是安卓新手。能否请您编写代码行以访问片段姿态?

标签: android android-fragments


【解决方案1】:

根据您的要求,您必须创建父活动,这意味着您的片段是使用 viewpager 加载的,因此您拥有每个片段的实例。

现在,当您单击活动中的按钮时,可以检查单个片段的验证并访问数据。

首先检查哪个片段在哪个位置,哪个位置将是来自视图分页器的onPageSelected方法的位置。

现在假设您有 Fragment1,并且您想要访问数据并对其进行验证。使用,

Fragment1 f1= (Fragment1) viewPager
                .getAdapter()
                .instantiateItem(viewPager, position);

现在您需要在Fragment1 中创建validate() 方法,以便您可以通过以下方式访问该方法,

f1.validate();

在调用 f1.validate(); 之前,请检查 if(f1!=null) &amp;&amp; f1.isVisible(),然后从中访问数据并对 Fragment2 执行相同操作。

【讨论】:

    【解决方案2】:

    要允许 Fragment 与其 Activity 进行通信,您可以在 Fragment 类中定义一个接口并在 Activity 中实现它。

    以下是 Fragment 到 Activity 通信的示例:

    public class HeadlinesFragment extends ListFragment {
        OnHeadlineSelectedListener callback;
    
        public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener callback) {
            this.callback = callback;
        }
    
        // This interface can be implemented by the Activity, parent Fragment,
        // or a separate test implementation.
        public interface OnHeadlineSelectedListener {
            public void onArticleSelected(int position);
        }
    
        // ...
    }
    

    MainActivity

    public static class MainActivity extends Activity
            implements HeadlinesFragment.OnHeadlineSelectedListener{
    
    
        @Override
        public void onAttachFragment(Fragment fragment) {
            if (fragment instanceof HeadlinesFragment) {
                HeadlinesFragment headlinesFragment = (HeadlinesFragment) fragment;
                headlinesFragment.setOnHeadlineSelectedListener(this);
            }
        }
    }
    

    例如,当用户单击列表项时,会调用片段中的以下方法。 Fragment 使用回调接口将事件传递给父 Activity。

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Send the event to the host activity
        callback.onArticleSelected(position);
    }
    

    希望它对你有用。

    【讨论】:

    • 这是一面倒的兄弟,activity会收到通知,因为activity正在监听回调。但根据问题,他想在用户单击活动按钮时通知片段。
    • @vikaskumar 他在“我如何从片段中获取数据到父活动?”的问题中说。和问题标题“如何从父活动中的片段中获取数据?”
    • 好的,但据此有人必须从片段中推送数据,但按钮处于活动状态。
    • @vikaskumar 我将在一段时间内更新 Activity 的答案以分散通信。
    • 上面的答案解决了我的问题,但非常感谢您尝试帮助我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 2016-12-30
    • 2022-01-10
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多