0

タブ付きナビゲーションを使用してプロジェクトを正常に作成し、onCreate()から新しいタブを追加できますが、ボタン(動的に生成しているフラグメント内)から新しいタブを追加したいと思います。

フラグメントは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」から「static」を削除したところ、機能しました。警告が出ますが、静的でないフラグメントがあるので、これで問題は解決したと思います。

于 2013-01-16T19:29:08.233 に答える