use of com.mikepenz.fastadapter.app.items.SwipeableItem 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);
}
use of com.mikepenz.fastadapter.app.items.SwipeableItem in project FastAdapter by mikepenz.
the class SwipeListActivity method itemSwiped.
@Override
public void itemSwiped(int position, int direction) {
// -- Option 1: Direct action --
//do something when swiped such as: select, remove, update, ...:
//A) fastItemAdapter.select(position);
//B) fastItemAdapter.remove(position);
//C) update item, set "read" if an email etc
// -- Option 2: Delayed action --
final SwipeableItem item = fastItemAdapter.getItem(position);
item.setSwipedDirection(direction);
// This can vary depending on direction but remove & archive simulated here both results in
// removal from list
final Runnable removeRunnable = new Runnable() {
@Override
public void run() {
item.setSwipedAction(null);
int position = fastItemAdapter.getAdapterPosition(item);
if (position != RecyclerView.NO_POSITION) {
//this sample uses a filter. If a filter is used we should use the methods provided by the filter (to make sure filter and normal state is updated)
((ItemAdapter.ItemFilter) fastItemAdapter.getItemFilter()).remove(position);
}
}
};
final View rv = findViewById(R.id.rv);
rv.postDelayed(removeRunnable, 3000);
item.setSwipedAction(new Runnable() {
@Override
public void run() {
rv.removeCallbacks(removeRunnable);
item.setSwipedDirection(0);
int position = fastItemAdapter.getAdapterPosition(item);
if (position != RecyclerView.NO_POSITION) {
fastItemAdapter.notifyItemChanged(position);
}
}
});
fastItemAdapter.notifyItemChanged(position);
//TODO can this above be made more generic, along with the support in the item?
}
Aggregations