Skip to content

Commit

Permalink
Resolved #382 - Adapted the demoApp to the new FlexibleItemDecoration…
Browse files Browse the repository at this point in the history
… and fixed some bugs around.
  • Loading branch information
davideas committed Jun 5, 2017
1 parent 3bf0f56 commit e7aa948
Show file tree
Hide file tree
Showing 34 changed files with 151 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,9 @@ public boolean shouldMoveItem(int fromPosition, int toPosition) {

@Override
public void onItemMove(int fromPosition, int toPosition) {
// When offset between item decorations have been set, we need to invalidate them
mAdapter.invalidateItemDecorations(100L);

//TODO FOR YOU: this doesn't work with all types of items (of course)..... we need to implement some custom logic. Consider to use also onActionStateChanged() when dragging is completed
/*
String prev = mItems.remove(from);
Expand Down Expand Up @@ -987,6 +990,8 @@ public void onPostAction() {
mAdapter.notifyItemChanged(mainPosition, "blink");
// New title for context
mActionModeHelper.updateContextTitle(mAdapter.getSelectedItemCount());
// Item decorations must be invalidated and rebuilt
mAdapter.invalidateItemDecorations(100L);
}
// We consume always the event, never finish the ActionMode
return true;
Expand All @@ -1010,6 +1015,8 @@ public void onPostAction() {
// New title for context
mActionModeHelper.updateContextTitle(mAdapter.getSelectedItemCount());
}
// Item decorations must be invalidated and rebuilt
mAdapter.invalidateItemDecorations(100L);
}
// We consume always the event, never finish the ActionMode
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import eu.davidea.samples.flexibleadapter.services.DatabaseConfiguration;
import eu.davidea.samples.flexibleadapter.services.DatabaseService;
import eu.davidea.samples.flexibleadapter.services.DatabaseType;
import eu.davidea.utils.Utils;

/**
* A fragment representing a list of Items.
Expand Down Expand Up @@ -103,7 +104,7 @@ private void initializeRecyclerView() {
mAdapter.setAnimateChangesWithDiffUtil(DatabaseConfiguration.animateWithDiffUtil)
.setAnimateToLimit(DatabaseConfiguration.animateToLimit)//Size limit = MAX_VALUE will always animate the changes
.setNotifyMoveOfFilteredItems(DatabaseConfiguration.notifyMove)//When true, filtering on big list is very slow!
.setNotifyChangeOfUnfilteredItems(DatabaseConfiguration.notifyChange)//We have highlighted text while filtering, so let's enable this feature to be consistent with the active filter
.setNotifyChangeOfUnfilteredItems(DatabaseConfiguration.notifyChange)
.setAnimationInitialDelay(100L)
.setAnimationOnScrolling(true)
.setAnimationOnReverseScrolling(true)
Expand All @@ -117,7 +118,9 @@ private void initializeRecyclerView() {
mRecyclerView.setAdapter(mAdapter);
//Custom divider item decorator with Offset
if (mDivider == null) {
mDivider = new FlexibleItemDecoration(getActivity(), R.drawable.divider_large).withOffset(true);
mDivider = new FlexibleItemDecoration(getActivity(), R.drawable.divider_large)
.withEdge(true)
.withOffset(Utils.dpToPx(getActivity(), 8f));
}

//Add FastScroll to the RecyclerView, after the Adapter has been attached the RecyclerView!!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void noMoreLoad(int newItemsSize) {
* @param lastPosition the position of the last main item in the adapter
* @param currentPage the current page
* @since 5.0.0-b6
* <br/>5.0.0-rc1 added {@code lastPosition} and {@code currentPage} as parameters
* <br>5.0.0-rc1 added {@code lastPosition} and {@code currentPage} as parameters
*/
@Override
public void onLoadMore(int lastPosition, int currentPage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand All @@ -14,6 +13,7 @@
import eu.davidea.flexibleadapter.SelectableAdapter;
import eu.davidea.flexibleadapter.common.FlexibleItemDecoration;
import eu.davidea.flexibleadapter.common.SmoothScrollGridLayoutManager;
import eu.davidea.flexibleadapter.utils.Log;
import eu.davidea.flipview.FlipView;
import eu.davidea.samples.flexibleadapter.ExampleAdapter;
import eu.davidea.samples.flexibleadapter.MainActivity;
Expand All @@ -28,8 +28,6 @@
*/
public class FragmentExpandableSections extends AbstractFragment {

public static final String TAG = FragmentExpandableSections.class.getSimpleName();

private ExampleAdapter mAdapter;

public static FragmentExpandableSections newInstance(int columnCount) {
Expand Down Expand Up @@ -68,6 +66,8 @@ private void initializeRecyclerView(Bundle savedInstanceState) {
// Initialize Adapter and RecyclerView
// ExampleAdapter makes use of stableIds, I strongly suggest to implement 'item.hashCode()'
mAdapter = new ExampleAdapter(DatabaseService.getInstance().getDatabaseList(), getActivity());
// OnItemAdd and OnItemRemove listeners
mAdapter.addListener(this);
// Experimenting NEW features (v5.0.0)
mAdapter.expandItemsAtStartUp()
.setAutoCollapseOnExpand(false)
Expand Down Expand Up @@ -135,7 +135,7 @@ public int getSpanSize(int position) {
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
Log.v(TAG, "onCreateOptionsMenu called!");
Log.v("onCreateOptionsMenu called!");
inflater.inflate(R.menu.menu_sections, menu);
mListener.initSearchView(menu);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import eu.davidea.fastscroller.FastScroller;
import eu.davidea.flexibleadapter.SelectableAdapter;
import eu.davidea.flexibleadapter.common.FlexibleItemDecoration;
import eu.davidea.flexibleadapter.common.SmoothScrollGridLayoutManager;
import eu.davidea.flexibleadapter.items.IHeader;
import eu.davidea.flexibleadapter.items.ISectionable;
Expand All @@ -23,7 +24,6 @@
import eu.davidea.samples.flexibleadapter.R;
import eu.davidea.samples.flexibleadapter.dialogs.OnParameterSelectedListener;
import eu.davidea.samples.flexibleadapter.items.ExpandableHeaderItem;
import eu.davidea.samples.flexibleadapter.items.HeaderItem;
import eu.davidea.samples.flexibleadapter.items.ScrollableUseCaseItem;
import eu.davidea.samples.flexibleadapter.services.DatabaseConfiguration;
import eu.davidea.samples.flexibleadapter.services.DatabaseService;
Expand Down Expand Up @@ -93,6 +93,10 @@ private void initializeRecyclerView(Bundle savedInstanceState) {
// NOTE: Use default item animator 'canReuseUpdatedViewHolder()' will return true if
// a Payload is provided. FlexibleAdapter is actually sending Payloads onItemChange.
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.addItemDecoration(new FlexibleItemDecoration(getActivity())
.addItemViewType(R.layout.recycler_simple_item, 8, 0)
.withSectionGapOffset(24)
.withBottomEdge(true));

// Add FastScroll to the RecyclerView, after the Adapter has been attached the RecyclerView!!!
FastScroller fastScroller = (FastScroller) getView().findViewById(R.id.fast_scroller);
Expand Down Expand Up @@ -126,12 +130,13 @@ private void initializeRecyclerView(Bundle savedInstanceState) {
int count = 1;
@Override
public void performFabAction() {
int position = mAdapter.getStickyPosition();
if (position > 0) {
HeaderItem header = (HeaderItem) mAdapter.getItem(position);
header.setTitle("New sticky title " + count++);
mAdapter.updateItem(header);
}
mAdapter.clear();
// int position = mAdapter.getStickyPosition();
// if (position > 0) {
// HeaderItem header = (HeaderItem) mAdapter.getItem(position);
// header.setTitle("New sticky title " + count++);
// mAdapter.updateItem(header);
// }
// BottomSheetDialog bottomSheetDialogFragment = BottomSheetDialog.newInstance(R.layout.bottom_sheet_headers_sections, this);
// bottomSheetDialogFragment.show(getActivity().getSupportFragmentManager(), BottomSheetDialog.TAG);
}
Expand Down Expand Up @@ -199,7 +204,6 @@ protected GridLayoutManager createNewGridLayoutManager() {
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
//noinspection ConstantConditions
return mAdapter.getItem(position).getSpanSize(mColumnCount, position);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void noMoreLoad(int newItemsSize) {
* @param lastPosition the position of the last main item in the adapter
* @param currentPage the current page
* @since 5.0.0-b6
* <br/>5.0.0-rc1 added {@code lastPosition} and {@code currentPage} as parameters
* <br>5.0.0-rc1 added {@code lastPosition} and {@code currentPage} as parameters
*/
@Override
public void onLoadMore(int lastPosition, int currentPage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.view.animation.DecelerateInterpolator;

import eu.davidea.flexibleadapter.SelectableAdapter;
import eu.davidea.flexibleadapter.common.FlexibleItemDecoration;
import eu.davidea.flexibleadapter.common.SmoothScrollGridLayoutManager;
import eu.davidea.samples.flexibleadapter.OverallAdapter;
import eu.davidea.samples.flexibleadapter.R;
Expand All @@ -32,6 +33,7 @@ public class FragmentOverall extends AbstractFragment {
* Custom implementation of FlexibleAdapter
*/
private OverallAdapter mAdapter;
private ScrollableUseCaseItem scrollableUseCaseItem;


public static FragmentOverall newInstance(int columnCount) {
Expand Down Expand Up @@ -77,6 +79,9 @@ private void initializeRecyclerView(Bundle savedInstanceState) {
mRecyclerView.setLayoutManager(createNewStaggeredGridLayoutManager());
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setHasFixedSize(true); //Size of RV will not change
mRecyclerView.addItemDecoration(new FlexibleItemDecoration(getActivity())
.addItemViewType(R.layout.recycler_overall_item, 8, 8)
.withEdge(true));

// After Adapter is attached to RecyclerView
mAdapter.setLongPressDragEnabled(true);
Expand All @@ -94,9 +99,10 @@ public void run() {
mListener.onFragmentChange(swipeRefreshLayout, mRecyclerView, SelectableAdapter.MODE_IDLE);

// Add 1 Scrollable Header
mAdapter.addScrollableHeader(new ScrollableUseCaseItem(
scrollableUseCaseItem = new ScrollableUseCaseItem(
getString(R.string.overall_use_case_title),
getString(R.string.overall_use_case_description)));
getString(R.string.overall_use_case_description));
mAdapter.addScrollableHeader(scrollableUseCaseItem);
}

@Override
Expand All @@ -118,15 +124,7 @@ protected GridLayoutManager createNewGridLayoutManager() {
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
// NOTE: If you use simple integers to identify the ViewType,
// here, you should use them and not Layout integers
switch (mAdapter.getItemViewType(position)) {
case R.layout.recycler_scrollable_usecase_item:
case R.layout.recycler_scrollable_layout_item:
return mColumnCount;
default:
return 1;
}
return mAdapter.getItem(position).getSpanSize(mColumnCount, position);
}
});
return gridLayoutManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.Payload;
import eu.davidea.flexibleadapter.SelectableAdapter;
import eu.davidea.flexibleadapter.common.FlexibleItemDecoration;
import eu.davidea.flexibleadapter.common.TopSnappedSmoothScroller;
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
import eu.davidea.flexibleadapter.items.IHeader;
Expand Down Expand Up @@ -75,7 +76,7 @@ private void initializeRecyclerView(Bundle savedInstanceState) {
// Customize the speed of the smooth scroll.
// NOTE: Every time you change this value you MUST recreate the LayoutManager instance
// and to assign it again to the RecyclerView!
TopSnappedSmoothScroller.MILLISECONDS_PER_INCH = 33f;
TopSnappedSmoothScroller.MILLISECONDS_PER_INCH = 33f; //Make faster the smooth scroll
mRecyclerView.setLayoutManager(createNewStaggeredGridLayoutManager());
// This value is restored to 100f (default) right here, because it is used in the
// constructor by Android. If we don't change it now, others LayoutManager will be
Expand All @@ -87,6 +88,9 @@ private void initializeRecyclerView(Bundle savedInstanceState) {
// NOTE: Use default item animator 'canReuseUpdatedViewHolder()' will return true if
// a Payload is provided. FlexibleAdapter is actually sending Payloads onItemChange.
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.addItemDecoration(new FlexibleItemDecoration(getActivity())
.addItemViewType(R.layout.recycler_staggered_item, 8, 8)
.withEdge(true));

// Experimenting NEW features (v5.0.0)
mAdapter.setDisplayHeadersAtStartUp(true) //Show Headers at startUp!
Expand Down Expand Up @@ -137,7 +141,7 @@ public boolean onOptionsItemSelected(MenuItem item) {

@Override
public void performFabAction() {
//Simulate changing status
// Simulate changing status
StaggeredItemStatus status = StaggeredItemStatus.values()[new Random().nextInt(StaggeredItemStatus.values().length - 1)];
StaggeredHeaderItem headerItem = DatabaseService.getInstance().getHeaderByStatus(status);
int scrollTo;
Expand Down Expand Up @@ -208,10 +212,11 @@ private int moveItem(StaggeredItemStatus status, StaggeredHeaderItem headerItem)
int toPosition = mAdapter.calculatePositionFor(staggeredItem, new DatabaseService.ItemComparatorByGroup());
// Move item to just calculated position under the correct section
mAdapter.moveItem(mAdapter.getGlobalPositionOf(staggeredItem), toPosition, Payload.MOVE);
DatabaseService.getInstance().sort(new DatabaseService.ItemComparatorById());
}
// Retrieve the final position due to a possible hidden header became now visible!
int scrollTo = mAdapter.getGlobalPositionOf(staggeredItem);
Log.d(TAG, "Moving Item to position" + scrollTo);
Log.d(TAG, "Moving Item " + staggeredItem + " to position" + scrollTo);
return scrollTo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.animation.Animator;
import android.support.annotation.NonNull;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
Expand All @@ -18,9 +17,9 @@
import eu.davidea.viewholders.ExpandableViewHolder;

/**
* This is an experiment to evaluate how a Section with header can also be expanded/collapsed.
* <p>Here, it still benefits of the common fields declared in AbstractModelItem.</p>
* It's important to note that, the ViewHolder must be specified in all &lt;diamond&gt; signature.
* This Section with header can also be expanded/collapsed.
* <p>It's important to note that, the ViewHolder must be specified in all &lt;diamond&gt;
* signature.</p>
*/
public class AnimatorExpandableItem
extends AbstractExpandableHeaderItem<AnimatorExpandableViewHolder, AnimatorSubItem> {
Expand Down Expand Up @@ -111,24 +110,30 @@ public AnimatorExpandableViewHolder(View view, FlexibleAdapter adapter) {
mTitle = (TextView) view.findViewById(R.id.title);
mSubtitle = (TextView) view.findViewById(R.id.subtitle);

//Support for StaggeredGridLayoutManager
if (itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams) {
((StaggeredGridLayoutManager.LayoutParams) itemView.getLayoutParams()).setFullSpan(true);
}
// Support for StaggeredGridLayoutManager
setFullSpan(true);
}

@Override
protected boolean shouldNotifyParentOnClick() {
// Let's notify the item has been expanded / collapsed
return true;//default=false
}

@Override
protected void expandView(int position) {
super.expandView(position);
//Let's notify the item has been expanded
if (mAdapter.isExpanded(position)) mAdapter.notifyItemChanged(position, true);
// Let's notify the item has been expanded. Note: from 5.0.0-rc1 the next line becomes
// obsolete, override the new method shouldNotifyParentOnClick() as showcased here
//if (mAdapter.isExpanded(position)) mAdapter.notifyItemChanged(position, true);
}

@Override
protected void collapseView(int position) {
super.collapseView(position);
//Let's notify the item has been collapsed
if (!mAdapter.isExpanded(position)) mAdapter.notifyItemChanged(position, true);
// Let's notify the item has been expanded. Note: from 5.0.0-rc1 the next line becomes
// obsolete, override the new method shouldNotifyParentOnClick() as showcased here
//if (!mAdapter.isExpanded(position)) mAdapter.notifyItemChanged(position, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/**
* If you don't have many fields in common better to extend directly from
* {@link eu.davidea.flexibleadapter.items.AbstractFlexibleItem} to benefit of the already
* {@link eu.davidea.flexibleadapter.items.AbstractSectionableItem} to benefit of the already
* implemented methods (getter and setters).
*/
public class AnimatorSubItem extends AbstractSectionableItem<AnimatorSubItem.ChildViewHolder, AnimatorExpandableItem> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package eu.davidea.samples.flexibleadapter.items;

import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
Expand All @@ -19,13 +18,12 @@

/**
* This is an experiment to evaluate how a Section with header can also be expanded/collapsed.
* <p>Here, it still benefits of the common fields declared in AbstractModelItem.</p>
* <p>Here, it still benefits of the common fields declared in AbstractItem.</p>
* It's important to note that, the ViewHolder must be specified in all &lt;diamond&gt; signature.
*/
public class ExpandableHeaderItem
extends AbstractItem<ExpandableHeaderViewHolder>
implements IExpandable<ExpandableHeaderViewHolder, SubItem>,
IHeader<ExpandableHeaderViewHolder> {
implements IExpandable<ExpandableHeaderViewHolder, SubItem>, IHeader<ExpandableHeaderViewHolder> {

/* Flags for FlexibleAdapter */
private boolean mExpanded = false;
Expand Down Expand Up @@ -144,10 +142,8 @@ static class ExpandableHeaderViewHolder extends ExpandableViewHolder {
this.mHandleView.setVisibility(View.GONE);
}

//Support for StaggeredGridLayoutManager
if (itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams) {
((StaggeredGridLayoutManager.LayoutParams) itemView.getLayoutParams()).setFullSpan(true);
}
// Support for StaggeredGridLayoutManager
setFullSpan(true);
}

/**
Expand Down
Loading

0 comments on commit e7aa948

Please sign in to comment.