SlidingTabLayout标签之间的通信 [英] Communication between SlidingTabLayout tabs
问题描述
我搜索了很多有关如何使用SlidingTabLayout片段之间进行通信,但还没有真正得到了一个很好的答案。我知道用动作条,但我想新的方式,是使用SlidingTabLayout Android的棒棒糖。我想这 - > <一个href="http://android-er.blogspot.in/2012/06/communication-between-fragments-in.html">http://android-er.blogspot.in/2012/06/communication-between-fragments-in.html但我想材质设计。我提到这个链接<一个href="http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html">http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html制作材料设计的滑动标签。 现在我想知道如何滑动标签之间进行通信。我已经尝试了很多,但无法找到我一直在寻找的答案。 任何帮助真的AP preciated。
I've searched a lot on how to communicate between fragments using SlidingTabLayout but haven't really got a good answer. I know using ActionBar but I wanted the new way that is for android lollipop using SlidingTabLayout. I tried this-> http://android-er.blogspot.in/2012/06/communication-between-fragments-in.html but I wanted material design. I referred this link http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html for making material design sliding tabs. Now I wanted to know how to communicate between the sliding tabs. I've tried a lot but couldn't find the answer I was looking for. Any help would really appreciated.
推荐答案
最彻底的方法是定义的活动
包含一个接口的片段
旨意实现。这是我最近怎么解决这样的:
The cleanest way is to define an interface that the Activity
that contains the Fragment
s will implement. This is how I recently solved this:
首先定义接口在它自己的文件,因为它必须是可见的其他类
First define the interface in it's own file, because it has to be visible to other classes.
public Interface FragmentCommunication
{
public void printMessage(String message);
....
}
在你的活动
你需要实现这个接口
In your Activity
you need to implement this interface
public class MainActivity extends ActionBarActivity implements FragmentCommunication
{
....
public void printMessage(String message)
{
System.out.println(message);
}
}
最后,在你的片段
是你能得到托管活动
与 getActivity()
和使用的通信方法只投活动到像这样实现的通讯接口:
Finally in your Fragment
s you can get the hosting Activity
with getActivity()
and to use the communication methods just cast the activity into the implemented communication interface like so:
((FragmentCommunication) getActivity()).printMessage("Hello from Fragment!");
修改:为了进一步将消息传递给其他片段
取值做到这一点:既然你的标签都继承片段
这是最好创建另一个接口
EDIT: To further pass the message to other Fragment
s do this: since your tabs all extend Fragment
it is the best to create another interface
public Interface ReceiverInterface
{
public void receiveMessage(String str);
}
然后实现这在标签页
Then implement this in your tabs
public class Tab1 extends Fragment implements ReceiverInterface
{
.... code .....
public void receiveString(String str)
{
//use str
}
}
要进一步发这条短信给其他片段,要求该活动看到他们。比如现在修改 printMessage()
的活动
实现这个
To further send this message to other Fragments it is required that the activity sees them. For example now modify the printMessage()
that Activity
implements to this
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);
}
}
这篇关于SlidingTabLayout标签之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!