如何从父活动中的片段获取数据? [英] How to get data from fragments in the parent activity?

查看:53
本文介绍了如何从父活动中的片段获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的家长活动",其中包含标签页和视图分页器以及底部的按钮:

This is my Parent Activity containing a tab layout and view pager and a button at the bottom:

<?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相似(具有不同的编辑文本,例如名称和电子邮件)

Fragment 2 is similar to fragment 1 (with different edit texts e.g. designation and email)

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

When save button in my parent activity gets pressed, I want to get all the data from both of these fragments in parent activity and if some field is missing then i want to set error to that field.

如何从片段到父活动获取数据?或者我可以通过某种方式获取父活动中片段的所有编辑文本(字段)吗?

How can i get data from fragments to the parent activity? Or Is there some way that i can get all the edit texts (fields) of the fragments in the parent activity?

推荐答案

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

As per your requirement you have to create you have parent activity that means your fragments are loaded using viewpager so you have the instance of each fragment.

现在,当您单击活动中的按钮时,要检查单个片段的有效性和访问数据,您可以执行此操作.

Now while you click the button from activity, to check the validation of individual fragment and accessing data you can do this.

首先检查哪个片段在哪个位置上,这将是视图分页器的 onPageSelected 方法中的位置.

First check that which fragment is on which position which will be the position from onPageSelected method of view pager.

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

Now suppose you have Fragment1 and you want to access data and validate it. Use,

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

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

Now you need to create validate() method in Fragment1 so you can access that method by,

f1.validate();

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

Before you call f1.validate(); check that if(f1!=null) && f1.isVisible() and then access data from it and do same for Fragment2.

这篇关于如何从父活动中的片段获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