5

ActionBar で Fragments を使用したかったのです。残念ながら、それは本当に複雑なようです。私のフラグメントにはテキストビューがあり、アクティビティからそれらと通信できるようにしたいと考えています。フラグメントの使用を開始する前に、フラグメントにアクセスできました

private EditText editText = (EditText) findViewById(R.id.editTextName);

そのため、ユーザーが保存をクリックしたときに editText 値を受け取ることができました。Fragment-wayでこれを行うにはどうすればよいですか?

アクティビティ:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_recipe);
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    getSupportActionBar().setDisplayOptions(0, com.actionbarsherlock.app.ActionBar.DISPLAY_SHOW_HOME | com.actionbarsherlock.app.ActionBar.DISPLAY_USE_LOGO | com.actionbarsherlock.app.ActionBar.DISPLAY_SHOW_TITLE);

    ActionBar.Tab newTab0 = getSupportActionBar()
            .newTab()
            .setText(R.string.fragment_general)
            .setTabListener(
            new MyTabListener<GeneralFragment>(this, "general",
                            GeneralFragment.class));


    getSupportActionBar().addTab(newTab0);

}
public static class MyTabListener<T extends Fragment> implements
        TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /**
     * * Constructor used each time a new tab is created. * * @param
     * activity * The host Activity, used to instantiate the fragment * @param
     * tag * The identifier tag for the fragment * @param clz * The
     * fragment's Class, used to instantiate the fragment
     */

    public MyTabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    /* The following are each of the ActionBar.TabListener callbacks */

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            //ft.setCustomAnimations(android.R.animator.fade_in, R.animator.animationtest);
            ft.attach(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            //ft.setCustomAnimations(android.R.animator.fade_in, R.animator.test);
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}
4

2 に答える 2

12

フラグメントでgetView()を呼び出してから、返されたビューでfindViewById()を使用する必要があります。

もちろん、ビューが作成されるまで何も返されないため、onCreate以外の場所で呼び出す必要がある場合があります。

于 2012-10-09T17:49:20.553 に答える
1

まあ、xmlファイルを膨らませるか、ゼロから作成する必要がありますが、余分な作業が好きでない限りお勧めしません:)しかし、アクティビティを使用するsetContentView()と、xmlファイルが膨らみ、その後onCreate()アクセスできますあなたが慣れている方法。親ビューなしで onCreate の外部のレイアウト ビューにアクセスしようとすると、同じ問題が発生することに注意してください。グローバル変数を作成しない限り。onCreate は通常、開発者が常に作業を開始する場所であるため、Android ではfindViewById.

edit.xml という xml ファイルがあるとします。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//this method is found within your Fragment, which you must ovverride
View view = inflater.inflate(R.layout.edit, container, false);
private EditText editText = (EditText) view.findViewById(R.id.editTextName);
//...
}

onCreateFragmentsなどの代わりにその方法でそれを行うことを選択した理由をよりよく理解するために、 Fragments に関する Android ドキュメントを自由にチェックしてください。

于 2012-10-09T17:34:38.117 に答える