如何禁用 ViewHolder 中的 onLongPressListener?
How to disable a onLongPressListener in a ViewHolder?
我正在尝试暂时禁用我在适配器的 ViewHolder 中设置的 onLongPressListener。我想禁用它,因为我想实现 RecyclerView 的拖放功能(以允许用户重新排列项目)。
目前,长按监听器允许用户重命名一个项目,我想当用户按下 "rearrange" 按钮(在工具栏中)时我想禁用 viewHolder 的长按监听器并激活拖动和 drop feature.I 不知道我必须如何禁用在 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);
}
}
}
在你的适配器上实现一个标志。
作为 LongClickListener 的第一行,检查标志。如果已设置,则不执行长按操作。你不需要禁用监听器,你只需要阻止它包含的代码执行。
我正在尝试暂时禁用我在适配器的 ViewHolder 中设置的 onLongPressListener。我想禁用它,因为我想实现 RecyclerView 的拖放功能(以允许用户重新排列项目)。
目前,长按监听器允许用户重命名一个项目,我想当用户按下 "rearrange" 按钮(在工具栏中)时我想禁用 viewHolder 的长按监听器并激活拖动和 drop feature.I 不知道我必须如何禁用在 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);
}
}
}
在你的适配器上实现一个标志。
作为 LongClickListener 的第一行,检查标志。如果已设置,则不执行长按操作。你不需要禁用监听器,你只需要阻止它包含的代码执行。