DrawerLayout beim Wechseln in den Portrait Modus

  • Antworten:32
Norman D.
  • Forum-Beiträge: 30

03.03.2014, 15:08:38 via Website

Hallo,

ich habe ein Fragment, welches zwei Fragments beeinhaltet ( Fragment A links, Fragment B rechts)
Ich möchte nun, wenn ich in den Portrait-Modus wechsle Fragment A ausblenden und als Left-Drawer einblenden lassen.
Mein xml-File habe ich nun auf ein DrawerLayout angepasst, allerdings funktioniert es nicht wirklich.
Was genau muss ich im Java-Code noch vornehmen, sodass ich den Drawer einblenden lassen kann?
Vielen Dank schon einmal im Voraus

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

03.03.2014, 15:54:55 via Website

Wie sieht dein jetziger Code aus?

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Norman D.
  • Forum-Beiträge: 30

03.03.2014, 16:11:10 via Website

Danke schonmal :)

Hier die Main:


package com.example.navigationdrawerdemoapp;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.widget.ListView;

public class NavigationDrawerMainActivity extends FragmentActivity {

private DrawerLayout myDrawerLayout;
private ListView myDrawerList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer_main);


FragmentA fragmentleft = new FragmentA();
FragmentB fragmentright = new FragmentB();
FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();
FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();

transaction1.replace(R.id.framelayout_left, fragmentleft);
transaction2.replace(R.id.framelayout_right, fragmentright);
transaction1.commit();
transaction2.commit();

myDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
myDrawerList = (ListView) findViewById(R.id.framelayout_left);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation_drawer_main, menu);
return true;
}

}

Portrait XML:

<android.support.v4.widget.DrawerLayout
xmlns:android= musste ich wegen externer Verlinkung entfernen
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
android:id="@+id/framelayout_right"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<ListView
android:id="@+id/framelayout_left"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>







<RelativeLayout xmlns:android= Musste wegen Verlinkung entfernt werden
xmlns:tools= Musste wegen Verlinkung entfernt werden
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".NavigationDrawerMainActivity" >

<FrameLayout
android:id="@+id/framelayout_left"
android:layout_width="500dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"/>

<FrameLayout
android:id="@+id/framelayout_right"
android:layout_width="300dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true" />

</RelativeLayout>












Das Linke Fragment ist ne einfache ListView, das rechte Fragment einfach ein leeres Fragment mit nem Demo Text.


Danke für die Hilfe :)

— geändert am 03.03.2014, 16:14:22

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

03.03.2014, 16:20:22 via Website

Du musst die Orientation des Gerätes Prüfen und darauf dann das Drawer layout unsichtbar (visible = false ) stellen

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Norman D.
  • Forum-Beiträge: 30

04.03.2014, 08:58:23 via Website

Hallo,

vielen Dank schonmal für die Antwort.
Ich habe jetzt mal folgendes versucht.


package com.example.navigationdrawerdemoapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.widget.ListView;

public class NavigationDrawerMainActivity extends FragmentActivity {

private DrawerLayout myDrawerLayout;
private ListView myDrawerList;
int myOrientation;

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
switch (orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
myOrientation = 1;
break;
case Configuration.ORIENTATION_PORTRAIT:
myOrientation = 2;
break;

}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer_main);

if (myOrientation == 1) {
FragmentA fragmentleft = new FragmentA();
FragmentB fragmentright = new FragmentB();
FragmentTransaction transaction1 = getSupportFragmentManager()
.beginTransaction();
FragmentTransaction transaction2 = getSupportFragmentManager()
.beginTransaction();

transaction1.replace(R.id.framelayout_left, fragmentleft);
transaction2.replace(R.id.framelayout_right, fragmentright);
transaction1.commit();
transaction2.commit();

} else if (myOrientation == 2) {
createNavigationDrawer();
}

}

private void createNavigationDrawer() {
myDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
myDrawerList = (ListView) findViewById(R.id.framelayout_left_drawer);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation_drawer_main, menu);
return true;
}

}





Irgendwie scheint es aber nicht zu gehen?

Antworten
Norman D.
  • Forum-Beiträge: 30

04.03.2014, 11:03:38 via Website

Ich habe es jetzt so gelöst:

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
orientation = newConfig.orientation;
switch (orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
FragmentA fragmentleft = new FragmentA();
FragmentB fragmentright = new FragmentB();
FragmentTransaction transaction1 = getSupportFragmentManager()
.beginTransaction();
FragmentTransaction transaction2 = getSupportFragmentManager()
.beginTransaction();

transaction1.replace(R.id.framelayout_left, fragmentleft);
transaction2.replace(R.id.framelayout_right, fragmentright);
transaction1.commit();
transaction2.commit();

break;
case Configuration.ORIENTATION_PORTRAIT:
createNavigationDrawer();
Log.d("TEST", "Portrait");


break;

}
}


Es funktioniert auch soweit, nur das FragmentA muss ich jetzt irgendwie in die ListView des DrawerLayouts bekommen.
Wie mache ich das genau?
Ich bekomme es einfach nicht hin :(

Vielen Dank schonmal im Voraus

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

04.03.2014, 11:06:53 via Website

Wieso muss das Fragment A genau in die List view?
Das geht doch gar nicht. Was ist denn auf dem Fragment drauf?
Wenn dannkannst du es unter die ListView schieben

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Norman D.
  • Forum-Beiträge: 30

04.03.2014, 11:18:07 via Website

Hi,

Es muss nicht genau in die ListView, theoretisch brauche ich die ListView garnicht, da das FragmentA eine ListView ist.
Kann ich statt der ListView einfach ein FrameLayout im DrawerLayout verwenden?

1<android.support.v4.widget.DrawerLayout xmlns:android=...
2 android:id="@+id/drawer_layout"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 >
6
7 <FrameLayout
8 android:id="@+id/framelayout_right"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent" />
11
12
13 <ListView
14 android:id="@+id/framelayout_left_drawer"
15 android:layout_width="240dp"
16 android:layout_height="match_parent"
17 android:layout_gravity="start"
18 android:background="#111"
19 android:choiceMode="singleChoice"
20 android:divider="@android:color/transparent"
21 android:dividerHeight="0dp" />
22
23</android.support.v4.widget.DrawerLayout>

Da er, wenn ich ein FrameLayout verwende und darin das FragmentA inflate, die ID des Framelayouts nicht findet(laut LogCat).

Danke schon einmal im Voraus :)

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

04.03.2014, 11:28:42 via Website

dann benutzt doch einfach ein include für das Layout, oder mach ein LinearLayour draus und führe die Funktionen dann in deiner DrawerLayout klasse aus.
Wie verstuchst du denn, das Layout hinzuzufügen?

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Norman D.
  • Forum-Beiträge: 30

04.03.2014, 11:36:09 via Website

1package com.example.navigationdrawerdemoapp;
2
3import android.os.Bundle;
4import android.app.Activity;
5import android.content.res.Configuration;
6import android.support.v4.app.Fragment;
7import android.support.v4.app.FragmentActivity;
8import android.support.v4.app.FragmentTransaction;
9import android.support.v4.widget.DrawerLayout;
10import android.util.Log;
11import android.view.Menu;
12import android.view.View;
13import android.widget.FrameLayout;
14import android.widget.ListView;
15
16public class NavigationDrawerMainActivity extends FragmentActivity {
17
18 private DrawerLayout myDrawerLayout;
19 private FrameLayout myDrawerList;
20
21 int orientation = 0;
22
23 @Override
24 protected void onCreate(Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.activity_navigation_drawer_main);
27 includeFragments();
28 }
29
30 @Override
31 public void onConfigurationChanged(Configuration newConfig) {
32 super.onConfigurationChanged(newConfig);
33 orientation = newConfig.orientation;
34 switch (orientation) {
35 case Configuration.ORIENTATION_LANDSCAPE:
36 includeFragments();
37 break;
38 case Configuration.ORIENTATION_PORTRAIT:
39 createNavigationDrawer();
40 FragmentB fragmentB = new FragmentB();
41 FragmentTransaction transPortr = getSupportFragmentManager().beginTransaction();
42 transPortr.replace(R.id.container, fragmentB);
43 transPortr.commit();
44 FragmentA fragmentA = new FragmentA();
45 FragmentTransaction transPortrA = getSupportFragmentManager().beginTransaction();
46 transPortrA.replace(R.id.framelayout_left_drawer, fragmentA);
47 transPortrA.commit();
48 Log.d("TEST", "Portrait");
49 break;
50
51 }
52 }
53 private void createNavigationDrawer() {
54 myDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
55 myDrawerList = (FrameLayout) findViewById(R.id.framelayout_left_drawer);
56
57 }
58
59 private void includeFragments() {
60 ContainerFragment fragmentCont = new ContainerFragment();
61 FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
62 trans.replace(R.id.container, fragmentCont);
63 trans.commit();
64 }
65
66 @Override
67 public boolean onCreateOptionsMenu(Menu menu) {
68 // Inflate the menu; this adds items to the action bar if it is present.
69 getMenuInflater().inflate(R.menu.navigation_drawer_main, menu);
70 return true;
71 }
72
73}

