88
public class MainActivity extends Activity implements MainMenuFragment.OnMainMenuItemSelectedListener {

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();

    // add menu fragment
    MainMenuFragment myFragment = new MainMenuFragment();
    fragmentTransaction.add(R.id.menu_fragment, myFragment);

    //add content
    DetailPart1 content1= new DetailPart1 ();
    fragmentTransaction.add(R.id.content_fragment, content1);
    fragmentTransaction.commit();

}
public void onMainMenuSelected(String tag) {
  //next menu is selected replace existing fragment
}

2つのリストビューを並べて表示する必要があります。左側にメニュー、右側にそのコンテンツがあります。デフォルトでは、最初のメニューが選択され、その内容が右側に表示されます。コンテンツを表示するフラグメントは次のとおりです。

public class DetailPart1 extends Fragment {
  ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();
  ListAdapter adap;
  ListView listview;

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);

       if(savedInstanceState!=null){
        myList = (ArrayList)savedInstanceState.getSerializable("MYLIST_obj");
        adap = new LoadImageFromArrayListAdapter(getActivity(),myList );
        listview.setAdapter(adap);
       }else{
        //get list and load in list view
        getlistTask = new GetALLListTasks().execute();
    }


     @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.skyview_fragment, container,false);
           return v;
        }


     @Override
      public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
          outState.putSerializable("MYLIST_obj", myList );
        }
    }

onActivityCreatedとonCreateViewは2回呼び出されます。フラグメントを使用した例はたくさんあります。私はこのテーマの初心者なので、例を私の問題と関連付けることはできません。向きの変更を処理するための絶対確実な方法が必要です。android:configChangesマニフェストファイルで宣言していません。ランドスケープモードで別のレイアウトを使用できるように、アクティビティの破棄と再作成が必要です。

4

2 に答える 2

133

アクティビティで画面を回転させるたびに新しいフラグメントを作成していますが、。onCreate();を使用して古いフラグメントも維持していsuper.onCreate(savedInstanceState);ます。したがって、タグを設定してフラグメントが存在する場合はそれを見つけるか、nullバンドルをsuperに渡します。

これは私が学ぶのに少し時間がかかりました、そしてあなたがviewpagerのようなもので働いているときそれは本当に苦痛かもしれません。

この正確なトピックがカバーされているので、フラグメントについてさらに時間を読むことをお勧めします。

定期的な向きの変更でフラグメントを処理する方法の例を次に示します。

アクティビティ

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if (savedInstanceState == null) {
            TestFragment test = new TestFragment();
            test.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().replace(android.R.id.content, test, "your_fragment_tag").commit();
        } else {
            TestFragment test = (TestFragment) getSupportFragmentManager().findFragmentByTag("your_fragment_tag");
        }
    }
}

フラグメント

public class TestFragment extends Fragment {

    public static final String KEY_ITEM = "unique_key";
    public static final String KEY_INDEX = "index_key";
    private String mTime;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout, container, false);
        
        if (savedInstanceState != null) {
            // Restore last state
            mTime = savedInstanceState.getString("time_key");
        } else {
            mTime = "" + Calendar.getInstance().getTimeInMillis();
        }
        
        TextView title = (TextView) view.findViewById(R.id.fragment_test);
        title.setText(mTime);
        
        return view;
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("time_key", mTime);
    }
}
于 2012-11-09T10:48:59.423 に答える
21

向きの変更とアクティビティのレクリエーションの間でデータを保持する方法に関する優れたガイドラインは、Androidのガイドラインに記載されています。

概要:

  1. フラグメントを保持可能にする:

    setRetainInstance(true);
    
  2. 必要な場合にのみ新しいフラグメントを作成します(または少なくともそこからデータを取得します)

    dataFragment = (DataFragment) fm.findFragmentByTag("data");
    
    // create the fragment and data the first time
    if (dataFragment == null) {
    
于 2015-10-01T13:33:31.260 に答える