Search in sources :

Example 1 with UndoHelper

use of com.mikepenz.fastadapter_extensions.UndoHelper 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();
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) HeaderAdapter(com.mikepenz.fastadapter.adapters.HeaderAdapter) SlideDownAlphaAnimator(com.mikepenz.itemanimators.SlideDownAlphaAnimator) Toolbar(android.support.v7.widget.Toolbar) ItemAdapter(com.mikepenz.fastadapter.adapters.ItemAdapter) UndoHelper(com.mikepenz.fastadapter_extensions.UndoHelper) MaterializeBuilder(com.mikepenz.materialize.MaterializeBuilder) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) ActionMode(android.support.v7.view.ActionMode) RecyclerView(android.support.v7.widget.RecyclerView) FastAdapter(com.mikepenz.fastadapter.FastAdapter) SimpleItem(com.mikepenz.fastadapter.app.items.SimpleItem) ActionModeHelper(com.mikepenz.fastadapter_extensions.ActionModeHelper)

Aggregations

ActionMode (android.support.v7.view.ActionMode)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 Toolbar (android.support.v7.widget.Toolbar)1 View (android.view.View)1 FastAdapter (com.mikepenz.fastadapter.FastAdapter)1 HeaderAdapter (com.mikepenz.fastadapter.adapters.HeaderAdapter)1 ItemAdapter (com.mikepenz.fastadapter.adapters.ItemAdapter)1 SimpleItem (com.mikepenz.fastadapter.app.items.SimpleItem)1 ActionModeHelper (com.mikepenz.fastadapter_extensions.ActionModeHelper)1 UndoHelper (com.mikepenz.fastadapter_extensions.UndoHelper)1 SlideDownAlphaAnimator (com.mikepenz.itemanimators.SlideDownAlphaAnimator)1 MaterializeBuilder (com.mikepenz.materialize.MaterializeBuilder)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1