Das ist mein Java-Code.

Ich habe ein ContainerFragment erstellt, in dem ich einfach FragmentA und FragmentB inflate, dieses wird in das FrameLayout inflatet, wenn sich das Tablet im Landscape Modus befindet.
Andernfalls wird einfach nur das FragmentB inflatet, allerdings mein FragmentA, nicht wie es sollte :(

1package com.example.navigationdrawerdemoapp;
2
3import android.os.Bundle;
4import android.support.v4.app.Fragment;
5import android.support.v4.app.FragmentTransaction;
6import android.view.LayoutInflater;
7import android.view.View;
8import android.view.ViewGroup;
9
10/**
11 * A simple {@link android.support.v4.app.Fragment} subclass.
12 *
13 */
14public class ContainerFragment extends Fragment {
15
16 public ContainerFragment() {
17 // Required empty public constructor
18 }
19
20 @Override
21 public View onCreateView(LayoutInflater inflater, ViewGroup container,
22 Bundle savedInstanceState) {
23 // Inflate the layout for this fragment
24 return inflater.inflate(R.layout.fragment_container, container, false);
25 }
26
27 @Override
28 public void onViewCreated(View view, Bundle savedInstanceState) {
29
30 super.onViewCreated(view, savedInstanceState);
31
32 FragmentA fragmentleft = new FragmentA();
33 FragmentB fragmentright = new FragmentB();
34 FragmentTransaction transaction1 = getActivity().getSupportFragmentManager()
35 .beginTransaction();
36 FragmentTransaction transaction2 = getActivity().getSupportFragmentManager()
37 .beginTransaction();
38
39 transaction1.replace(R.id.framelayout_left, fragmentleft);
40 transaction2.replace(R.id.framelayout_right, fragmentright);
41 transaction1.commit();
42 transaction2.commit();
43 }
44
45}

Normalerweise müsste das doch aber funktionieren, oder nicht?

Antworten
Norman D.
  • Forum-Beiträge: 30

04.03.2014, 13:58:56 via Website

Ich habe jetzt mal zum Testen eine weitere App erstellt, welche nur den Drawer beeinhaltet.

1package com.example.simpledrawer;
2
3import android.os.Bundle;
4import android.support.v4.app.FragmentActivity;
5import android.support.v4.app.FragmentTransaction;
6import android.support.v4.widget.DrawerLayout;
7import android.view.Menu;
8import android.widget.ArrayAdapter;
9import android.widget.FrameLayout;
10import android.widget.ListView;
11
12public class SimpleDrawerMain extends FragmentActivity {
13
14 private DrawerLayout mDrawerLayout;
15 private FrameLayout mDrawerList;
16
17 @Override
18 public void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.activity_simple_drawer_main);
21
22
23 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
24 mDrawerList = (FrameLayout) findViewById(R.id.left_drawer);
25
26 MyDrawerFragment fragment = new MyDrawerFragment();
27 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
28 transaction.replace(R.id.left_drawer, fragment);
29 transaction.commit();
30
31
32 }
33}

