-
Notifications
You must be signed in to change notification settings - Fork 554
5.x | Utils
Davide Steduto edited this page May 1, 2018
·
18 revisions
Inside the main library, has static methods to identify RecyclerView orientation, span count and visible items positions.
Direct link LayoutUtils.java
eu.davidea.flexibleadapter.utils
|_ LayoutUtils
This class has static methods to handle Accent color and Spannable text as well as latest API versions.
Direct link FlexibleUtils.java
eu.davidea.flexibleadapter.utils
|_ FlexibleUtils
/*----------------------------*/
/* TEXT COLOR UTILITY METHODS */
/*----------------------------*/
/**
* Sets a spannable text with the accent color (if available) into the provided TextView.
* Multiple matches will be highlighted, but if the 2nd match is consecutive,
* the highlight is skipped.
* Internally calls fetchAccentColor(Context, int).
*/
public static void highlightText(@NonNull TextView textView,
@Nullable String originalText,
@Nullable String constraint);
/**
* Same as before but with custom color.
*/
public static void highlightText(@NonNull TextView textView,
@Nullable String originalText,
@Nullable String constraint, @ColorInt int color);
/*------------------------------*/
/* ACCENT COLOR UTILITY METHODS */
/*------------------------------*/
/**
* Reset the internal accent color to #INVALID_COLOR, to give the possibility
* to re-fetch it at runtime, since once it is fetched it cannot be changed.
*/
public static void resetAccentColor();
/**
* Optimized method to fetch the accent color on devices with at least Lollipop.
* If accent color has been already fetched it is simply returned.
*/
public static int fetchAccentColor(Context context, @ColorInt int defColor);
/*-------------------------------*/
/* RECYCLER-VIEW UTILITY METHODS */
/*-------------------------------*/
/**
* Finds the layout orientation of the RecyclerView,
* no matter which LayoutManager is in use.
*/
public static int getOrientation(RecyclerView recyclerView);
/**
* Helper method to retrieve the number of the columns (span count)
* of the given LayoutManager. All Layouts are supported.
*/
public static int getSpanCount(RecyclerView recyclerView);
/**
* Helper methods to find visible item positions.
* All Layouts are supported.
*/
public static int findFirstCompletelyVisibleItemPosition(RecyclerView recyclerView);
public static int findLastCompletelyVisibleItemPosition(RecyclerView recyclerView);
public static int findFirstVisibleItemPosition(RecyclerView recyclerView);
public static int findLastVisibleItemPosition(RecyclerView recyclerView);
To use as following in the bindViewHolder()
:
@Override
public void bindViewHolder(final FlexibleAdapter adapter, ParentViewHolder holder,
int position, List payloads) {
...
// In case of searchText matches with Title or with subTitle fields
// this will be highlighted
if (adapter.hasSearchText()) {
FlexibleUtils.highlightText(
holder.mTitle, getTitle(), getSearchText());
FlexibleUtils.highlightText(
holder.mSubtitle, getSubtitle(), getSearchText());
} else {
holder.mTitle.setText(getTitle());
holder.mSubtitle.setText(getSubtitle());
}
...
}
This class has static methods to handle the Background color with ripple at runtime.
Direct link DrawableUtils.java
eu.davidea.flexibleadapter.utils
|_ DrawableUtils
/**
* Helper methods to set the background depending on the android version.
*/
public static void setBackgroundCompat(View view, Drawable drawable);
public static void setBackgroundCompat(View view, @DrawableRes int drawableRes);
/**
* Helper method to extract the drawable by its resource depending
* on the android version.
*/
public static Drawable getDrawableCompat(Context context, @DrawableRes int drawableRes);
/**
* Helper to get the default selectableItemBackground drawable of the
* R.attr.selectableItemBackground attribute of the overridden style.
*/
public static Drawable getSelectableItemBackground(Context context);
/**
* Helper to get the system default Color Control Highlight. Returns the color
* of the R.attr.colorControlHighlight attribute in the overridden style.
*/
@ColorInt
public static int getColorControlHighlight(Context context);
/**
* Helper to get a custom selectable background with Ripple if device has at
* least Lollipop.
*/
public static Drawable getSelectableBackgroundCompat(@ColorInt int normalColor,
@ColorInt int pressedColor,
@ColorInt int rippleColor);
/**
* Adds a ripple effect to any background.
*/
public static Drawable getRippleDrawable(Drawable drawable, @ColorInt int rippleColor);
/**
* Generate the ColorDrawable object from the provided Color.
*/
public static ColorDrawable getColorDrawable(@ColorInt int color);
With only 1 statement, we avoid the configuration of the XML 👍
To use as following in the bindViewHolder()
:
@Override
public void bindViewHolder(final FlexibleAdapter adapter, final ViewHolder holder,
int position, List payloads) {
Context context = holder.itemView.getContext();
...
Drawable drawable = DrawableUtils.getSelectableBackgroundCompat(
Color.WHITE, Color.parseColor("#dddddd"), // Same color of divider
DrawableUtils.getColorControlHighlight(context));
DrawableUtils.setBackgroundCompat(holder.itemView, drawable);
// OR
Drawable drawable = DrawableUtils.getSelectableBackgroundCompat(
status.getColor(), // normal background
Utils.getColorAccent(context), // pressed background
Color.WHITE)); // ripple color
DrawableUtils.setBackgroundCompat(holder.itemView, drawable);
}
- Update Data Set
- Selection modes
- Headers and Sections
- Scrollable Headers and Footers
- Expandable items
- Drag&Drop and Swipe
- EndlessScroll / On Load More
- Search Filter
- FastScroller
- Adapter Animations
- Third party Layout Managers
- Payload
- Smooth Layout Managers
- Flexible Item Decoration
- Utils
- ActionModeHelper
- AnimatorHelper
- EmptyViewHelper
- UndoHelper
* = Under revision!