【问题标题】:Communication between SlidingTabLayout tabsSlidingTabLayout 选项卡之间的通信
【发布时间】:2015-10-13 19:08:50
【问题描述】:

我已经搜索了很多关于如何使用 SlidingTabLayout 在片段之间进行通信的内容,但并没有真正得到一个好的答案。我知道使用 ActionBar,但我想要使用 SlidingTabLayout 的 android 棒棒糖的新方法。我试过这个-> http://android-er.blogspot.in/2012/06/communication-between-fragments-in.html 但我想要材料设计。我参考了这个链接http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html 来制作材料设计滑动标签。 现在我想知道如何在滑动标签之间进行通信。我已经尝试了很多,但找不到我正在寻找的答案。 任何帮助将不胜感激。

【问题讨论】:

标签: android android-fragments material-design


【解决方案1】:

最简洁的方法是定义一个包含Fragments 的Activity 将实现的接口。这就是我最近解决这个问题的方法:

首先在它自己的文件中定义接口,因为它必须对其他类可见。

public interface FragmentCommunication
{
    public void printMessage(String message);
    //....    
}

在你的Activity你需要实现这个接口

public class MainActivity extends ActionBarActivity implements FragmentCommunication
{   
    //....
    public void printMessage(String message)
    {
        System.out.println(message);
    }
}

最后,在您的Fragments 中,您可以通过getActivity() 获得托管Activity,并使用通信方法只需将活动转换为已实现的通信接口,如下所示:

((FragmentCommunication) getActivity()).printMessage("Hello from Fragment!");

编辑:要进一步将消息传递给其他Fragments,请执行以下操作:因为您的标签都扩展Fragment,所以最好创建另一个界面

public Interface ReceiverInterface
{
    public void receiveMessage(String str);
}

然后在你的标签中实现它

public class Tab1 extends Fragment implements ReceiverInterface
{
   // .... code .....
    public void receiveString(String str)
    {
        //use str
    }
}

要进一步将此消息发送到其他片段,活动需要看到它们。例如现在将Activity实现的printMessage()修改为此

    public void printMessage(String message)
    {
        System.out.println(message);
        //Send the message that came from one fragment to another
        if (tabFragment1 instanceof ReceiverInterface){
            ((ReceiverInterface) tabFragment1).receiveMessage(message);
        }
    } 

【讨论】:

  • 我想将一个字符串从一个片段发送到另一个片段。因此,当我单击片段 A 中的按钮时,它应该将包或字符串传递给片段 B。你认为我是如何做到的
  • 那么要求activity可以访问这两个片段。可以?您通过接口从一个片段发送字符串,并将其放置在接口方法中的活动片段中。片段也需要一个方法来接收消息,但是只要它们是公共的,Activity 就会看到您的片段所具有的所有方法,因此不需要接口。
  • 是的,活动可以访问这两个片段。我从片段 A 发送字符串,但我面临的问题是片段 B 没有收到发送的内容。如何实现片段B中的消息接收方法
  • 编辑答案,在手机上有点困难:)
  • 你至少能做到。这真的是一个很大的帮助。我被这个问题困扰了一个多星期。这真的很有帮助。
【解决方案2】:

当您滑动标签 (ViewPager) 时,您可以使用相同的 Fragment 或使用不同的 Fragments

正如你之前提到的,你尝试了this,所以我将使用不同的Fragments

你要做的基本上就是使用EventBus:https://github.com/greenrobot/EventBus

将其添加到位于 app 文件夹内的 build.gradle 依赖项中。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'de.greenrobot:eventbus:2.4.0'   
}

您也可以使用Intents 来实现它。

1 - 创建一个类来表示文本更改时的事件:

public class TextChangedEvent {
  public String newText;
  public TextChangedEvent(String newText) {
      this.newText = newText;
  }
}

2 - 片段 A:

//when text changes
EventBus bus = EventBus.getDefault();
bus.post(new TextChangedEvent(newText));    

3 - 片段 B:

EventBus bus = EventBus.getDefault();

//Register to EventBus
@Override
public void onCreate(SavedInstanceState savedState) {
 bus.register(this);
}

//catch Event from fragment A
public void onEvent(TextChangedEvent event) {
 yourTextView.setText(event.newText);
}

来源:https://stackoverflow.com/a/20475430/1549700

【讨论】:

  • 当我将 compile 'de.greenrobot:eventbus:2.4.0' 添加到我的 gradle 文件中时,它说它无法解决
  • 您添加的位置是否正确?它应该在您的 app build.gradle 文件中。如果我的编辑没有帮助,请检查:stackoverflow.com/questions/28493470/…
  • 非常感谢。它如此迅速地完成了这项工作。非常感谢
  • 您刚刚拯救了这一天!是否可以使用 LiveData 做同样的事情?
【解决方案3】:

使用 EventBus GitHub 库。这是目前最简单、最方便的方式。 https://github.com/greenrobot/EventBus

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 2015-04-10
    相关资源
    最近更新 更多