[英]Pass bundle from one tab fragment to another tab fragment
Creating app in which i want to pass bundle data from one tab fragment to another.I have created two tab and getting list in one fragment but now i want to select item from Recyclerview
list using checkbox
and set this selected item to another tab fragment on swipe.Using viewpager
for creating tab.In second tab fragment set bundle and in first tab fragment get bundle but not working. 创建应用程序,我想将捆绑数据从一个选项卡片段传递到另一个。我已经创建了两个选项卡并在一个片段中获取列表,但现在我想使用
checkbox
从Recyclerview
列表中选择项目并将此选中的项目设置为使用viewpager
创建选项卡。在第二个选项卡片段集中设置捆绑包,在第一个选项卡片段中获取捆绑包,但不起作用。
In second fragment i set bundle 在第二个片段中,我设置了捆绑
Bundle data = new Bundle();
data.putString("name",contactArrayList.get(position).getContactName());
data.putString("mobile",contactArrayList.get(position).getContactNumber());
Fragment fragment = FragmentInfo.newInstance(1);
fragment.setArguments(data);
In first fragment i get bundle 在第一个片段中,我得到捆绑
if (bundle != null) {
String name = bundle.getString("name");
String mobile = bundle.getString("mobile");
// object of Model Class
mydata.setName(name);
mydata.setMobile(mobile);
myDataArrayList.add(mydata);
}
try this: 尝试这个:
create a mechanism to access the tag across fragments
via the main activity, . 创建一种机制来通过主要活动跨
fragments
访问标签。 - Call getTag()
in onCreateView()
of FragmentB
. -在
FragmentB
onCreateView()
中调用getTag()
。 Then pass it to the main activity. 然后将其传递给主要活动。 When
FragmentA
need to pass something to FragmentB
, it retrieve it from MainActivity
. 当
FragmentA
需要将某些内容传递给FragmentB
,它将从MainActivity
检索它。
public static class TabsAdapter extends FragmentPagerAdapter
implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public TabsAdapter(FragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
@Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
mActionBar.setSelectedNavigationItem(position);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i=0; i<mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
@Override
public int getCount() {
return mTabs.size();
}
}
For full implementation see this link: 有关完整的实现,请参见以下链接:
http://android-er.blogspot.com/2012/06/communication-between-fragments-in.html http://android-er.blogspot.com/2012/06/communication-between-fragments-in.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.