Skip to content

Commit

Permalink
Resolves #200 - Moved getSpanCount() from SelectableAdapter to Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
davideas committed Nov 30, 2016
1 parent 135d6e0 commit 730c7f5
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import android.os.Bundle;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;

Expand Down Expand Up @@ -150,24 +148,6 @@ public RecyclerView getRecyclerView() {
return mRecyclerView;
}

/**
* Helper method to return the number of the columns (span count) of the given LayoutManager.
* <p>All Layouts are supported.</p>
*
* @param layoutManager the layout manager to check
* @return the span count
* @since 5.0.0-b7
*/
//TODO: Deprecated? move to Utils
public static int getSpanCount(RecyclerView.LayoutManager layoutManager) {
if (layoutManager instanceof GridLayoutManager) {
return ((GridLayoutManager) layoutManager).getSpanCount();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
return ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
}
return 1;
}

/**
* Sets the mode of the selection:
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.os.Build.VERSION_CODES;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
Expand Down Expand Up @@ -66,6 +67,29 @@ public static boolean hasJellyBean() {
return Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN;
}

/**
* @return the string representation of the provided {@link SelectableAdapter.Mode}
*/
@SuppressLint("SwitchIntDef")
public static String getModeName(@SelectableAdapter.Mode int mode) {
switch (mode) {
case SelectableAdapter.MODE_SINGLE:
return "MODE_SINGLE";
case SelectableAdapter.MODE_MULTI:
return "MODE_MULTI";
default:
return "MODE_IDLE";
}
}

/**
* @return the SimpleClassName of the provided object
*/
public static String getClassName(@NonNull Object o) {
return o.getClass().getSimpleName();
}


/**
* Sets a spannable text with the accent color (if available) into the provided TextView.
* <p>Internally calls {@link #fetchAccentColor(Context, int)}.</p>
Expand Down Expand Up @@ -95,6 +119,23 @@ public static void highlightText(@NonNull Context context, @NonNull TextView tex
}
}

/**
* Resolves bug #161. Necessary when {@code theme} attribute is used in the layout.
* Used by {@code FlexibleAdapter.getStickySectionHeadersHolder()} method.
*/
public static Activity scanForActivity(Context context) {
if (context instanceof Activity)
return (Activity) context;
else if (context instanceof ContextWrapper)
return scanForActivity(((ContextWrapper) context).getBaseContext());

return null;
}

/*------------------------------*/
/* ACCENT COLOR UTILITY METHODS */
/*------------------------------*/

/**
* Reset the internal accent color to {@link #INVALID_COLOR}, to give the possibility
* to re-fetch it at runtime, since once it is fetched it cannot be changed.
Expand All @@ -121,6 +162,10 @@ public static int fetchAccentColor(Context context, @ColorInt int defColor) {
return colorAccent;
}

/*-------------------------------*/
/* RECYCLER-VIEW UTILITY METHODS */
/*-------------------------------*/

/**
* Finds the layout orientation of the RecyclerView.
*
Expand Down Expand Up @@ -149,13 +194,30 @@ public static int getOrientation(RecyclerView.LayoutManager layoutManager) {
}

/**
* Helper method to find the adapter position of the First completely visible view [for each
* span], no matter which Layout is.
* Helper method to retrieve the number of the columns (span count) of the given LayoutManager.
* <p>All Layouts are supported.</p>
*
* @param layoutManager the layout manager to check
* @return the span count
* @since 5.0.0-b7
*/
public static int getSpanCount(RecyclerView.LayoutManager layoutManager) {
if (layoutManager instanceof GridLayoutManager) {
return ((GridLayoutManager) layoutManager).getSpanCount();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
return ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
}
return 1;
}

