我想临时禁用onLongPressListener,我在我的适配器的ViewHolder中设置。我想禁用它,因为我想实现RecyclerView的拖放功能(以允许用户重新排列项目)。如何禁用ViewHolder中的onLongPressListener?
目前,长按听众允许用户重命名一个项目,我想当用户按下“重新排列”按钮(在工具栏中)我想禁用viewHolder的长按听众并激活拖放功能。我不知道如何禁用在recyclerview的每个视图上设置的侦听器。
这是我的适配器代码:
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);
}
}
使用一个标志来启用/禁用长按监听器,当点击工具栏按钮时激活标志并在完成拖放功能时禁用标志。 –
是的,尝试过,但这并没有诀窍,因为我在创建适配器时设置了点击监听器,并且如果在此之后我无法禁用。改变旗子后,我试图改变notifydataset并且它也没有工作。 –