Das funktioniert auch. Ich bekomme mein Fragment als Drawer angezeigt, wenn ich den Drawer "reinwische".
Aber in der richtigen App funktioniert es leider nicht.
In der LogCat steht, dass er kein FrameLayout mit der ID finden kann, allerdings hat das FrameLayout die richtige ID.

— geändert am 04.03.2014, 13:59:46

Antworten
Norman D.
  • Forum-Beiträge: 30

05.03.2014, 09:42:33 via Website

1package com.example.navigationdrawerdemoapp;
2
3import android.os.Bundle;
4import android.app.Activity;
5import android.content.res.Configuration;
6import android.support.v4.app.Fragment;
7import android.support.v4.app.FragmentActivity;
8import android.support.v4.app.FragmentTransaction;
9import android.support.v4.widget.DrawerLayout;
10import android.util.Log;
11import android.view.Menu;
12import android.view.View;
13import android.widget.FrameLayout;
14import android.widget.ListView;
15
16public class NavigationDrawerMainActivity extends FragmentActivity {
17
18 private DrawerLayout myDrawerLayout;
19 private FrameLayout myDrawerList;
20
21 int orientation = 0;
22
23 @Override
24 protected void onCreate(Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.activity_navigation_drawer_main);
27 includeFragments();
28
29
30 }
31
32 @Override
33 public void onConfigurationChanged(Configuration newConfig) {
34 super.onConfigurationChanged(newConfig);
35 orientation = newConfig.orientation;
36 switch (orientation) {
37 case Configuration.ORIENTATION_LANDSCAPE:
38 includeFragments();
39 break;
40 case Configuration.ORIENTATION_PORTRAIT:
41 myDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
42 myDrawerList = (FrameLayout) findViewById(R.id.left_drawer);
43 FragmentB fragmentB = new FragmentB();
44 FragmentTransaction transPortr = getSupportFragmentManager().beginTransaction();
45 transPortr.replace(R.id.container, fragmentB);
46 transPortr.commit();
47 FragmentA fragmentA = new FragmentA();
48 FragmentTransaction transPortrA = getSupportFragmentManager().beginTransaction();
49 transPortrA.replace(R.id.left_drawer, fragmentA);
50 transPortrA.commit();
51 Log.d("TEST", "Portrait");
52 break;
53 }
54 }
55
56
57 private void includeFragments() {
58 ContainerFragment fragmentCont = new ContainerFragment();
59 FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
60 trans.replace(R.id.container, fragmentCont);
61 trans.commit();
62 }
63
64 @Override
65 public boolean onCreateOptionsMenu(Menu menu) {
66 // Inflate the menu; this adds items to the action bar if it is present.
67 getMenuInflater().inflate(R.menu.navigation_drawer_main, menu);
68 return true;
69 }
70
71}


Dieser Code funktioniert jetzt soweit, dass ich in der LogCat angezeigt bekomme, in welchem Modus ich mich befinde.
Das Fragment wird allerdings nicht als Drawer angezeigt, wenn ich es reinwischen möchte :(
Was mache ich denn falsch?

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

05.03.2014, 09:53:09 via App

Warum muss es ein Frsgment sein hast du es schon mit einem normalen Layout LinearLayout etc. probiert?

— geändert am 05.03.2014, 09:53:20

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Norman D.
  • Forum-Beiträge: 30

05.03.2014, 15:17:07 via Website

Es ist ein Fragment, welches als Layout ein FrameLayout enthält.

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

05.03.2014, 15:37:00 via Website

Wise muss es ein Fragment sein?
Mach es doch anders.

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Norman D.
  • Forum-Beiträge: 30

06.03.2014, 09:03:07 via Website

Wie meinst du ?
Es sollte ein Fragment sein, da das selbe Fragment zweimal verwendet werden soll.
Also im Landscape Modus sollen die beiden einfach nebeneinander sein, im Portrait Modus sollte das eine Fragment "reinwischbar" sein.

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

06.03.2014, 09:37:46 via App

Das ist aber auch ohne fragment realisierbar, poste mal das layout des fragments und das layout in das es später ein soll

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Norman D.
  • Forum-Beiträge: 30

06.03.2014, 09:58:14 via Website

Layout der Main-Port
1<android.support.v4.widget.DrawerLayout
2 android:id="@+id/drawer_layout"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 >
6
7 <FrameLayout
8 android:id="@+id/framelayout_right"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent" />
11
12
13 <FrameLayout
14 android:id="@+id/left_drawer"
15 android:layout_width="240dp"
16
17 android:layout_height="match_parent"
18 android:layout_gravity="start"
19 android:background="#111"
20 android:choiceMode="singleChoice"
21 android:divider="@android:color/transparent"
22 android:dividerHeight="0dp" />
23
24
25</android.support.v4.widget.DrawerLayout>

Das Fragment soll in das FrameLayout mit der ID left_drawer

1<ListView xmlns:android=
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 tools:context=".FragmentA"
6 android:entries="@array/drawer_entries"
7 android:id="@+id/drawer_list"
8 >
9
10
11
12
13</ListView>


Das ist das Layout für das Fragment

Vielen Dank schon einmal :)

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

