So Im using view pager and action bar to set fragments in my activity, now I want to set text on one of my fragment from the container activity which at the moment seems impossible to me..Googling suggested that Fragment.bgintransaction() should be added with the tag and then should be identified with that tag but here in my pager based fragment there is no BeginTransaction() method. Kindly suggest any way of doin
1
votes
1 Answers
0
votes
public class AdminTabsPager extends SherlockFragmentActivity {
private TabHost mTabHost;
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
private String comingFrom ="";
private String moduleName ="";
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed() {
return;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tabs_pager);
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
Bundle bundle = this.getIntent().getExtras();
if(bundle.containsKey("name")) {
comingFrom = bundle.getString("name");
}
if(bundle.containsKey("module")) {
moduleName = bundle.getString("module");
}
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(1);
mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
mTabsAdapter.addTab(mTabHost.newTabSpec("Tab 1").setIndicator("Tab 1"),TabOneFragmentActivity.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("Tab 2").setIndicator("Tab 2"),TabTwoFragmentActivity.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("Tab 3").setIndicator("Tab 3"),TabThreeFragmentActivity.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("Tab 4").setIndicator("Tab 4"),TabFoureFragmentActivity.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("Tab 5").setIndicator("Tab 5"),TabFiveFragmentActivity.class, null);
if(bundle.containsKey("name") && comingFrom.equals("Tab_1Fragment")) {
mTabHost.setCurrentTab(3);
}else if(bundle.containsKey("name") && comingFrom.equals("Tab_2Fragment")) {
mTabHost.setCurrentTab(4);
}else if(bundle.containsKey("name") && comingFrom.equals("Tab_3Fragment")) {
mTabHost.setCurrentTab(0);
}else if(bundle.containsKey("name") && comingFrom.equals("Tab_4Fragment")) {
mTabHost.setCurrentTab(1);
}else {
mTabHost.setCurrentTab(0);
}
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
}
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("tab", mTabHost.getCurrentTabTag()); }
public static class TabsAdapter extends FragmentPagerAdapter
implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final TabHost mTabHost;
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(String _tag, Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
static class DummyTabFactory implements TabHost.TabContentFactory {
private final Context mContext;
public DummyTabFactory(Context context) {
mContext = context;
}
@Override
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mTabHost = tabHost;
mViewPager = pager;
mTabHost.setOnTabChangedListener(this);
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
tabSpec.setContent(new DummyTabFactory(mContext));
String tag = tabSpec.getTag();
TabInfo info = new TabInfo(tag, clss, args);
mTabs.add(info);
mTabHost.addTab(tabSpec);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
@Override
public void onTabChanged(String tabId) {
int position = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(position);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
widget.setDescendantFocusability(oldFocusability);
mTabHost.setCurrentTab(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
}
viewId The id of the ViewPager retrieved via ViewPager.getId() * @param index The position of Fragment inside ViewPager
– Muhammad Babar