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.
*/
public static class ViewHolder {
public final ImageView iconView;
public final TextView dateView;
public final TextView descriptionView;
public final TextView highTempView;
public final TextView lowTempView;
public final ImageView mIconView;
public final TextView mDateView;
public final TextView mDescriptionView;
public final TextView mHighTempView;
public final TextView mLowTempView;
public ViewHolder(View view) {
iconView = (ImageView) view.findViewById(R.id.list_item_icon);
dateView = (TextView) view.findViewById(R.id.list_item_date_textview);
descriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview);
highTempView = (TextView) view.findViewById(R.id.list_item_high_textview);
lowTempView = (TextView) view.findViewById(R.id.list_item_low_textview);
mIconView = (ImageView) view.findViewById(R.id.list_item_icon);
mDateView = (TextView) view.findViewById(R.id.list_item_date_textview);
mDescriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview);
mHighTempView = (TextView) view.findViewById(R.id.list_item_high_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();
int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID);
int fallbackIconId;
int viewType = getItemViewType(cursor.getPosition());
switch (viewType) {
case VIEW_TYPE_TODAY: {
// Get weather icon
fallbackIconId = Utility.getArtResourceForWeatherCondition(
weatherId);
int defaultImage;
switch (getItemViewType(cursor.getPosition())) {
case VIEW_TYPE_TODAY:
defaultImage = Utility.getArtResourceForWeatherCondition(weatherId);
break;
}
default: {
// Get weather icon
fallbackIconId = Utility.getIconResourceForWeatherCondition(
weatherId);
break;
}
default:
defaultImage = Utility.getIconResourceForWeatherCondition(weatherId);
}
Glide.with(mContext)
.load(Utility.getArtUrlForWeatherCondition(mContext, weatherId))
.error(fallbackIconId)
.crossFade()
.into(viewHolder.iconView);
if (Utility.usingLocalGraphics(mContext)) {
viewHolder.mIconView.
setImageResource(defaultImage);
} else {
Glide.with(mContext)
.load(Utility.getArtUrlForWeatherCondition(mContext, weatherId))
.error(defaultImage)
.crossFade()
.into(viewHolder.mIconView);
}
// Read date from cursor
long dateInMillis = cursor.getLong(ForecastFragment.COL_WEATHER_DATE);
// 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
String description = Utility.getStringForWeatherCondition(context, weatherId);
// Read weather forecast from cursor
String description = Utility.getStringForWeatherCondition(mContext, weatherId);
// Find TextView and set weather forecast on it
viewHolder.descriptionView.setText(description);
viewHolder.descriptionView.setContentDescription(context.getString(R.string.a11y_forecast, description));
viewHolder.mDescriptionView.setText(description);
viewHolder.mDescriptionView.setContentDescription(mContext.getString(R.string.a11y_forecast, description));
// 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
// is not individually selectable
// Read high temperature from cursor
String high = Utility.formatTemperature(
context, cursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP));
viewHolder.highTempView.setText(high);
viewHolder.highTempView.setContentDescription(context.getString(R.string.a11y_high_temp, high));
double high = mCursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP);
String highString = Utility.formatTemperature(mContext, high);
viewHolder.mHighTempView.setText(highString);
viewHolder.mHighTempView.setContentDescription(mContext.getString(R.string.a11y_high_temp, highString));
// Read low temperature from cursor
String low = Utility.formatTemperature(
context, cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP));
viewHolder.lowTempView.setText(low);
viewHolder.lowTempView.setContentDescription(context.getString(R.string.a11y_low_temp, low));
double low = mCursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP);
String lowString = Utility.formatTemperature(mContext, low);
viewHolder.mLowTempView.setText(lowString);
viewHolder.mLowTempView.setContentDescription(mContext.getString(R.string.a11y_low_temp, lowString));
}
public void setUseTodayLayout(boolean useTodayLayout) {

View File

@@ -13,31 +13,10 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<FrameLayout
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"
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_forecast"
android:name="com.example.android.sunshine.app.ForecastFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@android:layout/list_content" />
</FrameLayout>