8

引数で渡されたパラメーターに従ってさまざまなレイアウトを膨らませる ( MyFragment と呼びましょう) がありますfragmentMyFragmentが別のフラグメントから開始された 場合、すべてが正常に機能します。しかし、MyFragmentがアクティブで、別のレイアウト パラメータで新しいMyFragmentを起動したい場合、新しいフラグメントはまったく作成されません。
fragmentManager

data.setInt("Layout index",i);
fragmentTab0 = (Fragment) new MyFragment();
fragmentTab0.setArguments(data);
fragmentTransaction.replace(R.id.fragmentContent, fragmentTab0, "MY");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

フラグメントを再度起動するよう強制するにはどうすればよいですか?fragmentTransaction

: ここで重要な点は、レイアウトを再度インフレートする必要があることです。これは、以前にインフレートされたレイアウトとは異なります。コードは次のようになります。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    switch( getArguments().getInt("Layout index") ) {
    case 1:
        rootView = inflater.inflate(R.layout.firstlayout, container, false);
        break;
    case 2:
        rootView = inflater.inflate(R.layout.secondlayout, container, false);
        break;
    case 3:
        rootView = inflater.inflate(R.layout.thirdlayout, container, false);
        break;
    default: break;
    }       
4

4 に答える 4

0

最初にフラグメントを削除してからremove(fragMgr.findFragmentByTag("MY"))、新しいフラグメントを追加しようとしましたか? PS:このフラグメントへの参照を保持していないと思います。

于 2013-06-17T13:18:12.787 に答える
0

If I understand you correctly: the fragment you want to replace what is currently being displayed and the user does something to cause it to re-display itself?

If this is correct then have done something similar this way:

 public class MyFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            final View V = inflater.inflate(R.layout.myLayout, container, false);

            // Call method that fills the layout with data
            displayData(V);

            // Put a listener here that checks for user input
            Button redisplayButton = (Button) V.findViewById(R.id.my_button);
                // if the button is clicked....
               redisplayButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v){
                    //
                    //  do some stuff
                    //
                    //  ....then eventually...
                    displayData(V); 
                }
              });

        return V;
        }

Later on you can have the displayData() method that defines what the fragment displays....

    public void displayData(View V){

       //  Do something


    return;
    }

Hope this helps!

于 2013-06-17T13:31:29.637 に答える