communication between fragments of fragment tab ho

2019-08-23 03:55发布

I am implementing tabs using FragmentTabHost of v4 support library.

My first tab contains the list of items.When I click on list item it should move to tab 2. My second tab shows the description of the list item.So I need to pass the list of the first tab and the index of list item clicked to the second tab.

I am currently using getters and setters at application level. But is there any other way to do so? What is the best way to do this?

First fragment-Tab-0

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);

    try {
        mCallback = (OnRadioSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnHeadlineSelectedListener");
    }
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);          
        mCallback.onRadioSelected(dataList, position);
}



public interface OnRadioSelectedListener {
    public void onRadioSelected(ArrayList<DataModel> playingList, int   playingIndex);
}

Tabhost activity code:

@Override
    public void onRadioSelected(ArrayList<DataModel> playingList, int playingIndex) {
        mPlayingIndex = playingIndex;
        mPlayingList = playingList;

       fragmentTabHost.setCurrentTab(1);
        Fragment2 frag = (Fragment2) getSupportFragmentManager().findFragmentByTag(getString(R.string.str));
    if (frag != null) {
        Bundle args = new Bundle();
        args.putSerializable("Key_list", mPlayingList);
        args.putInt("Key_current_index", mPlayingIndex);
        frag.setArguments(args);
    }


}

first time frag is null and second time it gives the exception

2条回答
smile是对你的礼貌
2楼-- · 2019-08-23 04:38

I know this question is a little old but if anyone is still interested in knowing an easy way to do this ,here i go. So the problem is sending data between two fragments right , so basically what you have to start with is create a function which returns a string in your Activity. Something like

String TabFragmentB;
public void setTabFragmentB(String t){
    TabFragmentB = t;
}
public String getTabFragmentB(){
    return TabFragmentB;
}

This is just getter and setter for a String TabFragmantB. This will return you the tag of the Fragment to which you are sending data(Lets call it Fragment B).So the code in Fragment B will look something like this ,write it in the onCreateView section itself.

String myTag = getTag();

    ((YourActivity)getActivity()).setTabFragmentB(myTag);

This code will return you the tag of Fragmant B to our activity.

Also in Fragmant B create a function which takes in the type of data you want to take in FragmantB. In this case

 public void Fucntion_FragmantB(List<YourObject> ob)
{
    //DO ANYTHING WITH THE DATA
}

Now Half the work is done ,lets move to Fragmant A, Here it goes something like this.

 List<YourObject> ob=new List<YourObject>();
 String TabOfFragmentB = ((Your_ACTIVITY)getActivity()).getTabFragmentB();

                    Statistics_Fragment fragmentB = (FragmentB)getActivity()
                            .getSupportFragmentManager()
                            .findFragmentByTag(TabOfFragmentB);

                    fragmentB.Fucntion_FragmantB(ob);

Note that by changing the functions in Fragment A and B ,you can pass any type of data.

Hope this works for everyone ,because it did for me.

Feel free to make changes.

查看更多
该账号已被封号
3楼-- · 2019-08-23 04:47

I recommend checking out this developer article on Communicating with other fragments: https://developer.android.com/training/basics/fragments/communicating.html

In short, you have your Activity implement an interface which all fragments use to communicate with each other. When a fragment needs to do something, it asks the Activity to handle the behavior.

So if fragment1 wants to go to fragment2, fragment1 calls YourActivity.goToFragment2() (or whatever), and YourActivity will handle it. eg:

public void goToFragment2(){
    mTabHost.setCurrentTab (indexOfFragment2Here);
}

You need to ensure your Activity implements the interface, so check out the implementation of Fragment.onAttach(Activity activity) in the "Define a Interface" section: https://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

查看更多
登录 后发表回答