use of android.support.v7.widget.RecyclerView.Adapter in project FastAdapter by mikepenz.
the class EndlessScrollListActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
findViewById(android.R.id.content).setSystemUiVisibility(findViewById(android.R.id.content).getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
// Handle Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//style our ui
new MaterializeBuilder().withActivity(this).build();
//create our FastAdapter which will manage everything
fastItemAdapter = new FastItemAdapter<>();
fastItemAdapter.withSelectable(true);
//create our FooterAdapter which will manage the progress items
footerAdapter = new FooterAdapter<>();
//configure our fastAdapter
fastItemAdapter.withOnClickListener(new FastAdapter.OnClickListener<SimpleItem>() {
@Override
public boolean onClick(View v, IAdapter<SimpleItem> adapter, SimpleItem item, int position) {
Toast.makeText(v.getContext(), (item).name.getText(v.getContext()), Toast.LENGTH_LONG).show();
return false;
}
});
//configure the itemAdapter
fastItemAdapter.withFilterPredicate(new IItemAdapter.Predicate<SimpleItem>() {
@Override
public boolean filter(SimpleItem item, CharSequence constraint) {
//return false to keep it
return !item.name.getText().toLowerCase().contains(constraint.toString().toLowerCase());
}
});
fastItemAdapter.getItemAdapter().withItemFilterListener(this);
//get our recyclerView and do basic setup
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(footerAdapter.wrap(fastItemAdapter));
recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(footerAdapter) {
@Override
public void onLoadMore(final int currentPage) {
footerAdapter.clear();
footerAdapter.add(new ProgressItem().withEnabled(false));
//simulate networking (2 seconds)
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
footerAdapter.clear();
for (int i = 1; i < 16; i++) {
fastItemAdapter.add(fastItemAdapter.getAdapterItemCount(), new SimpleItem().withName("Item " + i + " Page " + currentPage));
}
}
}, 2000);
}
});
//fill with some sample data (load the first page here)
List<SimpleItem> items = new ArrayList<>();
for (int i = 1; i < 16; i++) {
items.add(new SimpleItem().withName("Item " + i + " Page " + 1));
}
fastItemAdapter.add(items);
//add drag and drop for item
touchCallback = new SimpleDragCallback(this);
// Create ItemTouchHelper and pass with parameter the SimpleDragCallback
touchHelper = new ItemTouchHelper(touchCallback);
// Attach ItemTouchHelper to RecyclerView
touchHelper.attachToRecyclerView(recyclerView);
//restore selections (this has to be done after the items were added
fastItemAdapter.withSavedInstanceState(savedInstanceState);
//set the back arrow in the toolbar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(false);
}
use of android.support.v7.widget.RecyclerView.Adapter in project FastAdapter by mikepenz.
the class ExpandableMultiselectDeleteSampleActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
findViewById(android.R.id.content).setSystemUiVisibility(findViewById(android.R.id.content).getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
//as we use an icon from Android-Iconics via xml we add the IconicsLayoutInflater
//https://github.com/mikepenz/Android-Iconics
LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
// get RecyclerView
final RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
// Handle Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.string.sample_collapsible);
//style our ui
new MaterializeBuilder().withActivity(this).build();
//create our FastAdapter
fastItemAdapter = new FastItemAdapter<>();
fastItemAdapter.withPositionBasedStateManagement(false).withSelectable(true).withMultiSelect(true).withSelectOnLongClick(true).withOnPreClickListener(new FastAdapter.OnClickListener<IItem>() {
@Override
public boolean onClick(View v, IAdapter<IItem> adapter, IItem item, int position) {
//we handle the default onClick behavior for the actionMode. This will return null if it didn't do anything and you can handle a normal onClick
Boolean res = mActionModeHelper.onClick(ExpandableMultiselectDeleteSampleActivity.this, item);
// so that the click listener is not fired
if (res != null && !res)
return true;
return res != null ? res : false;
}
}).withOnClickListener(new FastAdapter.OnClickListener<IItem>() {
@Override
public boolean onClick(View v, IAdapter<IItem> adapter, IItem item, int position) {
// check if the actionMode consumes the click. This returns true, if it does, false if not
if (!mActionModeHelper.isActive())
Toast.makeText(ExpandableMultiselectDeleteSampleActivity.this, ((SimpleSubItem) item).name + " clicked!", Toast.LENGTH_SHORT).show();
// else
// mFastAdapter.notifyItemChanged(position); // im Bsp. ist das nicht nötig, k.A. warum ich das machen muss!
mRangeSelectorHelper.onClick();
return false;
}
}).withOnPreLongClickListener(new FastAdapter.OnLongClickListener<IItem>() {
@Override
public boolean onLongClick(View v, IAdapter<IItem> adapter, IItem item, int position) {
boolean actionModeWasActive = mActionModeHelper.isActive();
ActionMode actionMode = mActionModeHelper.onLongClick((AppCompatActivity) ExpandableMultiselectDeleteSampleActivity.this, position);
mRangeSelectorHelper.onLongClick(position);
if (actionMode != null) {
//we want color our CAB
ExpandableMultiselectDeleteSampleActivity.this.findViewById(R.id.action_mode_bar).setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(ExpandableMultiselectDeleteSampleActivity.this, R.attr.colorPrimary, R.color.material_drawer_primary));
// start the drag selection
mDragSelectTouchListener.startDragSelection(position);
}
//if we have no actionMode we do not consume the event
return actionMode != null && !actionModeWasActive;
}
});
// provide a custom title provider that even shows the count of sub items
mActionModeHelper = new ActionModeHelper(fastItemAdapter, R.menu.cab, new ActionBarCallBack()).withTitleProvider(new ActionModeHelper.ActionModeTitleProvider() {
@Override
public String getTitle(int selected) {
return selected + "/" + SubItemUtil.countItems(fastItemAdapter.getItemAdapter(), false);
}
}).withSupportSubItems(true);
// this will take care of selecting range of items via long press on the first and afterwards on the last item
mRangeSelectorHelper = new RangeSelectorHelper(fastItemAdapter).withSavedInstanceState(savedInstanceState).withActionModeHelper(mActionModeHelper);
// setup the drag select listener and add it to the RecyclerView
mDragSelectTouchListener = new DragSelectTouchListener().withSelectListener(new DragSelectTouchListener.OnDragSelectListener() {
@Override
public void onSelectChange(int start, int end, boolean isSelected) {
mRangeSelectorHelper.selectRange(start, end, isSelected, true);
// we handled the long press, so we reset the range selector
mRangeSelectorHelper.reset();
}
});
rv.addOnItemTouchListener(mDragSelectTouchListener);
// do basic RecyclerView setup
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setItemAnimator(new SlideDownAlphaAnimator());
rv.setAdapter(fastItemAdapter);
//fill with some sample data
List<IItem> items = new ArrayList<>();
for (int i = 0; i < 20; i++) {
if (i % 2 == 0) {
final HeaderSelectionItem expandableItem = new HeaderSelectionItem();
expandableItem.withSubSelectionProvider(new HeaderSelectionItem.ISubSelectionProvider() {
@Override
public int getSelectedSubItems() {
return SubItemUtil.countSelectedSubItems(fastItemAdapter, expandableItem);
}
}).withName("Test " + (i + 1)).withDescription("ID: " + (i + 1)).withIdentifier(i + 1);
//.withIsExpanded(true) don't use this in such a setup, use adapter.expand() to expand all items instead
//add subitems so we can showcase the collapsible functionality
List<IItem> subItems = new LinkedList<>();
for (int ii = 1; ii <= 5; ii++) {
final SimpleSubItem sampleItem = new SimpleSubItem();
sampleItem.withName("-- Test " + (i + 1) + "." + ii).withDescription("ID: " + (i + 1) * 100 + ii).withIdentifier((i + 1) * 100 + ii);
subItems.add(sampleItem);
}
expandableItem.withSubItems(subItems);
items.add(expandableItem);
} else {
SimpleSubItem sampleItem = new SimpleSubItem();
sampleItem.withName("Test " + (i + 1)).withDescription("ID: " + (i + 1)).withIdentifier(i + 1);
items.add(sampleItem);
}
}
fastItemAdapter.add(items);
fastItemAdapter.expand();
fastItemAdapter.withSelectionListener(new ISelectionListener() {
@Override
public void onSelectionChanged(IItem item, boolean selected) {
if (item instanceof SimpleSubItem) {
IItem headerItem = ((SimpleSubItem) item).getParent();
if (headerItem != null) {
int pos = fastItemAdapter.getAdapterPosition(headerItem);
// Important: notify the header directly, not via the notifyadapterItemChanged!
// we just want to update the view and we are sure, nothing else has to be done
fastItemAdapter.notifyItemChanged(pos);
}
}
}
});
//restore selections (this has to be done after the items were added
fastItemAdapter.withSavedInstanceState(savedInstanceState);
//set the back arrow in the toolbar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(false);
// restore action mode
if (savedInstanceState != null)
mActionModeHelper.checkActionMode(this);
}
use of android.support.v7.widget.RecyclerView.Adapter in project FastAdapter by mikepenz.
the class MultiselectSampleActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
findViewById(android.R.id.content).setSystemUiVisibility(findViewById(android.R.id.content).getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
// Handle Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.string.sample_multi_select);
//style our ui
new MaterializeBuilder().withActivity(this).build();
//create our FastAdapter
mFastAdapter = new FastAdapter<>();
//
mUndoHelper = new UndoHelper(mFastAdapter, new UndoHelper.UndoListener<SimpleItem>() {
@Override
public void commitRemove(Set<Integer> positions, ArrayList<FastAdapter.RelativeInfo<SimpleItem>> removed) {
Log.e("UndoHelper", "Positions: " + positions.toString() + " Removed: " + removed.size());
}
});
//we init our ActionModeHelper
mActionModeHelper = new ActionModeHelper(mFastAdapter, R.menu.cab, new ActionBarCallBack());
//create our adapters
ItemAdapter<SimpleItem> itemAdapter = new ItemAdapter<>();
final HeaderAdapter<SimpleItem> headerAdapter = new HeaderAdapter<>();
//configure our mFastAdapter
//as we provide id's for the items we want the hasStableIds enabled to speed up things
mFastAdapter.setHasStableIds(true);
mFastAdapter.withSelectable(true);
mFastAdapter.withMultiSelect(true);
mFastAdapter.withSelectOnLongClick(true);
mFastAdapter.withOnPreClickListener(new FastAdapter.OnClickListener<SimpleItem>() {
@Override
public boolean onClick(View v, IAdapter<SimpleItem> adapter, SimpleItem item, int position) {
//we handle the default onClick behavior for the actionMode. This will return null if it didn't do anything and you can handle a normal onClick
Boolean res = mActionModeHelper.onClick(item);
return res != null ? res : false;
}
});
mFastAdapter.withOnClickListener(new FastAdapter.OnClickListener<SimpleItem>() {
@Override
public boolean onClick(View v, IAdapter<SimpleItem> adapter, SimpleItem item, int position) {
Toast.makeText(v.getContext(), "SelectedCount: " + mFastAdapter.getSelections().size() + " ItemsCount: " + mFastAdapter.getSelectedItems().size(), Toast.LENGTH_SHORT).show();
return false;
}
});
mFastAdapter.withOnPreLongClickListener(new FastAdapter.OnLongClickListener<SimpleItem>() {
@Override
public boolean onLongClick(View v, IAdapter<SimpleItem> adapter, SimpleItem item, int position) {
ActionMode actionMode = mActionModeHelper.onLongClick(MultiselectSampleActivity.this, position);
if (actionMode != null) {
//we want color our CAB
findViewById(R.id.action_mode_bar).setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(MultiselectSampleActivity.this, R.attr.colorPrimary, R.color.material_drawer_primary));
}
//if we have no actionMode we do not consume the event
return actionMode != null;
}
});
//get our recyclerView and do basic setup
RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setItemAnimator(new SlideDownAlphaAnimator());
rv.setAdapter(itemAdapter.wrap(headerAdapter.wrap(mFastAdapter)));
//fill with some sample data
SimpleItem SimpleItem = new SimpleItem();
SimpleItem.withName("Header").withIdentifier(1).withSelectable(false);
headerAdapter.add(SimpleItem);
List<SimpleItem> items = new ArrayList<>();
for (int i = 1; i <= 100; i++) {
SimpleItem item = new SimpleItem();
item.withName("Test " + i).withIdentifier(100 + i);
items.add(item);
}
itemAdapter.add(items);
//restore selections (this has to be done after the items were added
mFastAdapter.withSavedInstanceState(savedInstanceState);
//set the back arrow in the toolbar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(false);
//inform that longClick is required
Toast.makeText(this, "LongClick to enable Multi-Selection", Toast.LENGTH_LONG).show();
}
use of android.support.v7.widget.RecyclerView.Adapter in project FastAdapter by mikepenz.
the class FastAdapterDiffUtil method set.
public static <A extends ItemAdapter<Item>, Item extends IItem> A set(final A adapter, final List<Item> items, final DiffCallback<Item> callback, final boolean detectMoves) {
if (adapter.isUseIdDistributor()) {
IdDistributor.checkIds(items);
}
//first collapse all items
adapter.getFastAdapter().collapse(false);
//if we have a comparator then sort
if (adapter.getComparator() != null) {
Collections.sort(items, adapter.getComparator());
}
//map the types
adapter.mapPossibleTypes(items);
//remember the old items
final List<Item> oldItems = adapter.getAdapterItems();
DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() {
@Override
public int getOldListSize() {
return oldItems.size();
}
@Override
public int getNewListSize() {
return items.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return callback.areItemsTheSame(oldItems.get(oldItemPosition), items.get(newItemPosition));
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return callback.areContentsTheSame(oldItems.get(oldItemPosition), items.get(newItemPosition));
}
@Nullable
@Override
public Object getChangePayload(int oldItemPosition, int newItemPosition) {
Object result = callback.getChangePayload(oldItems.get(oldItemPosition), oldItemPosition, items.get(newItemPosition), newItemPosition);
return result == null ? super.getChangePayload(oldItemPosition, newItemPosition) : result;
}
}, detectMoves);
//make sure the new items list is not a reference of the already mItems list
if (items != oldItems) {
//remove all previous items
if (!oldItems.isEmpty()) {
oldItems.clear();
}
//add all new items to the list
oldItems.addAll(items);
}
result.dispatchUpdatesTo(new ListUpdateCallback() {
@Override
public void onInserted(int position, int count) {
adapter.getFastAdapter().notifyAdapterItemRangeInserted(adapter.getFastAdapter().getPreItemCountByOrder(adapter.getOrder()) + position, count);
}
@Override
public void onRemoved(int position, int count) {
adapter.getFastAdapter().notifyAdapterItemRangeRemoved(adapter.getFastAdapter().getPreItemCountByOrder(adapter.getOrder()) + position, count);
}
@Override
public void onMoved(int fromPosition, int toPosition) {
adapter.getFastAdapter().notifyAdapterItemMoved(adapter.getFastAdapter().getPreItemCountByOrder(adapter.getOrder()) + fromPosition, toPosition);
}
@Override
public void onChanged(int position, int count, Object payload) {
adapter.getFastAdapter().notifyAdapterItemRangeChanged(adapter.getFastAdapter().getPreItemCountByOrder(adapter.getOrder()) + position, count, payload);
}
});
return adapter;
}
use of android.support.v7.widget.RecyclerView.Adapter in project FastAdapter by mikepenz.
the class SwipeListActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
findViewById(android.R.id.content).setSystemUiVisibility(findViewById(android.R.id.content).getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
// Handle Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//style our ui
new MaterializeBuilder().withActivity(this).build();
//create our FastAdapter which will manage everything
fastItemAdapter = new FastItemAdapter<>();
//configure our fastAdapter
fastItemAdapter.withOnClickListener(new FastAdapter.OnClickListener<SwipeableItem>() {
@Override
public boolean onClick(View v, IAdapter<SwipeableItem> adapter, SwipeableItem item, int position) {
Toast.makeText(v.getContext(), (item).name.getText(v.getContext()), Toast.LENGTH_LONG).show();
return false;
}
});
//configure the itemAdapter
fastItemAdapter.withFilterPredicate(new IItemAdapter.Predicate<SwipeableItem>() {
@Override
public boolean filter(SwipeableItem item, CharSequence constraint) {
//return false to keep it
return !item.name.getText().toLowerCase().contains(constraint.toString().toLowerCase());
}
});
//get our recyclerView and do basic setup
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(fastItemAdapter);
//fill with some sample data
int x = 0;
List<SwipeableItem> items = new ArrayList<>();
for (String s : ALPHABET) {
int count = new Random().nextInt(20);
for (int i = 1; i <= count; i++) {
SwipeableItem swipeableItem = new SwipeableItem().withName(s + " Test " + x).withIdentifier(100 + x);
swipeableItem.withIsSwipeable(i % 5 != 0);
items.add(swipeableItem);
x++;
}
}
fastItemAdapter.add(items);
//add drag and drop for item
//and add swipe as well
Drawable leaveBehindDrawableLeft = new IconicsDrawable(this).icon(MaterialDesignIconic.Icon.gmi_delete).color(Color.WHITE).sizeDp(24);
Drawable leaveBehindDrawableRight = new IconicsDrawable(this).icon(MaterialDesignIconic.Icon.gmi_archive).color(Color.WHITE).sizeDp(24);
touchCallback = new SimpleSwipeDragCallback(this, this, leaveBehindDrawableLeft, ItemTouchHelper.LEFT, ContextCompat.getColor(this, R.color.md_red_900)).withBackgroundSwipeRight(ContextCompat.getColor(this, R.color.md_blue_900)).withLeaveBehindSwipeRight(leaveBehindDrawableRight);
// Create ItemTouchHelper and pass with parameter the SimpleDragCallback
touchHelper = new ItemTouchHelper(touchCallback);
// Attach ItemTouchHelper to RecyclerView
touchHelper.attachToRecyclerView(recyclerView);
//restore selections (this has to be done after the items were added
fastItemAdapter.withSavedInstanceState(savedInstanceState);
//set the back arrow in the toolbar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(false);
}
Aggregations