/**
* Helper method to find the adapter position of the <b>first completely</b> visible view
* [for each span], no matter which Layout is.
*
* @param layoutManager the layout manager in use
* @return the adapter position of the first fully visible item or {@code RecyclerView.NO_POSITION}
* @return the adapter position of the <b>first fully</b> visible item or {@code RecyclerView.NO_POSITION}
* if there aren't any visible items.
* @see #findLastCompletelyVisibleItemPosition(RecyclerView.LayoutManager)
* @see #findFirstVisibleItemPosition(RecyclerView.LayoutManager)
* @since 5.0.0-b8
*/
public static int findFirstCompletelyVisibleItemPosition(RecyclerView.LayoutManager layoutManager) {
Expand All @@ -167,56 +229,57 @@ public static int findFirstCompletelyVisibleItemPosition(RecyclerView.LayoutMana
}

/**
* Helper method to find the adapter position of the Last completely visible view [for each
* span], no matter which Layout is.
* Helper method to find the adapter position of the <b>first partially</b> visible view
* [for each span], no matter which Layout is.
*
* @param layoutManager the layout manager in use
* @return the adapter position of the last fully visible item or {@code RecyclerView.NO_POSITION}
* @return the adapter position of the <b>first partially</b> visible item or {@code RecyclerView.NO_POSITION}
* if there aren't any visible items.
* @see #findFirstCompletelyVisibleItemPosition(RecyclerView.LayoutManager)
* @since 5.0.0-b8
* @since 5.0.0-rc1
*/
public static int findLastCompletelyVisibleItemPosition(RecyclerView.LayoutManager layoutManager) {
public static int findFirstVisibleItemPosition(RecyclerView.LayoutManager layoutManager) {
if (layoutManager instanceof StaggeredGridLayoutManager) {
return ((StaggeredGridLayoutManager) layoutManager).findLastCompletelyVisibleItemPositions(null)[0];
return ((StaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(null)[0];
} else {
return ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
return ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
}
}

/**
* Resolves bug #161. Necessary when {@code theme} attribute is used in the layout.
* Used by {@code FlexibleAdapter.getStickySectionHeadersHolder()} method.
*/
public static Activity scanForActivity(Context context) {
if (context instanceof Activity)
return (Activity) context;
else if (context instanceof ContextWrapper)
return scanForActivity(((ContextWrapper) context).getBaseContext());

return null;
}

/**
* @return the string representation of the provided {@link SelectableAdapter.Mode}
* Helper method to find the adapter position of the <b>last completely</b> visible view
* [for each span], no matter which Layout is.
*
* @param layoutManager the layout manager in use
* @return the adapter position of the <b>last fully</b> visible item or {@code RecyclerView.NO_POSITION}
* if there aren't any visible items.
* @see #findLastVisibleItemPosition(RecyclerView.LayoutManager)
* @since 5.0.0-b8
*/
@SuppressLint("SwitchIntDef")
public static String getModeName(@SelectableAdapter.Mode int mode) {
switch (mode) {
case SelectableAdapter.MODE_SINGLE:
return "MODE_SINGLE";
case SelectableAdapter.MODE_MULTI:
return "MODE_MULTI";
default:
return "MODE_IDLE";
public static int findLastCompletelyVisibleItemPosition(RecyclerView.LayoutManager layoutManager) {
if (layoutManager instanceof StaggeredGridLayoutManager) {
return ((StaggeredGridLayoutManager) layoutManager).findLastCompletelyVisibleItemPositions(null)[0];
} else {
return ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
}
}

/**
* @return the SimpleClassName of the provided object
* Helper method to find the adapter position of the <b>last partially</b> visible view
* [for each span], no matter which Layout is.
*
* @param layoutManager the layout manager in use
* @return the adapter position of the <b>last partially</b> visible item or {@code RecyclerView.NO_POSITION}
* if there aren't any visible items.
* @see #findLastCompletelyVisibleItemPosition(RecyclerView.LayoutManager)
* @since 5.0.0-rc1
*/
public static String getClassName(@NonNull Object o) {
return o.getClass().getSimpleName();
public static int findLastVisibleItemPosition(RecyclerView.LayoutManager layoutManager) {
if (layoutManager instanceof StaggeredGridLayoutManager) {
return ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(null)[0];
} else {
return ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
}
}

}

0 comments on commit 730c7f5

Please sign in to comment.