06.03.2014, 10:02:17 via Website

Wieso machst du nicht einfach ein Layout include und dann hast du dein ERgebnis?
Du musst doch da erstmal nix anderes machen.
Das ausblenden läuft dann programatisch ab.

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Norman D.
  • Forum-Beiträge: 30

06.03.2014, 10:19:00 via Website

Was genau meinst du mit Layout Include?

Ich habe mal gegoogled und bin auf folgendes gestoßen:

<include android:id="@+id/cell1" layout="@layout/workspace_screen" />


Also wäre das dann einfach
<include android:id="@+id/left_drawer layout="@layout/drawer_list" />

Dieses <include /> dann einfach in das FrameLayout des DrawerLayout's ?

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

06.03.2014, 10:22:08 via Website

Genau einfach damit includen und dann sollte es Funktionieren, aber du musst dann halt schauen, wan du das SideMMenu anzeigen willst und wann nicht.

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Norman D.
  • Forum-Beiträge: 30

06.03.2014, 12:17:06 via Website

Es funktioniert nicht wirklich :(

Folgendes Coding funktioniert, das ist eine andere App, die ich nochmals zum Test erstellt habe.

main.xml

1<android.support.v4.widget.DrawerLayout
2 xmlns:android="xxx"
3 android:id="@+id/drawer_layout"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent">
6 <!-- The main content view -->
7 <FrameLayout
8 android:id="@+id/content_frame"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent" />
11 <!-- The navigation drawer -->
12
13
14 <FrameLayout android:id="@+id/left_drawer"
15 android:layout_width="240dp"
16 android:layout_height="match_parent"
17 android:layout_gravity="start"
18 android:choiceMode="singleChoice"
19 android:divider="@android:color/transparent"
20 android:dividerHeight="0dp"
21 android:background="#111"/>
22</android.support.v4.widget.DrawerLayout>



MainActivity

1package com.example.simpledrawer;
2
3import android.os.Bundle;
4import android.support.v4.app.FragmentActivity;
5import android.support.v4.app.FragmentTransaction;
6import android.support.v4.widget.DrawerLayout;
7import android.view.Menu;
8import android.widget.ArrayAdapter;
9import android.widget.FrameLayout;
10import android.widget.ListView;
11
12public class SimpleDrawerMain extends FragmentActivity {
13 private String[] mPlanetTitles;
14 private DrawerLayout mDrawerLayout;
15 private FrameLayout mDrawerList;
16
17 @Override
18 public void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.activity_simple_drawer_main);
21
22 mPlanetTitles = getResources().getStringArray(R.array.entries);
23 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
24 mDrawerList = (FrameLayout) findViewById(R.id.left_drawer);
25
26 MyDrawerFragment fragment = new MyDrawerFragment();
27 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
28 transaction.replace(R.id.left_drawer, fragment);
29 transaction.commit();
30 // Set the adapter for the list view
31// mDrawerList.setAdapter(new ArrayAdapter<String>(this,
32// R.layout.drawer_list_item, mPlanetTitles));
33 // Set the list's click listener
34 // mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
35
36 }
37}

