私はこれのためにひどくうんざりしています。SherlockFragmentActivity から SherlockFragment のメソッドを呼び出そうとしています。私の SherlockFragmentActivity は、Sherlock のタブとページャー機能を使用しています。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");
}
}
}