使用带有 recycler-android 的长按监听器

新手上路,请多包涵

我正在研究像 android 应用程序项目这样的记事本。我已经实施了回收站。我的项目包含扩展 RecyclerView.Adapter<NotesAdapter.ViewHolder> 的 NotedAdaper 类

在那个使用下面代码的类中,我使用了点击监听器,

 public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.ViewHolder> {

private List<Notes> mNotes;
private Context mContext;

public NotesAdapter(Context context, List<Notes> notes) {
    mNotes = notes;
    mContext = context;
}

@Override
public NotesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    // Inflate the custom layout
    View notesView = inflater.inflate(R.layout.items_notes, parent, false);

    // Return a new holder instance
    ViewHolder viewHolder = new ViewHolder(notesView);
    return viewHolder;
}

// Easy access to the context object in the recyclerview
private Context getContext() {
    return mContext;
}

@Override
public void onBindViewHolder(NotesAdapter.ViewHolder viewHolder, final int position) {

    // Get the data model based on position
    Notes notes = mNotes.get(position);

    // Set item views based on your views and data model
    TextView textView = viewHolder.preTitle;
    textView.setText(notes.getTitle());
    TextView textView1 = viewHolder.preText;
    textView1.setText(notes.getText());
    String color=notes.getColor();

    CardView preCard=viewHolder.preCard;
    preCard.setBackgroundColor(Color.parseColor(color));
    ImageView img = viewHolder.preImage;
    img.setVisibility(View.GONE);

    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Notes notes = mNotes.get(position);
            Intent intent = new Intent(view.getContext(),EditNote.class);

            Bundle bundle = new Bundle();
            bundle.putSerializable("DATA",notes);
            intent.putExtras(bundle);
            getContext().startActivity(intent);

            Toast.makeText(getContext(), "Recycle Click" + position+"  ", Toast.LENGTH_SHORT).show();
        }
    });
}

// Returns the total count of items in the list
@Override
public int getItemCount() {
    return mNotes.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    // Your holder should contain a member variable
    // for any view that will be set as you render a row
    public RobotoTextView preTitle, preText;
    public ImageView preImage;
    public CardView preCard;

    public ViewHolder(View itemView) {
        super(itemView);

        preTitle = (RobotoTextView) itemView.findViewById(R.id.preTitle);
        preText = (RobotoTextView) itemView.findViewById(R.id.preText);
        preImage=(ImageView) itemView.findViewById(R.id.preImage);
        preCard=(CardView) itemView.findViewById(R.id.preCard);

    }
}}

它绝对有效的发现。单击回收器中的某个项目时,它会使用该项目的位置检索数据。并在另一项活动中展示。就像,假设一个活动显示用户创建的笔记列表。点击任何笔记,它会显示该笔记的全部内容。

但现在我想在项目上实现长按监听器。并获得职位。所以,我使用了以下代码……

 viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Notes notes = mNotes.get(position);
                Toast.makeText(getContext(), "long Click" + position+"  ", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

所以,它也在工作。但我想要的是,长按后,它应该只显示 Toast。但它不仅显示长按吐司。但也识别点击侦听器并在显示敬酒后>>“长按:…”它执行为单击事件编写的代码。我不想要它。两个听众应该分开工作。但为什么它在长按后执行单击???任何的想法???

我在任何地方犯错了吗?

原文由 varsha valanju 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 318
2 个回答

因此,我的代码中的以下更改帮助我实现了我的输出。 1)每次将视图与数据绑定时都会调用 onBindViewHolder 方法。所以没有设置点击监听器的最佳位置。您不必为一个视图多次设置 OnClickListener。这就是为什么,我在 ViewHolder 中编写了点击侦听器,(实际上这不是我的问题,但我在某处读到这将是最佳实践,这就是我遵循它的原因)

像这样,

 public static class ViewHolder extends RecyclerView.ViewHolder {
        // Your holder should contain a member variable
        // for any view that will be set as you render a row
        public ImageView preImage;
        public CardView preCard;

        // We also create a constructor that accepts the entire item row
        // and does the view lookups to find each subview
        public ViewHolder(final View itemView) {
            // Stores the itemView in a public final member variable that can be used
            // to access the context from any ViewHolder instance.
            super(itemView);
            itemView.findViewById(R.id.preTitle);
            preImage=(ImageView) itemView.findViewById(R.id.preImage);
            preCard=(CardView) itemView.findViewById(R.id.preCard);

            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int p=getLayoutPosition();
                    System.out.println("LongClick: "+p);
                    return true;// returning true instead of false, works for me
                }
            });

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   int p=getLayoutPosition();

                   Notes notes = mNotes.get(p);
                   Toast.makeText(getContext(), "Recycle Click" + p +"  ", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

您可能会注意到,在 onLongClick 中,我返回了“true”,默认情况下它是“false”。这个改变对我有用。

原文由 varsha valanju 发布,翻译遵循 CC BY-SA 3.0 许可协议

只需制作 onLongClick(View v) 返回 return true 而不是 return false 这解决了我的问题它也应该解决你的问题

原文由 Amir Eleuch 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题