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

Commit

Permalink
5.14 Coding Task for Today Item on Tablet
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyla committed Mar 4, 2015
1 parent 04188f7 commit b920522
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Expand Up @@ -19,6 +19,9 @@ public class ForecastAdapter extends CursorAdapter {
private static final int VIEW_TYPE_TODAY = 0;
private static final int VIEW_TYPE_FUTURE_DAY = 1;

// Flag to determine if we want to use a separate view for "today".
private boolean mUseTodayLayout = true;

/**
* Cache of the children views for a forecast list item.
*/
Expand Down Expand Up @@ -109,9 +112,13 @@ public void bindView(View view, Context context, Cursor cursor) {
viewHolder.lowTempView.setText(Utility.formatTemperature(context, low, isMetric));
}

public void setUseTodayLayout(boolean useTodayLayout) {
mUseTodayLayout = useTodayLayout;
}

@Override
public int getItemViewType(int position) {
return position == 0 ? VIEW_TYPE_TODAY : VIEW_TYPE_FUTURE_DAY;
return (position == 0 && mUseTodayLayout) ? VIEW_TYPE_TODAY : VIEW_TYPE_FUTURE_DAY;
}

@Override
Expand Down
Expand Up @@ -41,6 +41,7 @@ public class ForecastFragment extends Fragment implements LoaderManager.LoaderCa

private ListView mListView;
private int mPosition = ListView.INVALID_POSITION;
private boolean mUseTodayLayout;

private static final String SELECTED_KEY = "selected_position";

Expand Down Expand Up @@ -160,6 +161,8 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon
mPosition = savedInstanceState.getInt(SELECTED_KEY);
}

mForecastAdapter.setUseTodayLayout(mUseTodayLayout);

This comment has been minimized.

Copy link
@farhanarnob

farhanarnob Sep 29, 2017

Lesson 8, the answer of 34 lecture, In Forcast Fragment, Katherine Kuan mam is saying that MainActivity's onCreate method will happen before ForecastFragment's onCreateView Method. But, the reality is, MainActivity's onCreate method will call before ForecastFragment's onCreateView but before finishing MainActivity's onCreate method the ForecastFragment's onCreateView will finish. And so, setUseFragmentLayout will call twice when MainActivity's onCreate method will finish. And the biggest problem is that every first-time useTodayLayout will remain false.

So, there is no need of calling mForecastAdapter.setUseTodayLayout(mUseTodayLayout); in ForecastFragment.

Am I wrong? Please help me.


return rootView;
}

Expand Down Expand Up @@ -226,7 +229,14 @@ public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
}

@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
public void onLoaderReset(Loader<Cursor> loader) {
mForecastAdapter.swapCursor(null);
}

public void setUseTodayLayout(boolean useTodayLayout) {
mUseTodayLayout = useTodayLayout;
if (mForecastAdapter != null) {
mForecastAdapter.setUseTodayLayout(mUseTodayLayout);
}
}
}
Expand Up @@ -53,6 +53,10 @@ protected void onCreate(Bundle savedInstanceState) {
} else {
mTwoPane = false;
}

ForecastFragment forecastFragment = ((ForecastFragment)getSupportFragmentManager()
.findFragmentById(R.id.fragment_forecast));
forecastFragment.setUseTodayLayout(!mTwoPane);
}

@Override
Expand Down

0 comments on commit b920522

Please sign in to comment.