我成功创建了一个带标签导航的项目,我可以从onCreate()添加新标签,但我想从按钮添加新标签(在我动态生成的片段内)。
Fragment是Main(Activity)中的一个staic类,如果我对选项卡插入行进行注释,它会起作用:
/*Fragments set the layout of the tabs*/
public static class MenuFragmentTab extends SherlockFragment{
private Context context;
private byte position;
public void initialize(Context context, byte position){
this.context = context;
this.position = position;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
/*Create the Fragment layout so I can attach the handlers*/
View overView = inflater.inflate(R.layout.menu_fragment, container, false);
/*When I create the fragment I initialize the button listeners*/
if(context != null){
/*Close Main tab*/
Button closeMainButton = (Button)overView.findViewById(R.id.main_close_btn);
CloseMainHandler closeMain = new CloseMainHandler();
closeMainButton.setOnClickListener(closeMain);
/*Open a new Tab*/
Button newTabButton = (Button)overView.findViewById(R.id.new_tab_btn);
NewTabHandler newTab = new NewTabHandler();
newTabButton.setOnClickListener(newTab);
}
return overView;
}
/*the button to open a new tab*/
private class NewTabHandler implements View.OnClickListener{
@Override
public void onClick(View v) {
/*get the info for the new tab*/
View parent = (View)v.getParent();
EditText newTabTitle = (EditText)parent.findViewById(R.id.new_tab_title);
EditText newTabContent = (EditText)parent.findViewById(R.id.new_tab_content);
//Toast.makeText(context, newTabTitle.getText(), Toast.LENGTH_LONG).show();
/*add the info to the ArrayLists*/
Main.titles.add(newTabTitle.getText().toString());
Main.contents.add(newTabContent.getText().toString());
/*initialize the new tab*/
ActionBar.Tab newTab = Main.actionBar.newTab().setText(
Main.titles.get(Main.titles.size()-1)
);
ContentFragmentTab fragContent = new ContentFragmentTab();
fragContent.initialize(
context,
(byte)(Main.titles.size()-1)
);
/*This gives back a compilation error because of non-static reference*/
TabClickHandler newListener = new TabClickHandler(fragContent);
newTab.setTabListener(newListener);
Main.actionBar.addTab(newTab);
}
}
/*the button to close the main tab*/
/*This should be a public, static function in Main, to avoid code repetition*/
private class CloseMainHandler implements View.OnClickListener{
@Override
public void onClick(View v) {
Main.actionBar.removeTabAt(position);
}
}
};
我的问题是:我如何使这项工作?我可以删除标签甚至添加标签,我只能在绑定TabChangeListeners时遇到麻烦!任何帮助/提示将不胜感激!谢谢!
答案 0 :(得分:0)
从“MenuFragmentTab”中移除“静态”并且它可以正常工作! 虽然给出了警告,但是有非静态片段,所以我想这解决了这个问题。