试图让我的测试应用程序我遇到了一个奇怪的问题。 我设置onClick以显示AlertDialog和onLongClick以显示ContextMenu。保持触摸一定时间触发两种方法,AlertDialog覆盖ContextMenu。 onClick和onLongClick似乎是并行触发的
我的recycleView
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(),
recyclerView, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, final int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
ViewGroup viewGroupRecycle = findViewById(R.id.layoutParent);
View viewUpdate = getLayoutInflater().inflate(R.layout.create, viewGroupRecycle);
final EditText text = viewUpdate.findViewById(R.id.edit_text);
builder.setView(viewUpdate);
builder.setTitle(note_update);
final int id = notesList.get(position).getId();
String textStr = notesList.get(position).getText();
text.setText(textStr);
text.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) MainActivity.this
.getSystemService(Context.INPUT_METHOD_SERVICE);
assert keyboard != null;
keyboard.showSoftInput(text, 0);
}
}, 50);
builder.setPositiveButton(save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strText = removeSeparator(text);
Boolean check = checked(strText);
if (check){
updatable(strText, id);
prepareData();
alertDialog.dismiss();
} else
message(error);
}
});
}
@Override
public void onLongClick(View view, int position) {
}
}));
我的ContextMenu
@Override
public void onCreateContextMenu(ContextMenu menu, final View v, ContextMenuInfo menuInfo) {
menu.add(0, R.id.move, 0, R.string.move_widget);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
try {
int position = ((NotesAdapter)recyclerView.getAdapter()).getPosition();
switch (item.getItemId()){
case R.id.move:
Intent intent = new Intent(MainActivity.this, WriteNotes.class);
intent.putExtra("key", position);
sendBroadcast(intent);
break;
}
} catch (Exception e){
Log.e("TAG", e.getMessage());
}
return super.onContextItemSelected(item);
}
答案 0 :(得分:1)
您可以在自定义适配器实现中添加侦听器。它将类似于:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
CustomItemClickListener customItemClickListener;
public interface CustomItemClickListener {
public void onItemClicked(int position);
public boolean onItemLongClicked(int position);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public View v;
public ViewHolder(View v) {
super(v);
this.v = v;
}
}
public RecyclerViewAdapter(CustomItemClickListener _customItemClickListener;) {
customItemClickListener = _customItemClickListener;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customItemClickListener.onItemClicked(position);
}
});
holder.v.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
customItemClickListener.onItemLongClicked(position);
return true;
}
});
}
}
活动类如下:
RecyclerView recyclerView;
ItemsListAdapter adapter;
recyclerView = (RecyclerView)findViewById(R.id.items_list);
recyclerView.setHasFixedSize(true);
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLinearLayoutManager);
adapter = new ItemsListAdapter( new CustomItemClickListener() {
@Override
public void onItemClick(View v, int position) {
Log.d(TAG, "onItemClick position:" + position);
// do what ever you want to do with it
}
@Override
public void onLongClick(View v, int position) {
Log.d(TAG, "onLongClick position:" + position);
// do what ever you want to do with it
}
});
recyclerView.setAdapter(adapter);
我收到错误消息
java.lang.NullPointerException: Attempt to invoke interface method 'void com.personal.anton.notes.recycle.NotesAdapter$CustomClickListener.onItemClickListener(android.view.View, int)' on a null object reference
at com.personal.anton.notes.recycle.NotesAdapter$1.onClick(NotesAdapter.java:72)
public class NotesAdapter extends Adapter<NotesAdapter.NotesHolder> {
private List<Notes> notesList;
private NotesHandler notesHandler;
private Context context;
private int position;
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public CustomClickListener listener;
public interface CustomClickListener{
void onItemClickListener(View view, int position);
void onItemLongClickListener(View view, int position);
}
public NotesAdapter(CustomClickListener listener){
this.listener = listener;
}
public NotesAdapter(Context context, List<Notes> notesList){
this.notesList = notesList;
this.context = context;
notesHandler = new NotesHandler(context);
}
@NonNull
@Override
public NotesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final View itemView = LayoutInflater.from(parent.getContext()).
inflate(R.layout.view_row, parent, false);
return new NotesHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final NotesHolder holder, final int position) {
Notes notes = notesList.get(position);
holder.tvText.setText(notes.getText());
holder.tvDate.setText(notes.getDate());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClickListener(v, position);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
listener.onItemLongClickListener(v, position);
return true;
}
});
}
@Override
public int getItemCount() {
return notesList.size();
}
public void removeItem(int position){
notesHandler.deleteNotes(new Notes(notesList.get(position).getId()));
notesList.remove(position);
notifyItemInserted(position);
}
public class NotesHolder extends ViewHolder implements OnCreateContextMenuListener{
TextView tvText, tvDate;
RelativeLayout foreground, background;
NotesHolder(View itemView) {
super(itemView);
tvText = itemView.findViewById(R.id.tv_text);
tvDate = itemView.findViewById(R.id.tv_date);
foreground = itemView.findViewById(R.id.liner_foreground);
background = itemView.findViewById(R.id.liner_background);
//itemView.setOnCreateContextMenuListener(this);
}
@Override
public void onCreateContextMenu(ContextMenu menu, final View v, ContextMenuInfo menuInfo) {
menu.add(0, R.id.move, 0, R.string.move_widget);
}
}
}