gpt4 book ai didi

android - 如何从使用 Tabs 和寻呼机的 Activity 中获取对 SherlockFragment 的引用

转载 作者:搜寻专家 更新时间:2023-11-01 09:01:41 26 4
gpt4 key购买 nike

因为这个,我受够了。我试图从 SherlockFragmentActivity 中调用 SherlockFragment 中的方法。我的 SherlockFragmentActivity 使用 Sherlock 的选项卡和寻呼机功能。我无法获得对我的 SherlockFragment 的引用。如果您能帮助我了解如何从其 Activity 中调用 fragment 中存在的方法,我将不胜感激。

这是我的 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");
}
}

最佳答案

尝试替换:

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

与:

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

关于android - 如何从使用 Tabs 和寻呼机的 Activity 中获取对 SherlockFragment 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14650679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com