search

Home  >  Q&A  >  body text

android - 用xml创建的fragment如何进行数据的传递与更新?

<android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/content_main" />

        <fragment
            android:id="@+id/menu_drawer"
            android:name=".DrawerFragment"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start" />
    </android.support.v4.widget.DrawerLayout>

这是mainactivity的布局,DrawerLayout的左边是一个侧滑菜单,里面是一个fragment,已经写死了,所以不需要初始化,打开程序的时候直接里面一次性更新了数据。

现在通过搜索界面会给mainactivity传递数据,求问怎样才能把数据传给fragment使用?

阿神阿神2772 days ago476

reply all(4)I'll reply

  • 高洛峰

    高洛峰2017-04-17 17:37:08

    Use FragmentManager to find your Fragment and let it interact with other classes by constructing an interface. Of course, you can directly obtain the Fragment instance and call its methods.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:37:08

    Fragments can be loaded this way. transaction.replace(R.id.frame, fragment); Just write a set method in the fragment, so that passing the value is simple.
    I have never tried this kind of writing to death. You can completely use the above method instead

    reply
    0
  • 阿神

    阿神2017-04-17 17:37:08

    1. Message bus EventBus, otto or RxBus

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 17:37:08

    The following two methods:
    1. Use event bus EventBus or RxBus
    2. Write the set method in Fragment

    reply
    0
  • Cancelreply