万无一失的方法来处理片段的方向变化方法来、片段、方向

2023-09-11 20:28:12 作者:与寂寞狼狈为奸

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
}

我有一个需要显示两个列表视图并排菜单上左,其右侧的内容,默认情况下,第一个菜单选择,其内容显示在右侧一面。片段显示内容如下

I have a need to display two list views side by side ,menu on left and its content on right side,by default the first menu is selected and its content is displayed on right side.The Fragment that displays content is as below

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被称为两次,有许多例子在那里使用的碎片,因此我是新手在这个领域我无法涉及的例子与我的问题。我需要万无一失的方法处理方向变化更好way.I还没有宣布Android版本:configChanges清单文件,我需要的活性破坏并重新创建,这样我可以在横向mode.Please帮助使用不同的布局,以一种解决方法

The onActivityCreated and onCreateView is called twice ,there are many examples out there using fragments ,hence i am beginner in this segment i am unable relate the example with my problem .I need fool proof way to handle orientation change in a better way.I have NOT declared android:configChanges in manifest file ,i need the activity destroy and recreate so that i can use different layout in landscape mode.Please help to a workaround

推荐答案

您在每次打开屏幕在活动时间内创建一个新的片段的onCreate(); 但是,您也保持旧有 super.onCreate(savedInstanceState); 。因此,也许设置标签,并找到片段,如果它存在,或传递null捆绑到超。

You are creating a new fragment every time you turn the screen in your activity onCreate(); But you are also maintaining the old ones with super.onCreate(savedInstanceState);. So maybe set tag and find the fragment if it exist, or pass null bundle to super.

这我花了一段时间来学习,才能够真正成为一个双向*的 * 的,当你正在使用的东西,如viewpager。

This took me a while to learn and it can really be a bi** when you are working with stuff like viewpager.

我建议你阅读有关片段的,因为这确切的主题涵盖了额外的时间。

I'd recommend you to read about fragments an extra time as this exact topic is covered.

下面是如何处理的碎片定期方向变化的一个例子:

Here is an example of how to handle fragments on a regular orientation change:

活动

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);
    }
}