2016-04-18 18 views
0

Im Moment funktioniert meine App gut im normalen Bildschirm. Ich habe eine schwierige Zeit herauszufinden, wie ich meine aktuelle Benutzeroberfläche verwalten kann, um die zwei Panel-Layout zu behandeln.Android Two Pane Layout, keine Ansicht gefunden

Im Moment hat meine DetailActivity einige UI-Elemente zu laden, 3 Fragmente auf separaten Registerkarten, steuert, was jeder FAB tut, FAB-Animation und einen Fragmentseitenadapter für das Registerkartenlayout.

Was muss ich tun, um alle diese Elemente und Logiken in einer separaten Klasse und auch in der Hauptaktivität für das Zwei-Fenster-Layout zu verwalten?

Hier ist meine Hauptaktivität

public class MainActivity extends AppCompatActivity { 

public static boolean TWO_PANE; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    if (findViewById(R.id.movie_detail_container) != null) { 
     // The detail container view will be present only in the large-screen layouts 
     // (res/layout-sw600dp). If this view is present, then the activity should be 
     // in two-pane mode. 
     TWO_PANE = true; 
     if (savedInstanceState == null) { 
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 
     } 
    }else{ 
     TWO_PANE = false; 
    } 
    getSupportFragmentManager().beginTransaction() 
      .replace(R.id.container, new FragmentMain()) 
      .commit(); 
} 

Fragment Haupt zeigt einen Film-Raster-Layout, wenn darauf geklickt wird, wenn es normale Telefon eine andere Tätigkeit öffnen, da sonst die Filmdetails an der rechten Ecke angezeigt werden soll.

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Defines the xml file for the fragment 
    View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
    imageAdapter = new ImageAdapter(getContext()); 
    gridView = (GridView) rootView.findViewById(R.id.gridView); 
    gridView.setAdapter(imageAdapter); 
    //Start DetailActivity with the movie details. 
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, 
           int position, long id) { 
      if (MainActivity.TWO_PANE){ 
       Bundle bundle = new Bundle(); 
       bundle.putParcelable(Constants.BUNDLE_CONSTANT, mMovieElements.get(position)); 
       FragmentDetail fragment = new FragmentDetail(); 
       fragment.setArguments(bundle); 
       getChildFragmentManager() 
         .beginTransaction() 
         .replace(R.id.movie_detail_container, fragment) 
         .addToBackStack(null) 
         .commit(); 
      }else{ 
       //Starting detail activity 
       Intent intent = new Intent(getContext(), DetailActivity.class); 
       intent.putExtra(Intent.EXTRA_TEXT, mMovieElements.get(position)); 
       startActivity(intent); 
      } 
     } 
    }); 

    return rootView; 
} 

Land \ Activity-main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:baselineAligned="false" 
android:divider="?android:attr/dividerHorizontal" 
android:orientation="horizontal" 
tools:context=".ui.ui.MainActivity"> 

<!-- 
This layout is activity_main two-pane layout for the Items master/detail flow. 
--> 
<FrameLayout 
    android:id="@+id/container" 
    class = "com.joaonogueira.nmovies.ui.ui.FragmentMain" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="2" 
    tools:layout="@android:layout/list_content" /> 

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:name="com.joaonogueira.nmovies.ui.ui.FragmentDetail" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:layout_weight="4" 
    android:id="@+id/movie_detail_container" 
    tools:context="com.joaonogueira.nmovies.ui.ui.DetailActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fitsSystemWindows="true" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/toolbar_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fitsSystemWindows="true" 
      app:contentScrim="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed" 
      app:expandedTitleGravity="center" 
      > 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/app_bar_height" 
       android:id="@+id/posterImage" 
       android:background="@drawable/nopicture" 
       android:fitsSystemWindows="true" 
       android:scaleType="centerCrop" 
       app:layout_collapseMode="parallax" /> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/AppTheme.PopupOverlay"/> 


     </android.support.design.widget.CollapsingToolbarLayout> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="?actionBarSize"/> 


    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/MovieContainer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 


    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/btn_star_big_off" 
     app:layout_anchor="@id/app_bar" 
     app:layout_anchorGravity="bottom|end" 
     app:backgroundTint="@color/colorPrimaryDark" /> 

</android.support.design.widget.CoordinatorLayout> 

Mein Fragment-Detail nicht an, ich die folgende Fehlermeldung erhalten:

No view found for id 0x7f0c0070 (com.joaonogueira.nmovies:id/movie_detail_container) for fragment FragmentDetail{1b71bc14 #0 id=0x7f0c0070 bundle}

Antwort

0

Sie sind versuchen, ein Fragment aus dem Fragment selbst zu ersetzen

getChildFragmentManager() 
         .beginTransaction() 
         .replace(R.id.movie_detail_container, fragment) 
         .addToBackStack(null) 
         .commit(); 

Aber da nicht in der Fragmentansicht vorhanden ist und stattdessen ein Teil des Aktivitätslayouts ist, erhalten Sie diesen Fehler.

Was Sie tun müssen, ist eine Listener-Schnittstelle in Ihrer Aktivität implementiert und lassen Sie Ihr Fragment zu hören. Schließlich rufen Sie diesen Listener auf, damit Ihre Aktivität ein neues Fragment ersetzt. Wenn Sie nicht wissen, wie das geht, lesen Sie this.

+0

vielen Dank. Alles funktioniert jetzt. Ich dachte, wenn ich getChildFragmentManager verwende, rief ich die vorherige Aktivität an. –

Verwandte Themen