首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在方向改变时处理碎片的简单方法

在方向改变时处理碎片的简单方法
EN

Stack Overflow用户
提问于 2012-11-09 18:01:13
回答 2查看 86.3K关注 0票数 88
代码语言:javascript
复制
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
}

我需要并排显示两个列表视图,菜单在左侧,其内容在右侧。默认情况下,第一个菜单处于选中状态,其内容显示在右侧。显示内容的片段如下:

代码语言:javascript
复制
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称为两次。有很多使用片段的例子。因为我是这门课的初学者,我不能把这个例子和我的问题联系起来。我需要一个愚蠢的方法来处理方向的改变。我没有在清单文件中声明android:configChanges。我需要活动销毁和重新创建,以便我可以在景观模式下使用不同的布局。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-09 18:48:59

每次在activity onCreate();中翻转屏幕时,您都会创建一个新的片段,但使用super.onCreate(savedInstanceState);也会维护旧的片段。因此,可以设置tag并找到存在的片段,或者将null bundle传递给super。

这花了我一段时间来学习,当你使用viewpager之类的东西时,这真的是一件痛苦的事情。

我建议你多花点时间阅读关于fragments的文章,因为这个话题已经讲到了。

以下是如何在常规方向更改中处理片段的示例:

活动

代码语言:javascript
复制
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");
        }
    }
}

片段

代码语言:javascript
复制
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);
    }
}
票数 133
EN

Stack Overflow用户

发布于 2015-10-01 21:33:31

关于如何在定向变化和活动娱乐之间保留数据,可以在in android guidelines中找到一个很好的指南。

摘要:

  1. 使您的片段可保留:

仅在必要时才对新片段执行setRetainInstance(true);

  • Create操作(或至少从中提取数据)

dataFragment = (DataFragment) fm.findFragmentByTag(" data ");//如果(dataFragment == null) {,则第一次创建分片和数据

票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13305861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档