如何从使用Tabs和pager的活动中引用SherlockFragment

时间:2013-02-01 16:25:37

标签: android actionbarsherlock

由于这个原因,我非常厌倦了。 我试图从SherlockFragmentActivity调用SherlockFragment中的方法.. 我的SherlockFragmentActivity使用Sherlock的Tabs和寻呼机功能。 我无法参考我的SherlockFragment。 如果您可以帮助我如何从其活动中调用片段中存在的方法,我将非常感激。

这是我的SherlockFragmentActivity!

public class main extends SherlockFragmentActivity implements 
menu_fragment.OnMenuSelectedListener,
content_fragment.OnContactSelectedListener{
TabHost mTabHost;
ViewPager mViewPager;
TabsAdapter mTabsAdapter;

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>();
private final ActionBar mActionBar;

static final class TabInfo {
    private final String tag;
    private final Class<?> clss;
    private final Bundle args;

    TabInfo(String _tag, Class<?> _class, Bundle _args) {
        tag = _tag;
        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(SherlockFragmentActivity activity, TabHost tabHost, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mTabHost = tabHost;
        mViewPager = pager;
        mTabHost.setOnTabChangedListener(this);
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
        mActionBar = activity.getSupportActionBar();
    }

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args){
        tabSpec.setContent(new DummyTabFactory(mContext));
        String tag = tabSpec.getTag();
        Log.d("NET","tabtag: "+tag);
        TabInfo info = new TabInfo(tag, clss, args);
        mTabs.add(info);
        mTabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int position) {
        // Unfortunately when TabHost changes the current tab, it kindly
        // also takes care of putting focus on it when not in touch mode.
        // The jerk.
        // This hack tries to prevent this from pulling focus out of our
        // ViewPager.
        TabWidget widget = mTabHost.getTabWidget();
        int oldFocusability = widget.getDescendantFocusability();
        widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        mTabHost.setCurrentTab(position);
        widget.setDescendantFocusability(oldFocusability);
    }

    @Override
    public void onTabChanged(String arg0) {
        int position = mTabHost.getCurrentTab();
        mViewPager.setCurrentItem(position);
    }

    @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();
    }

}



@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    setContentView(R.layout.main);
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();

    mViewPager = (ViewPager) findViewById(R.id.pager);

    mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);     

    mTabsAdapter.addTab(mTabHost.newTabSpec("MENU").setIndicator("MENU"),
            menu_fragment.class, null);
    mTabsAdapter.addTab(mTabHost.newTabSpec("CONTENT").setIndicator("CONTENT"),
            content_fragment.class, null);

    content_fragment c = (content_fragment) getSupportFragmentManager().findFragmentByTag(makeFragmentName(R.id.pager, 1));

    //Log.d("NET",getFragmentTag(1));
    //Log.d("NET",makeFragmentName(R.layout.contacts, 1));

    try{
        c.test();
    }catch(Exception e){
        Log.d("NET",e.toString());
    }
}

private String getFragmentTag(int pos){
    return "android:switcher:"+R.id.pager+":"+pos;   //fragmentpageradapter auto generated tag
}

private static String makeFragmentName(int viewId, int index) {
     return "android:switcher:" + viewId + ":" + index;
}

@Override
public void onMenuSelected(int i){
    Toast.makeText(getApplicationContext(), "menu selected "+i, Toast.LENGTH_SHORT).show();

}

@Override
public void onContactSelected(){
    Toast.makeText(getApplicationContext(), "contact selected", Toast.LENGTH_SHORT).show();
    //ActionBar mActionBar = getSupportActionBar();
}

}

这是我的SherlockFragment!

public class content_fragment extends SherlockFragment{
static ScrollView scroller_page;
static String[] contacts = {"gp","janu","tn","varun","joseph"};
OnContactSelectedListener cListener;

public interface OnContactSelectedListener {
    public void onContactSelected();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View f = inflater.inflate(R.layout.content_fragment, container, false);

    return f;
}

public void test(){
    Toast.makeText(getSherlockActivity(), "method called ", Toast.LENGTH_SHORT).show();
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try{
        cListener = (OnContactSelectedListener) activity;
    }catch(Exception e){
        throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
    }
}

}

1 个答案:

答案 0 :(得分:3)

尝试更换:

content_fragment c = (content_fragment) getSupportFragmentManager().findFragmentByTag(makeFragmentName(R.id.pager, 1));

使用:

content_fragment c = (content_fragment) mTabsAdapter.getItem(1);