我想为RecyclerView设置一个longClick事件,但它似乎不起作用。请注意,当longClicking时,我还需要点击项目的位置(请参阅下面的代码)。这是RecyclerView所在片段的onCreateView中的代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.whitelist, container, false);
final RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(Utils.context));
rcAdapter = new RecyclerAdapter();
mRecyclerView.setAdapter(rcAdapter);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
final int itemPosition = mRecyclerView.getChildAdapterPosition(view);
AlertDialog deleteDialog = new AlertDialog.Builder(Utils.context)
.setTitle(getString(R.string.delete))
.setMessage(getString(R.string.delete_confirm) + Utils.cInfo.get(itemPosition).name + "?")
.setIcon(R.drawable.ic_launcher)
.setPositiveButton(getString(R.string.delete), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Utils.cInfo.remove(itemPosition);
Utils.convertWhitelistToJSON();
rcAdapter.notifyItemRemoved(itemPosition);
dialog.dismiss();
}
})
.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
return true;
}
});
return rootView;
}