【问题标题】:How to pass data between 2 fragments displaying on the screen?如何在屏幕上显示的 2 个片段之间传递数据?
【发布时间】:2015-12-14 21:27:03
【问题描述】:

我有一个充满图像的 Gridview,当您单击其中一个图像时,它会启动一个详细活动。
一切正常,但现在我想做一个主从布局。所以我创建了那个“layout-land”文件夹,而不仅仅是一个gridview,它是这样的:

<LinearLayout 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:background="@color/background"
tools:context=".MainActivityFragment">

<GridView
    android:id="@+id/main_grid"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:numColumns="auto_fit" />

<fragment
    android:id="@+id/fragment"
    android:name="com.example.lucas.popularmovies.DetailActivityFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    tools:layout="@layout/fragment_detail" />

</LinearLayout>


在此之前,我将详细信息的数据作为 Intent 的 Extra 传递并在 Fragment 中检索它。
但是当我在同一个屏幕上显示所有内容时,如何在不启动新活动的情况下单击图像时以更新详细信息的方式传递数据?谢谢。

【问题讨论】:

标签: android android-fragments android-activity android-fragmentactivity


【解决方案1】:

简单。

DetailActivityFragment fragment= (DetailActivityFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
    if (fragment != null) {
        fragment.updateImage(url);
    }

【讨论】:

    【解决方案2】:

    由于您的布局中有一个静态片段,我假设您不会删除它,我会说使用这样的简单逻辑:

    在您的活动中,创建一个方法来更新您的静态fragment,例如:

    public void updateImage(String imageUrl) {
        DetailActivityFragment fragment= (DetailActivityFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
        if (fragment != null) {
            fragment.updateImage(imageUrl);
        }
    }
    

    每当您单击图像时,都会调用此方法。

    在你的片段中加载你的图片到你的ImageView(你应该有一个)。

    【讨论】:

      【解决方案3】:

      按正方形使用 EventBus。

      创建一个总线对象并在Activity中注册

      Bus bus = new Bus();
      bus.register(this);
      

      在 Activity 中创建一个 public 方法,并将参数作为您的模型。

      @Subscribe 
      public void getDataFromGrid(MainGridItem item) {
          // TODO: React to the event somehow!
      }
      

      GridViewonItemClick 发布项目 -

      bus.post(mainGridItems.get(position));
      

      【讨论】:

        【解决方案4】:

        你可以使用

        找到片段
        Fragment fragment = getFragmentManager().findFragmentById(R.id.fragmentLoadingSpace)
        if(fragment instanceof  Fragment1){
            ((Fragment1) fragment).updateSelectedObjec(Objects object);
        }
        

        【讨论】:

          【解决方案5】:

          我在另一个 SO 链接 @Passing data between fragments contained in an activity 上发布了完整答案。只需找到我的用户名,这是该帖子的唯一答案。让我们知道您的进展。

          【讨论】:

          • 如果这是重复的,您应该这样投票,而不是发布仅指向另一个帖子的链接答案。
          • @MikeM。下次我会考虑的。我不愿意将任何帖子投票为重复,然后它可能会被关闭,就像其他人投票的那样。我记得有一次我实际上不同意其他人的另一篇文章。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-08-25
          • 1970-01-01
          • 2019-11-01
          • 1970-01-01
          • 2011-07-08
          • 1970-01-01
          相关资源
          最近更新 更多