use of com.mikepenz.fastadapter_extensions.swipe.SimpleSwipeDragCallback 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