0

我成功地制作了一个带有选项卡式导航的项目,我可以从 onCreate() 添加新选项卡,但我想从按钮添加新选项卡(在我动态生成的片段内)。

Fragment 是 Main(Activity) 中的一个静态类,如果我将制表符插入行注释掉,它就可以工作:

/*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 时遇到了麻烦!任何帮助/提示将不胜感激!谢谢!

4

1 回答 1

0

从“MenuFragmentTab”中删除“静态”并且它有效!虽然给出了警告,但有非静态片段,所以我想这解决了问题。

于 2013-01-16T19:29:08.233 回答