2

I am trying to temporary disable onLongPressListener that I set in my adapter's ViewHolder. I want to disable it because I want to implement the Drag and Drop functionality (to allow the user to rearrange the items) of the RecyclerView.

Currently, the long press listener allows the user to rename an item and I want when the user presses the "rearrange" button (in the toolbar) I want to disable the viewHolder's long press listener and activate the drag and drop feature.I have no idea how I have to disable the listener which is set on every view of the recyclerview.

This is my adapter code:

public class GroceryItemsAdapter extends RecyclerView.Adapter<GroceryItemsAdapter.ShoppingListViewHolder> {
    private ArrayList<String> mItems;
    private Context mContext;
    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;
    private MaterialDialog addItemdialog;
    public static String nameOfList;

    public GroceryItemsAdapter(Context context, ArrayList<String> items, SharedPreferences preferences, SharedPreferences.Editor editor, String nameOfList) {
        mItems = items;
        mContext = context;
        mSharedPreferences = preferences;
        mEditor = editor;
        this.nameOfList = nameOfList;
    }

    @Override
    public ShoppingListViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.shopping_list_item,viewGroup,false);
        ShoppingListViewHolder viewHolder = new ShoppingListViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ShoppingListViewHolder shoppingListViewHolder, int position) {
        shoppingListViewHolder.bindShoppingList(mItems.get(position));
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    public class ShoppingListViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener, View.OnLongClickListener{
        public TextView mShoppingListItem;
        public CheckBox mCheckBox;
        public TextView mEmptyTextView;

        public ShoppingListViewHolder(View itemView) {
            super(itemView);
            mShoppingListItem = (TextView) itemView.findViewById(R.id.shoppingListItem);
            mCheckBox = (CheckBox) itemView.findViewById(R.id.shoppingListCheckBox);

            View rootView = ((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content);

            mEmptyTextView = (TextView)rootView.findViewById(R.id.list_empty);
            mEmptyTextView.setVisibility(View.INVISIBLE);
            mCheckBox.setOnCheckedChangeListener(this);
            itemView.setOnLongClickListener(this);
        }
        public void bindShoppingList(String item){
            mShoppingListItem.setText(item);
            mCheckBox.setChecked(false);
        }
    }
}
smac89
  • 39,374
  • 15
  • 132
  • 179
Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126
  • use one flag to enable/disable long click listener, activate flag when click on toolbar button and deactivate flag on completion of drag drop function. – Pratik Tank Oct 05 '15 at 17:39
  • Yes, tried that but that does not do the trick because I set the on click listener on the creation of my adapter and I cannot disable if after that. I tried notifydatasetchanged as well after changing the flag and it did not work as well. – Georgi Koemdzhiev Oct 05 '15 at 17:43

1 Answers1

1

Implement a flag on your adapter.

As the first line on your LongClickListener, check the flag. If it is set, do not perform the Long Click operation. You do not need to disable the listener, you just need to prevent the code it contains from executing.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • Thank you for your suggestion. Although, I think I did it by disabling the listener I might change it to this since it is smarter way of achieving the same thing. – Georgi Koemdzhiev Nov 26 '15 at 10:53