2017-02-10 27 views
0

我有一个活动che包含一个片段,我称之为片段A. 当屏幕足够大时,(layout-large)A显示另外两个片段,B(产品列表)和C(详细信息从列表中选择的项目)。 当屏幕不够大时,A只显示片段B,当我单击某个列表项时,它会打开片段C并显示所选产品。如何管理方向变化的两个片段?

问题是,当在肖像模式下,片段C是可见的(片段vith细节),如果我改变方向,它回来可见的片段B(列表),但我想维持片段C可见,在横向模式下。

我该如何做到这一点?

下面是一些代码:

大屏幕

<?xml version="1.0" encoding="utf-8"?> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" 
    android:id="@+id/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <FrameLayout 
     android:id="@+id/book_list_content" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 

    <FrameLayout 
     android:id="@+id/book_details_content" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="3"/> 

</LinearLayout> 

普通屏幕

<?xml version="1.0" encoding="utf-8"?> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <FrameLayout 
     android:id="@+id/book_list_content" 
     android:layout_below="@id/tool_bar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 


</LinearLayout> 

而在最后的片段答:

public class HomeFragment extends Fragment implements ListFragment.onBookSelectedListener { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedIstance){ 
     return inflater.inflate(R.layout.drawer_fragments,container,false); 
     /*drawer_fragments is the name of the layout that i've posted above*/ 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState){ 
     if(savedInstanceState == null) { 
      ListFragment listfrag = new ListFragment(); 
      FragmentManager fragManager = getChildFragmentManager(); 
      fragManager.beginTransaction().replace(R.id.book_list_content, listfrag).commit(); 
      if (view.findViewById(R.id.book_details_content) != null) { 
        /*Here if the screen is large enought to see also the details*/ 
       fragManager.beginTransaction().replace(R.id.book_details_content, new DetailsFragment()).commit(); 
      } 
     } 

    /*Other code...*/ 

    } 

回答

0

在你Fragment_One

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

     frameLayout = new FrameLayout(getActivity()); 
     rootView = inflater.inflate(R.layout.fragment_home_page_updated, null); 
     frameLayout.addView(rootView); 
     initUI(); 

     return frameLayout; 
    } 

,并onConfigurationChange()

@Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     setRetainInstance(true); 
     frameLayout.removeAllViews(); 
     LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     rootView = inflater.inflate(R.layout.fragment_home_page_updated, null); 
     frameLayout.addView(rootView); 
     initUI(); 
    } 
+0

它的行为以同样的方式,也许我已经把里面的东西initUI特别?初始化initUI()中的 – Ollaw

+0

,初始化所有TextView,Button和全部。 – Shekhar