2017-02-10 4 views
0

Ich habe eine Aktivität che enthält ein Fragment, das ich Fragment A nennen. Wenn der Bildschirm groß genug ist, (Layout-groß) A zeigt zwei andere Fragment, B (eine Liste von Produkten) und C (die Details des ausgewählten Artikels aus der Liste). Wenn der Bildschirm nicht groß genug ist, zeigt A nur das Fragment B, und wenn ich auf ein Listenelement klicke, wird das Fragment C mit dem ausgewählten Produkt geöffnet. Das Problem ist, dass, wenn im Portrait-Modus das Fragment C sichtbar ist (das Fragment vith Details), wenn ich die Orientierung ändere, es kommt wieder sichtbar das Fragment B (die Liste), aber ich möchte das Fragment C behalten sichtbar, im Querformat.Wie man zwei Fragmente bei Orientierungsänderung verwaltet?

Wie kann ich das erreichen?

Hier einige Code:

Großbild

<?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> 

Normalbild

<?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> 

Und in dem das Fragment A Ende:

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...*/ 

    } 

Antwort

0

In Ihrem 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; 
    } 

und onConfigurationChange machen()

@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

Es verhält sich in der gleichen Art und Weise, vielleicht muss ich etwas Besonderes innerhalb initUI setzen? – Ollaw

+0

in initUI(), initialisieren Sie alle TextView, Button und alle. – Shekhar

Verwandte Themen