Starting point for RecyclerView.

This commit is contained in:
Dan Galpin
2015-05-25 02:57:21 -07:00
parent 4495e7a567
commit dbff5f9982
2 changed files with 41 additions and 64 deletions

View File

@@ -43,18 +43,18 @@ public class ForecastAdapter extends CursorAdapter {
* Cache of the children views for a forecast list item. * Cache of the children views for a forecast list item.
*/ */
public static class ViewHolder { public static class ViewHolder {
public final ImageView iconView; public final ImageView mIconView;
public final TextView dateView; public final TextView mDateView;
public final TextView descriptionView; public final TextView mDescriptionView;
public final TextView highTempView; public final TextView mHighTempView;
public final TextView lowTempView; public final TextView mLowTempView;
public ViewHolder(View view) { public ViewHolder(View view) {
iconView = (ImageView) view.findViewById(R.id.list_item_icon); mIconView = (ImageView) view.findViewById(R.id.list_item_icon);
dateView = (TextView) view.findViewById(R.id.list_item_date_textview); mDateView = (TextView) view.findViewById(R.id.list_item_date_textview);
descriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview); mDescriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview);
highTempView = (TextView) view.findViewById(R.id.list_item_high_textview); mHighTempView = (TextView) view.findViewById(R.id.list_item_high_textview);
lowTempView = (TextView) view.findViewById(R.id.list_item_low_textview); mLowTempView = (TextView) view.findViewById(R.id.list_item_low_textview);
} }
} }
@@ -91,55 +91,53 @@ public class ForecastAdapter extends CursorAdapter {
ViewHolder viewHolder = (ViewHolder) view.getTag(); ViewHolder viewHolder = (ViewHolder) view.getTag();
int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID); int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID);
int fallbackIconId; int defaultImage;
int viewType = getItemViewType(cursor.getPosition());
switch (viewType) { switch (getItemViewType(cursor.getPosition())) {
case VIEW_TYPE_TODAY: { case VIEW_TYPE_TODAY:
// Get weather icon defaultImage = Utility.getArtResourceForWeatherCondition(weatherId);
fallbackIconId = Utility.getArtResourceForWeatherCondition(
weatherId);
break; break;
} default:
default: { defaultImage = Utility.getIconResourceForWeatherCondition(weatherId);
// Get weather icon
fallbackIconId = Utility.getIconResourceForWeatherCondition(
weatherId);
break;
}
} }
Glide.with(mContext) if (Utility.usingLocalGraphics(mContext)) {
.load(Utility.getArtUrlForWeatherCondition(mContext, weatherId)) viewHolder.mIconView.
.error(fallbackIconId) setImageResource(defaultImage);
.crossFade() } else {
.into(viewHolder.iconView); Glide.with(mContext)
.load(Utility.getArtUrlForWeatherCondition(mContext, weatherId))
.error(defaultImage)
.crossFade()
.into(viewHolder.mIconView);
}
// Read date from cursor // Read date from cursor
long dateInMillis = cursor.getLong(ForecastFragment.COL_WEATHER_DATE); long dateInMillis = cursor.getLong(ForecastFragment.COL_WEATHER_DATE);
// Find TextView and set formatted date on it // Find TextView and set formatted date on it
viewHolder.dateView.setText(Utility.getFriendlyDayString(context, dateInMillis)); viewHolder.mDateView.setText(Utility.getFriendlyDayString(context, dateInMillis));
// Get description from weather condition ID // Read weather forecast from cursor
String description = Utility.getStringForWeatherCondition(context, weatherId); String description = Utility.getStringForWeatherCondition(mContext, weatherId);
// Find TextView and set weather forecast on it // Find TextView and set weather forecast on it
viewHolder.descriptionView.setText(description); viewHolder.mDescriptionView.setText(description);
viewHolder.descriptionView.setContentDescription(context.getString(R.string.a11y_forecast, description)); viewHolder.mDescriptionView.setContentDescription(mContext.getString(R.string.a11y_forecast, description));
// For accessibility, we don't want a content description for the icon field // For accessibility, we don't want a content description for the icon field
// because the information is repeated in the description view and the icon // because the information is repeated in the description view and the icon
// is not individually selectable // is not individually selectable
// Read high temperature from cursor // Read high temperature from cursor
String high = Utility.formatTemperature( double high = mCursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP);
context, cursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP)); String highString = Utility.formatTemperature(mContext, high);
viewHolder.highTempView.setText(high); viewHolder.mHighTempView.setText(highString);
viewHolder.highTempView.setContentDescription(context.getString(R.string.a11y_high_temp, high)); viewHolder.mHighTempView.setContentDescription(mContext.getString(R.string.a11y_high_temp, highString));
// Read low temperature from cursor // Read low temperature from cursor
String low = Utility.formatTemperature( double low = mCursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP);
context, cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP)); String lowString = Utility.formatTemperature(mContext, low);
viewHolder.lowTempView.setText(low); viewHolder.mLowTempView.setText(lowString);
viewHolder.lowTempView.setContentDescription(context.getString(R.string.a11y_low_temp, low)); viewHolder.mLowTempView.setContentDescription(mContext.getString(R.string.a11y_low_temp, lowString));
} }
public void setUseTodayLayout(boolean useTodayLayout) { public void setUseTodayLayout(boolean useTodayLayout) {

View File

@@ -13,31 +13,10 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
--> -->
<FrameLayout <fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<ImageView
android:layout_width="wrap_content"
android:layout_height="?attr/listPreferredItemHeight"
android:layout_marginTop="?attr/actionBarSize"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_logo"
android:scaleType="center"
android:background="@color/primary"/>
</android.support.v7.widget.Toolbar>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_forecast" android:id="@+id/fragment_forecast"
android:name="com.example.android.sunshine.app.ForecastFragment" android:name="com.example.android.sunshine.app.ForecastFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:layout="@android:layout/list_content" /> tools:layout="@android:layout/list_content" />
</FrameLayout>