Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.roughike.swipeselector;

import android.graphics.drawable.Drawable;
import android.support.annotation.StringRes;

/*
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView
android:id="@+id/swipeselector_content_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
tools:ignore="ContentDescription"/>

</LinearLayout>