Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

android - Communication between SlidingTabLayout tabs

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The cleanest way is to define an interface that the Activity that contains the Fragments 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);
    }
}

Finally in your Fragments 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 Fragments 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
    }
}

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);
        }
    } 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...