diff --git a/swipe-selector/src/main/java/com/roughike/swipeselector/SwipeAdapter.java b/swipe-selector/src/main/java/com/roughike/swipeselector/SwipeAdapter.java index f7a19a8..455f280 100644 --- a/swipe-selector/src/main/java/com/roughike/swipeselector/SwipeAdapter.java +++ b/swipe-selector/src/main/java/com/roughike/swipeselector/SwipeAdapter.java @@ -1,7 +1,6 @@ package com.roughike.swipeselector; import android.animation.ObjectAnimator; -import android.animation.ValueAnimator; import android.content.Context; import android.graphics.Typeface; import android.graphics.drawable.ShapeDrawable; @@ -254,7 +253,16 @@ protected void setItems(SwipeItem... items) { for (SwipeItem item : items) { if (item.titleRes != -1) { - item.title = mContext.getString(item.titleRes); + if(!item.isTitleImage) { + item.title = mContext.getString(item.titleRes); + } else { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + item.image = mContext.getDrawable(item.titleRes); + } else { + //noinspection deprecation + item.image = mContext.getResources().getDrawable(item.titleRes); + } + } } if (item.descriptionRes != -1) { @@ -297,33 +305,46 @@ protected void onRestoreInstanceState(Bundle state) { */ @Override public Object instantiateItem(ViewGroup container, int position) { - LinearLayout layout = (LinearLayout) View.inflate(mContext, R.layout.swipeselector_content_item, null); - TextView title = (TextView) layout.findViewById(R.id.swipeselector_content_title); - TextView description = (TextView) layout.findViewById(R.id.swipeselector_content_description); - SwipeItem slideItem = mItems.get(position); - title.setText(slideItem.title); - description.setText(slideItem.description); - - // We shouldn't get here if the typeface didn't exist. - // But just in case, because we're paranoid. - if (mCustomTypeFace != null) { - title.setTypeface(mCustomTypeFace); - description.setTypeface(mCustomTypeFace); - } + LinearLayout layout; - if (mTitleTextAppearance != -1) { - setTextAppearanceCompat(title, mTitleTextAppearance); - } + if(!slideItem.isTitleImage) { + layout = (LinearLayout) View.inflate(mContext, R.layout.swipeselector_content_item_text, null); - if (mDescriptionTextAppearance != -1) { - setTextAppearanceCompat(description, mDescriptionTextAppearance); - } + TextView title = (TextView) layout.findViewById(R.id.swipeselector_content_title); + TextView description = (TextView) layout.findViewById(R.id.swipeselector_content_description); + + title.setText(slideItem.title); + description.setText(slideItem.description); + + // We shouldn't get here if the typeface didn't exist. + // But just in case, because we're paranoid. + if (mCustomTypeFace != null) { + title.setTypeface(mCustomTypeFace); + description.setTypeface(mCustomTypeFace); + } + + if (mTitleTextAppearance != -1) { + setTextAppearanceCompat(title, mTitleTextAppearance); + } + + if (mDescriptionTextAppearance != -1) { + setTextAppearanceCompat(description, mDescriptionTextAppearance); + } + + if (mDescriptionGravity != -1) { + description.setGravity(mDescriptionGravity); + } + } else { + layout = (LinearLayout) View.inflate(mContext, R.layout.swipeselector_content_item_image, null); - if (mDescriptionGravity != -1) { - description.setGravity(mDescriptionGravity); + ImageView title = (ImageView) layout.findViewById(R.id.swipeselector_content_image); + + title.setImageDrawable(slideItem.image); + title.setContentDescription(slideItem.description); } + layout.setPadding(mContentLeftPadding, mSweetSixteen, mContentRightPadding, diff --git a/swipe-selector/src/main/java/com/roughike/swipeselector/SwipeItem.java b/swipe-selector/src/main/java/com/roughike/swipeselector/SwipeItem.java index f9f07a6..c44964e 100644 --- a/swipe-selector/src/main/java/com/roughike/swipeselector/SwipeItem.java +++ b/swipe-selector/src/main/java/com/roughike/swipeselector/SwipeItem.java @@ -1,5 +1,6 @@ package com.roughike.swipeselector; +import android.graphics.drawable.Drawable; import android.support.annotation.StringRes; /* @@ -32,8 +33,10 @@ public class SwipeItem { public Object value; public String title; + public Drawable image; public String description; + protected boolean isTitleImage; protected int titleRes = -1; protected int descriptionRes = -1; @@ -76,4 +79,42 @@ public SwipeItem(Object value, @StringRes int title, @StringRes int description) this.titleRes = title; this.descriptionRes = description; } + + /** + * Constructor for creating a new item for the {@link SwipeSelector} + * using String resources. + * + * @param value The value for this item, which should generally be unique + * for current {@link SwipeSelector}. This is used the same + * way one might use radio buttons on webpages with HTML. + * @param title A short descriptive title for this item, such as "Pizza" or an image. + * @param description Longer explanation related to the title or, if title is an image, the [Accessibility] description for image. + * @param isImage if the title is an image resource. + **/ + public SwipeItem(Object value, @StringRes int title, int description, boolean isImage) { + //TODO throw exception if title isn't image and should + checkForStringResources = true; + isTitleImage = isImage; + + this.value = value; + this.titleRes = title; + this.descriptionRes = description; + } + + /** + * Constructor for creating a new item for the {@link SwipeSelector}. + * + * @param value The value for this item, which should generally be unique + * for current {@link SwipeSelector}. This is used the same + * way one might use radio buttons on webpages with HTML. + * @param image A small title for this item. + * @param contentDescription [Accessibility] Description for image + */ + public SwipeItem(Object value, Drawable image, String contentDescription) { + isTitleImage = true; + + this.value = value; + this.image = image; + this.description = contentDescription; + } } diff --git a/swipe-selector/src/main/res/layout/swipeselector_content_item_image.xml b/swipe-selector/src/main/res/layout/swipeselector_content_item_image.xml new file mode 100644 index 0000000..7bff6a7 --- /dev/null +++ b/swipe-selector/src/main/res/layout/swipeselector_content_item_image.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/swipe-selector/src/main/res/layout/swipeselector_content_item.xml b/swipe-selector/src/main/res/layout/swipeselector_content_item_text.xml similarity index 100% rename from swipe-selector/src/main/res/layout/swipeselector_content_item.xml rename to swipe-selector/src/main/res/layout/swipeselector_content_item_text.xml