简体   繁体   English

使用recycleler-android使用Long Click Listener

[英]Working with Long Click Listener with recycler-android

i am working on a notapad like android app project. 我正在开发像Android应用程序项目的notapad。 i which i have implemented recycler. 我已经实施了回收商。 My project contains NotedAdaper class that extends RecyclerView.Adapter<NotesAdapter.ViewHolder> 我的项目包含扩展RecyclerView.Adapter<NotesAdapter.ViewHolder> NotedAdaper类
in that class using the below code i used click listener, 在该类中使用以下代码我使用了点击监听器,

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);

    }
}}   

And its absolutely working find. 它绝对有效。 on clicking of a item in recycler, it retrieves the data using position of that item. 在单击Recycler中的项目时,它使用该项目的位置检索数据。 and showing in another activity. 并在另一项活动中展示。 just like, suppose a activity shows the list of notes created by user. 就像,假设一个活动显示用户创建的笔记列表。 and clicking on any note, it shows the full content of that note. 并单击任何注释,它会显示该注释的完整内容。

but now i want to implement Long click listener on the item. 但现在我想在项目上实现Long click监听器。 and get the position. 并得到这个位置。 so that, i used the following code ... 所以,我使用以下代码......

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;
            }
        });  

so, its also working. 所以,它也有效。 but what i want is, on long click, it should only show that Toast. 但我想要的是,长按一下,它应该只显示Toast。 but its not only showing the long click toast. 但它不仅显示长按烤面包。 but also recognising click listener and after showing the toast>> "Long click: ..." it executing the the code written for single click event. 但是还要识别点击监听器并在显示toast >>“长按:...”后执行为单击事件编写的代码。 ni dont want it. 你不想要它。 both listeners should work separately. 两个听众都应该分开工作。 but why its executing single click after long click??? 但为什么它长时间点击后执行单击??? any idea??? 任何的想法???
Am i making mistake anywhere? 我在哪里弄错了?

So, the following changes in my code, help me to achieve my output. 所以,我的代码中的以下更改,帮助我实现我的输出。 1) The method onBindViewHolder is called every time when you bind your view with data. 1)每次将视图与数据绑定时,都会调用onBindViewHolder方法。 So there is not the best place to set click listener. 所以没有设置点击监听器的最佳位置。 You don't have to set OnClickListener many times for the one View. 您不必为一个View多次设置OnClickListener。 Thats why, i wrote click listeners in ViewHolder, (actually that was not my question, but i read somewhere that it would be the best practice, thats why i am following it) 这就是为什么,我在ViewHolder中编写了点击监听器,(实际上这不是我的问题,但我在某处读到这将是最好的做法,这就是为什么我要关注它)

like this, 像这样,

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();
                }
            });
        }
    }  

You may notice that, in onLongClick, i have returned "true", bydefault it was "false". 您可能会注意到,在onLongClick中,我已经返回“true”,默认情况下它是“false”。 and this change works for me. 这个改变对我有用。

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

i think you should set both the listeners from ViewHolder class. 我认为你应该从ViewHolder类中设置两个监听ViewHolder

itemView.setOnClickListener(...);
itemView.setOnLongClickListener(...);

And call getAdapterPosition() from ViewHolder to get the adapter position of the item. 并从ViewHolder调用getAdapterPosition()以获取项目的适配器位置。

You can checkout the following resource. 您可以签出以下资源。 https://www.bignerdranch.com/blog/recyclerview-part-1-fundamentals-for-listview-experts/ https://www.bignerdranch.com/blog/recyclerview-part-1-fundamentals-for-listview-experts/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM