片段标签主机片段之间的通信 [英] communication between fragments of fragment tab host
问题描述
我实现使用V4支持库FragmentTabHost标签。
I am implementing tabs using FragmentTabHost of v4 support library.
我的第一个选项卡包含我的列表项单击它应该移动到标签2 items.When名单。
我的第二个选项卡显示列表的描述item.So我需要通过第一个选项卡的列表和列表项的索引点击第二个选项卡。
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.
我目前使用的getter和setter方法在应用程序级。但是,有没有其他办法这样做呢?什么是做到这一点的最好方法是什么?
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?
第一个片段 - 制表0
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活动code:
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);
}
}
第一次的frag为null,并且第二次它给人的例外
first time frag is null and second time it gives the exception
推荐答案
我知道这个问题是有点老了,但如果有人仍想知道一个简单的方法来做到这一点,在这里,我走了。所以,问题是发送两个片段之间的数据正确,所以基本上你有什么开始是创建一个返回的活动一个字符串的函数。类似
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;
}
这只是getter和setter一个字符串TabFragmantB。
这将返回该片段的标记您发送数据(让我们把它称为片段B)。所以在片段B的code会是这个样子,它在onCreateView部分自己写出来。
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);
这code将Fragmant B的标签返回到我们的活动。
This code will return you the tag of Fragmant B to our activity.
此外,在Fragmant b创建一个函数,它接受在要采取FragmantB数据的类型。
在这种情况下
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
}
现在有一半的工作完成后,让移动到Fragmant A,
这是这样的。
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);
请注意,通过改变片段中的A和B的功能,你可以通过任何类型的数据。
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.
随意进行更改。
这篇关于片段标签主机片段之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!