communication between fragments of fragment tab host

六月ゝ 毕业季﹏ 提交于 2019-12-13 05:19:09

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/23467612/communication-between-fragments-of-fragment-tab-host

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!