Allerdings bekomme ich den Drawer jetzt auch eingeflogen, wenn ich das Layout im Landscape Modus habe.
Irgendwas ist da sehr komisch :(

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

06.03.2014, 12:51:04 via Website

Nein da ist nix komsich, du hast den Drawer ja dauerhaft eingebunden, jetzt musst du ihn nurnoch ausblenden, wenn landscapeModus.
Pseudocode wie ich das gedacht hatte:
1//BildschirmOrientation Prüfen
2If(Orientation.getScreenOrientation() == Orientation.landscape) //Code ist nicht richtig, keine Ahnung wie man das Abfragt
3{
4 mDrawerList.setVisibility(false); //FrameLayout ausbelenden
5}

PS: Hast genau beim Gleichen Tutorial nachgeschaut wie ich, als ich einen NavigationDrawer gebraucht habe :)

— geändert am 06.03.2014, 12:51:23

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Norman D.
  • Forum-Beiträge: 30

06.03.2014, 14:00:31 via Website

Das habe ich ja hier:


1@Override
2 public void onConfigurationChanged(Configuration newConfig) {
3 super.onConfigurationChanged(newConfig);
4 orientation = newConfig.orientation;
5 switch (orientation) {
6 case Configuration.ORIENTATION_LANDSCAPE:
7 includeFragments();
8 break;
9 case Configuration.ORIENTATION_PORTRAIT:
10 myDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
11 myDrawerList = (FrameLayout) findViewById(R.id.left_drawer);
12 FragmentB fragmentB = new FragmentB();
13 FragmentTransaction transPortr = getSupportFragmentManager().beginTransaction();
14 transPortr.replace(R.id.container, fragmentB);
15 transPortr.commit();
16 FragmentA fragmentA = new FragmentA();
17 FragmentTransaction transPortrA = getSupportFragmentManager().beginTransaction();
18 transPortrA.replace(R.id.left_drawer, fragmentA);
19 transPortrA.commit();
20 Log.d("TEST", "Portrait");
21 break;
22 }
23 }


Er schreibt mir auch in die Log, auch wird beim Drehen des Gerätes nur das Fragment B angezeigt, allerdings kann ich den Drawer nicht reinwischen :(

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

06.03.2014, 14:06:30 via Website

Das kommt davon dass Drawer b anders als A included wird oder?

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Norman D.
  • Forum-Beiträge: 30

06.03.2014, 14:12:07 via Website

Wie meinst du?

Hier mal die gesamte Main

1public class NavigationDrawerMainActivity extends FragmentActivity {
2
3 private DrawerLayout myDrawerLayout;
4 private FrameLayout myDrawerList;
5
6 int orientation = 0;
7
8 @Override
9 protected void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.activity_navigation_drawer_main);
12 includeFragments();
13
14
15 }
16
17 @Override
18 public void onConfigurationChanged(Configuration newConfig) {
19 super.onConfigurationChanged(newConfig);
20 orientation = newConfig.orientation;
21 switch (orientation) {
22 case Configuration.ORIENTATION_LANDSCAPE:
23 includeFragments();
24 break;
25 case Configuration.ORIENTATION_PORTRAIT:
26 myDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
27 myDrawerList = (FrameLayout) findViewById(R.id.left_drawer);
28 FragmentB fragmentB = new FragmentB();
29 FragmentTransaction transPortr = getSupportFragmentManager().beginTransaction();
30 transPortr.replace(R.id.container, fragmentB);
31 transPortr.commit();
32 FragmentA fragmentA = new FragmentA();
33 FragmentTransaction transPortrA = getSupportFragmentManager().beginTransaction();
34 transPortrA.replace(R.id.left_drawer, fragmentA);
35 transPortrA.commit();
36 Log.d("TEST", "Portrait");
37 break;
38 }
39 }
40
41
42 private void includeFragments() {
43 ContainerFragment fragmentCont = new ContainerFragment();
44 FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
45 trans.replace(R.id.container, fragmentCont);
46 trans.commit();
47
48 }
49
50 @Override
51 public boolean onCreateOptionsMenu(Menu menu) {
52 // Inflate the menu; this adds items to the action bar if it is present.
53 getMenuInflater().inflate(R.menu.navigation_drawer_main, menu);
54 return true;
55 }
56
57}

xml-landscape:

