2

layout-small-portraitという名前の単一の「コンテナアクティビティ」に含まれるさまざまなフラグメントをレイアウトで起動したいアプリケーションに取り組んでいますSingleActivity。レイアウトなどでこれを異なる方法で処理しますがlayout-landlayout-largeそれは私の問題とは関係ありません。

MainActivity名前が示すように、アプリケーションのメイン アクティビティ (ランチャー) であるアクティビティがあります。これには、最初にListFragment、ユーザーが押すためのさまざまな項目が含まれています。

ユーザーが押したアイテムに基づいてSingleActivityが起動し、そのコンテンツはFragmentこのアイテムに関連する特定のものに対応します。私の問題はここから始まります。ユーザーが項目を押すと、表示したい対応するフラグメントへの参照がありますSingleFragment。以下に図を示します。

  String tag = myFragmentReference.getTag();
  Intent i = new Intent(this, SingleActivity.class);
  i.putExtra(SingleActivity.CONST_TAG, tag);
  startActivity(i);

アクティビティが正常に開始されます。SingleActivity私は次の方法onCreate()を持っています:

...

// Retrieve the fragment tag from the intent
String tag = getIntent().getStringExtra(CONST_TAG);
Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);

if(fragment == null) {
    // always end up here, this is my problem.
}

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragmentContainer, fragment);
ft.commit();

...

フラグメントがまだ膨張していないため、fragment常にそうであると思われます。null私が正しければ、フラグメントが膨張する前にフラグメントのタグを定義して、findFragmentByTag(). それは可能ですか?

ご不明な点がございましたら、お知らせください。

良いアイデアが聞けるのを楽しみにしています!これを実装するためのより良い、またはより賢い方法があれば、あなたの考えを聞きたいです! ありがとう :)

4

2 に答える 2

3

別のアクティビティにジャンプしているため、独自の Fragment BackStack があり、そのフラグメントは存在しません。

次の行に沿って、新しいアクティビティでフラグメントを膨らませる必要があります。

String tag = intent.getStringExtra(CONST_TAG);

    if (getSupportFragmentManager().findFragmentByTag(tag) == null) {
        Fragment fragment = Fragment.instantiate(this, tag, extras);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fragmentContainer, fragment, tag);
        ft.commit();
    }

タグ文字列には、「com.android.myprojectname.myfragment」などのフラグメントのパッケージの場所が必要です。

于 2013-01-16T22:57:08.240 に答える
1

最初に SlidingMenu ライブラリを使用します: https://github.com/jfeinstein10/SlidingMenu これはあなたを助け、あなたのアプリはよりクールになります。それが私があなたが必要とするものを作るのを助けることができる唯一の方法です。ここにコードがあります:

MainActivity は次のとおりです。

このサンプル コードを説明しようと思います。必要に応じて使用してください。

これは、BehindContent (SlidingMenu) の ListFragment です。

public class ColorMenuFragment extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.list, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String[] colors = getResources().getStringArray(R.array.color_names);
        ArrayAdapter<String> colorAdapter = new ArrayAdapter<String>(getActivity(), 
                android.R.layout.simple_list_item_1, android.R.id.text1, colors);
        setListAdapter(colorAdapter);
//This array is only to fill SlidingMenu with a Simple String Color.
//I used MergeAdapter from Commonsware to create a very nice SlidingMenu.
    }

    @Override
    public void onListItemClick(ListView lv, View v, int position, long id) {
//This switch case is a listener to select wish item user have been selected,  so it Call
//ColorFragment, you can change to Task1Fragment, Task2Fragment, Task3Fragment.
        Fragment newContent = null;
        switch (position) {
        case 0:
            newContent = new ColorFragment(R.color.red);
            break;
        case 1:
            newContent = new ColorFragment(R.color.green);
            break;
        case 2:
            newContent = new ColorFragment(R.color.blue);
            break;
        case 3:
            newContent = new ColorFragment(android.R.color.white);
            break;
        case 4:
            newContent = new ColorFragment(android.R.color.black);
            break;
        }
        if (newContent != null)
            switchFragment(newContent);
    }

    // the meat of switching the above fragment
    private void switchFragment(Fragment fragment) {
        if (getActivity() == null)
            return;

        if (getActivity() instanceof FragmentChangeActivity) {
            FragmentChangeActivity fca = (FragmentChangeActivity) getActivity();
            fca.switchContent(fragment);
        } else if (getActivity() instanceof ResponsiveUIActivity) {
            ResponsiveUIActivity ra = (ResponsiveUIActivity) getActivity();
            ra.switchContent(fragment);
        }
    }


}

BaseActivity クラスは次のとおりです。

私が理解できるように、スワイプはありません。これは必要ありません。

public class FragmentChangeActivity extends BaseActivity {

    private Fragment mContent;

    public FragmentChangeActivity() {
        super(R.string.changing_fragments);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set the Above View
        if (savedInstanceState != null)
            mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
        if (mContent == null)
            mContent = new ColorFragment(R.color.red);  

        // set the Above View
            //This will be the first AboveView
        setContentView(R.layout.content_frame);
        getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.content_frame, mContent)
        .commit();

        // set the Behind View
            //This is the SlidingMenu
        setBehindContentView(R.layout.menu_frame);
        getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.menu_frame, new ColorMenuFragment())
        .commit();

        // customize the SlidingMenu
            //This is opcional
        getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        getSupportFragmentManager().putFragment(outState, "mContent", mContent);
    }

    public void switchContent(Fragment fragment) {
            // the meat of switching fragment
        mContent = fragment;
        getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.content_frame, fragment)
        .commit();
        getSlidingMenu().showContent();
    }

}

わかりました。ColorFragment を別のものに変更したい場合は、次のようにします。

まず、使用するアイテムを選択します。

case 0:
                newContent = new ColorFragment(R.color.red);
                break;

に:

case 0:
            newContent = new ArrayListFragment();
            break;

私は配列リストを作成しました。これは単純な例にすぎません。多くのことを行うことができます。その後、Fragment について読んで、さまざまなことを行う方法を学ぶことができます。

public class ArrayListFragment extends ListFragment {

@Override                               
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setListAdapter(new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, Listnames.TITLES));

//Listnames は String[] TITLES を持つクラスです。

}

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            Log.i("FragmentList2", "Item clicked: " + id);

            String item = (String) getListAdapter().getItem(position);
        Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show();

        }

    }

ご覧のとおり、ListFragment (MainActivity) 内のどのアイテムをユーザーが押したかに基づいて、異なるフラグメントを表示できます。

まあ、何か誤解していたら、教えてください。

于 2013-01-16T23:32:58.563 に答える