Working with a Long Click Listener with Android Recyclers

I am working on notapad as an Android app project. I which I have implemented recycler. My project contains a NotedAdaper class that extends RecyclerView.Adapter<NotesAdapter.ViewHolder>
in this class using the code below, I used a 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 his absolutely working find. when you click an item in the recycler, it retrieves data using the position of that item. and display in other activities. likewise, suppose the activity shows a list of notes created by the user. and clicking on any note, it shows the full content of this note.

but now I want to implement a long click on an element. and get a position. so 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;
            }
        });  

. , , . . → " :..." , . . . ??? ??? ?

+4
3

, . 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", bydefault - "false". .

+12

onLongClick(View v) return true return false , .

+2

I think you should set both class listeners ViewHolder.

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

And call getAdapterPosition()from ViewHolderto get the element adapter position.

You can check the following resource. https://www.bignerdranch.com/blog/recyclerview-part-1-fundamentals-for-listview-experts/

+1
source

Source: https://habr.com/ru/post/1659096/


All Articles