1<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 tools:context=".ContainerFragment" >
6
7 <!-- TODO: Update blank fragment layout -->
8 <FrameLayout
9 android:id="@+id/container"
10 android:layout_width="match_parent"
11 android:layout_height="match_parent"
12
13 />
14 <FrameLayout
15 android:id="@+id/framelayout_right"
16 android:layout_width="match_parent"
17 android:layout_height="match_parent"
18 android:visibility="invisible"
19 />
20 <FrameLayout
21 android:id="@+id/left_drawer"
22 android:layout_width="match_parent"
23 android:layout_height="match_parent"
24 android:visibility="invisible"
25 />
26
27</FrameLayout>


xml Portrait Layout

1<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:id="@+id/drawer_layout"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 >
6
7 <FrameLayout
8 android:id="@+id/framelayout_right"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent" />
11
12
13 <FrameLayout
14 android:id="@+id/left_drawer"
15 android:layout_width="240dp"
16
17 android:layout_height="match_parent"
18 android:layout_gravity="start"
19 android:background="#111"
20 android:choiceMode="singleChoice"
21 android:divider="@android:color/transparent"
22 android:dividerHeight="0dp" />
23 <include
24 android:id="@+id/left_drawer"
25 layout="@layout/fragment_fragment_a"
26 />
27
28
29</android.support.v4.widget.DrawerLayout>

Antworten
Andreas S.
  • Forum-Beiträge: 76

06.03.2014, 23:43:35 via Website

Im Prinzip ist es nicht so schwer, wenn man das Auswählen des Layouts Android überlässt.

Funktionierendes Beispiel: Sourcecode und APK:

http://www.file-upload.net/download-8688717/DrawerExample.zip.html

Wenn weitere Erklärung nötig ist bzw. du Fragen zum Code hast, dann helf ich gerne weiter.

Antworten
Norman D.
  • Forum-Beiträge: 30

07.03.2014, 08:11:05 via Website

Vielen Dank schon einmal für deine Antwort.
Aber du inflatest ja auch das Fragment(welches als Layout eine ListView beinhaltet) in ein FrameLayout(als Drawer).
Was genau ist an meinem Coding denn falsch?

Antworten
Norman D.
  • Forum-Beiträge: 30

07.03.2014, 13:02:25 via Website

Hallo,

ich habe jetzt eine erneute Demo App erstellt, wo es funktioniert.
Allerdings habe ich zwei Fragments, in der Richtigen App habe ich ja ein Fragment, welches als Container für die beiden anderen Fragments dient.
Ist es Möglich, dass ich in diesem Container (Ist ein Fragment) auf das Ändern der Orientation reagiere und das DrawerLayout als Layout des Fragments und nicht der Activity habe?

Antworten
Andreas S.
  • Forum-Beiträge: 76

07.03.2014, 22:19:54 via App

Warum ist ein Fragment als Parent für die zwei Fragmente notwendig?

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

07.03.2014, 22:53:26 via Website

Was meinst du mit notwendig?
Ein Fragment muss nicht unbedingt ein Parent von zwei anderen sein
Ein Fragment ist eigentlich ein ganz normales Layout, hat aber eine eigene klasse, die Für die Ansteuerung und benutereingabenüberprüfung da ist. Wenn du nun ein neues "Fragment" erstellst, inflatest du in Wirklichkeit nur dein Layout und lässt es von einer klasse behandeln.
Diese View kannst du jetzt noch mit einer anderen in Main Layout ersetzen und fertig.
Wenn ich dich richtig verstanden habe, dann hast du ein Parent Layout (vlt. linear) in diesem befinden sich zwei andere, die zufällig als Fragment deklariert sind und eine eigene Ansteuerungsklasse haben.
Theoretisch kannst du das ganze mit den Fragmenten auch weglassen wenn es sich verwirrt. Dann bindest du die Drawer mittels XML include tag ins layout ein und kannst die dann in der Activity oder in der dafür zuständigen klasse ausblenden und auch einblenden.

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Norman D.
  • Forum-Beiträge: 30

12.03.2014, 10:35:19 via Website

Hallo,

vielen Dank :)

Ich werde mir das Video mal anschauen! :)

Antworten