Skip to content
This repository was archived by the owner on Jun 23, 2022. It is now read-only.

Commit 1ea4116

Browse files
author
Lyla
committed
5.09 Build Two Pane Tablet UI
1 parent 5da9899 commit 1ea4116

File tree

7 files changed

+67
-24
lines changed

7 files changed

+67
-24
lines changed

app/src/main/java/com/example/android/sunshine/app/DetailActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected void onCreate(Bundle savedInstanceState) {
3030
setContentView(R.layout.activity_detail);
3131
if (savedInstanceState == null) {
3232
getSupportFragmentManager().beginTransaction()
33-
.add(R.id.container, new DetailFragment())
33+
.add(R.id.weather_detail_container, new DetailFragment())
3434
.commit();
3535
}
3636
}

app/src/main/java/com/example/android/sunshine/app/DetailFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
145145
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
146146
Log.v(LOG_TAG, "In onCreateLoader");
147147
Intent intent = getActivity().getIntent();
148-
if (intent == null) {
148+
if (intent == null || intent.getData() == null) {
149149
return null;
150150
}
151151

app/src/main/java/com/example/android/sunshine/app/MainActivity.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,32 @@
2626
public class MainActivity extends ActionBarActivity {
2727

2828
private final String LOG_TAG = MainActivity.class.getSimpleName();
29-
private final String FORECASTFRAGMENT_TAG = "FFTAG";
29+
private static final String DETAILFRAGMENT_TAG = "DFTAG";
3030

31+
private boolean mTwoPane;
3132
private String mLocation;
3233

3334
@Override
3435
protected void onCreate(Bundle savedInstanceState) {
35-
mLocation = Utility.getPreferredLocation(this);
3636
super.onCreate(savedInstanceState);
37+
mLocation = Utility.getPreferredLocation(this);
38+
3739
setContentView(R.layout.activity_main);
38-
if (savedInstanceState == null) {
39-
getSupportFragmentManager().beginTransaction()
40-
.add(R.id.container, new ForecastFragment(), FORECASTFRAGMENT_TAG)
41-
.commit();
40+
if (findViewById(R.id.weather_detail_container) != null) {
41+
// The detail container view will be present only in the large-screen layouts
42+
// (res/layout-sw600dp). If this view is present, then the activity should be
43+
// in two-pane mode.
44+
mTwoPane = true;
45+
// In two-pane mode, show the detail view in this activity by
46+
// adding or replacing the detail fragment using a
47+
// fragment transaction.
48+
if (savedInstanceState == null) {
49+
getSupportFragmentManager().beginTransaction()
50+
.replace(R.id.weather_detail_container, new DetailFragment(), DETAILFRAGMENT_TAG)
51+
.commit();
52+
}
53+
} else {
54+
mTwoPane = false;
4255
}
4356
}
4457

@@ -94,8 +107,8 @@ protected void onResume() {
94107
super.onResume();
95108
String location = Utility.getPreferredLocation( this );
96109
// update the location in our second pane using the fragment manager
97-
if (location != null && !location.equals(mLocation)) {
98-
ForecastFragment ff = (ForecastFragment)getSupportFragmentManager().findFragmentByTag(FORECASTFRAGMENT_TAG);
110+
if (location != null && !location.equals(mLocation)) {
111+
ForecastFragment ff = (ForecastFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_forecast);
99112
if ( null != ff ) {
100113
ff.onLocationChanged();
101114
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<LinearLayout 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+
android:baselineAligned="false"
6+
android:divider="?android:attr/dividerHorizontal"
7+
android:orientation="horizontal"
8+
tools:context="com.example.android.sunshine.app.MainActivity">
9+
10+
<!--
11+
This layout is a two-pane layout for the Items master/detail flow.
12+
-->
13+
14+
<fragment
15+
android:id="@+id/fragment_forecast"
16+
android:name="com.example.android.sunshine.app.ForecastFragment"
17+
android:layout_width="0dp"
18+
android:layout_height="match_parent"
19+
android:layout_weight="2"
20+
tools:layout="@android:layout/list_content" />
21+
22+
<FrameLayout
23+
android:id="@+id/weather_detail_container"
24+
android:layout_width="0dp"
25+
android:layout_height="match_parent"
26+
android:layout_weight="4" />
27+
28+
</LinearLayout>
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2-
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
3-
android:layout_width="match_parent" android:layout_height="match_parent"
4-
tools:context="com.example.android.sunshine.app.DetailActivity" tools:ignore="MergeRootFrame" />
5-
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:id="@+id/weather_detail_container"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
tools:context="com.example.android.sunshine.app.DetailActivity"
7+
tools:ignore="MergeRootFrame" />
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2-
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
3-
android:layout_width="match_parent" android:layout_height="match_parent"
4-
tools:context=".MainActivity" tools:ignore="MergeRootFrame" />
1+
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:id="@+id/fragment_forecast"
4+
android:name="com.example.android.sunshine.app.ForecastFragment"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:layout_marginLeft="16dp"
8+
android:layout_marginRight="16dp"
9+
tools:context="com.example.android.sunshine.app.ForecastFragment"
10+
tools:layout="@android:layout/list_content" />

app/src/main/res/values-w820dp/dimens.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)