Search in sources :

Example 1 with SwipeableItem

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);
}
Also used : SimpleSwipeDragCallback(com.mikepenz.fastadapter_extensions.swipe.SimpleSwipeDragCallback) SwipeableItem(com.mikepenz.fastadapter.app.items.SwipeableItem) ArrayList(java.util.ArrayList) IconicsDrawable(com.mikepenz.iconics.IconicsDrawable) Drawable(android.graphics.drawable.Drawable) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) MaterializeBuilder(com.mikepenz.materialize.MaterializeBuilder) SearchView(android.support.v7.widget.SearchView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) DefaultItemAnimator(android.support.v7.widget.DefaultItemAnimator) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) Random(java.util.Random) IItemAdapter(com.mikepenz.fastadapter.IItemAdapter) RecyclerView(android.support.v7.widget.RecyclerView) FastAdapter(com.mikepenz.fastadapter.FastAdapter) IconicsDrawable(com.mikepenz.iconics.IconicsDrawable) Toolbar(android.support.v7.widget.Toolbar)

Example 2 with SwipeableItem

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?
}
Also used : SwipeableItem(com.mikepenz.fastadapter.app.items.SwipeableItem) SearchView(android.support.v7.widget.SearchView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Aggregations

RecyclerView (android.support.v7.widget.RecyclerView)2 SearchView (android.support.v7.widget.SearchView)2 View (android.view.View)2 SwipeableItem (com.mikepenz.fastadapter.app.items.SwipeableItem)2 Drawable (android.graphics.drawable.Drawable)1 DefaultItemAnimator (android.support.v7.widget.DefaultItemAnimator)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 Toolbar (android.support.v7.widget.Toolbar)1 ItemTouchHelper (android.support.v7.widget.helper.ItemTouchHelper)1 FastAdapter (com.mikepenz.fastadapter.FastAdapter)1 IItemAdapter (com.mikepenz.fastadapter.IItemAdapter)1 SimpleSwipeDragCallback (com.mikepenz.fastadapter_extensions.swipe.SimpleSwipeDragCallback)1 IconicsDrawable (com.mikepenz.iconics.IconicsDrawable)1 MaterializeBuilder (com.mikepenz.materialize.MaterializeBuilder)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1