Search code examples
androidandroid-recyclerviewonitemlongclicklistener

Android: RecyclerView adapter longClickListener not working


I am trying to use longClickListener in my Fragment but for some reason it is just not working. If I do a long click, event is received by onItemClickListener

This is my code

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private ArrayList<Info> Infos;
    private ClickListener clickListener;
    private MyLongClickListener myLongClickListener;

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        // each data item is just a string in this case
        public ImageView image;
        public/* Roboto */ TextView name;
        public/* Roboto */ TextView companyName;
        public/* Roboto */ TextView city;
        public/* Roboto */ TextView time;
        public/* Material */ TextView icon;

        public ViewHolder(View convertView) {
            super(convertView);
            companyName = (TextView) convertView
                    .findViewById(R.id.t1);
            city = (TextView) convertView
                    .findViewById(R.id.t2);
            convertView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            clickListener.onItemClick(getPosition(), v);
        }

        @Override
        public boolean onLongClick(View v) {
            myLongClickListener.onItemLongClick(getPosition(), v);
            return true;
        }
    }

    public void add(int position, Info item) {
        Infos.add(position, item);
        notifyItemInserted(position);
    }


    // Provide a suitable constructor (depends on the kind of dataset)
    public RecyclerViewAdapter(ArrayList<Info> dummyModelList) {
        Infos = dummyModelList;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                             int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.genre_list_item, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Info currInfo = Infos.get(position);

        holder.city.setText(currInfo.getTitle());
        holder.companyName.setText(currInfo.getArtist());

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return Infos.size();
    }


    public void setOnItemClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setOnItemLongClickListener(MyLongClickListener clickListener) {
        this.myLongClickListener = clickListener;
    }

    public interface ClickListener {
        void onItemClick(int position, View v);
    }

    public interface MyLongClickListener {
        boolean onItemLongClick(int position, View v);
    }
}

This is how I use it inside my Fragment. This onLongClick and onItemClickListener both are placed in onResume function of the Fragment.

recyclerViewAdapter.setOnItemLongClickListener(new
    RecyclerViewAdapter.MyLongClickListener() {
        @Override
        public boolean onItemLongClick(int position, View v) {
            // Task
            return true;
        }
});

I can't see any mistake in my code but don't know why it is not working. The touch event is always received by onItemClickListener. Any help would be appreciated, Thanks !!!


Solution

  • Set both click and onclicklisteners in your constructor.

            convertView.setOnClickListener(this);
            convertView.setOnLongClickListener(this);
    

    You can not listen long clicks without setOnLongClickListener.