从活动中获取片段文本视图

时间:2015-02-13 10:56:39

标签: android android-fragments

所以我使用视图寻呼机和操作栏在我的活动中设置片段,现在我想在容器活动中设置我的一个片段上的文本,此刻对我来说似乎不可能.Googling建议Fragment.bgintransaction( )应该添加标签,然后应该用该标签识别,但在我的基于寻呼机的片段中没有BeginTransaction()方法。请建议任何办法

1 个答案:

答案 0 :(得分:0)

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"));
    }
}

@覆盖     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) {
    }
}

}