What is it?

Small and lightweight extensions to RecycerView.Adpter which adds missing ListView and GridView features to RecyclerView

Usage

You need to extend EffectiveRecyclerAdapter in order to get the benefits of this library.

MultiChoiceMode

MultiChoiceMode is customizable and very easy to use. You just need to set the MultiChoiceModeListener with setMultiChoiceModeListener method. MultiChoiceModeListener has few important callbacks like

@Override
public void onItemSelectionChanged(ActionMode mode, int position, boolean selected) {
    // Called when selection is changed at position index.
}

@Override
public boolean onCreateActionMode(ActionMode mode, MenuInflater inflater, Menu menu) {
    // On Create Action Mode, Inflate the menu.
    inflater.inflate(R.menu.menu_action, menu);
    return true;
}

@Override
public void onDestroyActionMode() {
    // Action mode destroyed.
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    // Action menu item is clicked
    return true;
}

You decide when you need your Item's selection state to be changed. For example, here's a ViewHolder implementation which toggles selection states on onClick and onLongClick events.

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

    public MyViewHolder(TextView itemView) {
       super(itemView);
       itemView.setOnClickListener(this);
       itemView.setOnLongClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // You decide when Selected state should be change
        if (mMyAdapter.isActionModeActive()) {
            mMyAdapter.toggleSelected(getAdapterPosition());
        } else {
            // Do normal onclick action
        } 
    }

    @Override
    public boolean onLongClick(View v) {
        // You decide when Selected state should be change
        mMyAdapter.toggleSelected(getAdapterPosition());
        return true;
    }
}

You can check if item is selected in your Adapter's onBindViewHolder method and change view according to it.

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    // Check if item is selected and set the appropriate view
    holder.item.setBackgroundColor(isSelected(position) ? Color.GRAY : Color.WHITE);
}

Upcoming

How do I get it?

Maven

<dependency>
  <groupId>com.jimmy.effective-recycler-adapter</groupId>
  <artifactId>effective-recycler-adapter</artifactId>
  <version>1.0.1</version>
</dependency>

Gradle

compile 'com.jimmy.effective-recycler-adapter:effective-recycler-adapter:1.0.1'