Home > Back-end >  How to add long click listener on recyclerview adapter?
How to add long click listener on recyclerview adapter?

Time:03-19

I already implemented onClick now I want to add onLongClick , I searched non of the solutions matched my case, because my adapter is setup differently

Adapter

public class StudioAdapter extends RecyclerView.Adapter<StudioAdapter.StudioViewHolder> {
    public interface OnItemClickListener {
        void onItemClick(int item);
    }
    private List<String> StudioLogoPaths;
    private OnItemClickListener listener;
    public StudioAdapter(List<String> StudioLogoPaths, OnItemClickListener listener) {
        this.StudioLogoPaths = (List<String>) StudioLogoPaths;
        this.listener = listener;
    }
    @Override
    public StudioAdapter.StudioViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.studio_logo, parent, false);
        StudioViewHolder vh = new StudioViewHolder(v);
        return vh;
    }
    @Override
    public void onBindViewHolder( StudioAdapter.StudioViewHolder vh, int position) {
        final String path = StudioLogoPaths.get(position);
        Glide.with(vh.itemView.getContext()).load(path).into(vh.logoImage);
        String basepath = "/data/user/0/org.courville.nova/app_scraper_studiologos/";
        String extension = ".png";
        final String clicked_studioname = path.replace(basepath, "").replace(extension, "");

        final int Position = position;
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(Position);
                Toast.makeText(vh.itemView.getContext(), clicked_studioname, Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    public int getItemCount() {
        return StudioLogoPaths.size();
    }
    public class StudioViewHolder extends RecyclerView.ViewHolder {
        protected ImageView logoImage;

        public StudioViewHolder(View itemView) {
            super(itemView);
            logoImage = itemView.findViewById(R.id.studio_logo);
        }
    }
}

my Fragment

StudioAdapter.OnItemClickListener studioLogoCallback = new StudioAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(int item) {
                            Glide.with(mContext).clear(mLogo);
                            Glide.with(mContext).load(tags.getStudioLogosLargeFileF().get(item))
                                    .fitCenter().into(mLogo);
                            List<ScraperImage> scraperImage = tags.getStudioLogos();
                            ScraperImage clickedImage = (ScraperImage) scraperImage.get(item);
                            new VideoInfoActivityFragment.ClearLogoSaver(mContext, new VideoInfoActivityFragment()).execute(clickedImage);
                        }
                    };
                    final StudioAdapter studioAdapter = new StudioAdapter(StudioLogoPaths,studioLogoCallback);
                    studios.setAdapter(studioAdapter);

CodePudding user response:

You need to add a 2nd method to your interface to represent the long click event. Then, within onBindViewHolder, add vh.itemView.setOnLongClickListener(...) in which you call that 2nd method.

Edit:

Your interface should now look like this:

public interface OnItemClickListener {
     void onItemClick(int item); 
     void onItemLongClick(int item);
}

CodePudding user response:

I solved it like this:

Adapter

public class StudioAdapter extends RecyclerView.Adapter<StudioAdapter.StudioViewHolder> {
    public interface OnItemClickListener {
        void onItemClick(int item);
        void onItemLongClick(int position);
    }
    private List<String> StudioLogoPaths;
    private OnItemClickListener listener;
    public StudioAdapter(List<String> StudioLogoPaths, OnItemClickListener listener) {
        this.StudioLogoPaths = (List<String>) StudioLogoPaths;
        this.listener = listener;
    }
    @Override
    public StudioAdapter.StudioViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.studio_logo, parent, false);
        StudioViewHolder vh = new StudioViewHolder(v);
        return vh;
    }
    @Override
    public void onBindViewHolder( StudioAdapter.StudioViewHolder vh, int position) {
        final String path = StudioLogoPaths.get(position);
        Glide.with(vh.itemView.getContext()).load(path).into(vh.logoImage);
        String basepath = "/data/user/0/org.courville.nova/app_scraper_studiologos/";
        String extension = ".png";
        final String clicked_studioname = path.replace(basepath, "").replace(extension, "");

        final int Position = position;
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(Position);
                Toast.makeText(vh.itemView.getContext(), clicked_studioname, Toast.LENGTH_SHORT).show();
            }
        });

        vh.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                listener.onItemLongClick(Position);
                return false;
            }
        });
    }
    @Override
    public int getItemCount() {
        return StudioLogoPaths.size();
    }
    public class StudioViewHolder extends RecyclerView.ViewHolder {
        protected ImageView logoImage;

        public StudioViewHolder(View itemView) {
            super(itemView);
            logoImage = itemView.findViewById(R.id.studio_logo);
        }
    }
}

Fragment

StudioAdapter.OnItemClickListener studioLogoCallback = new StudioAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(int item) {

                        }

                        @Override
                        public void onItemLongClick(int position) {
                            Glide.with(mContext).clear(mLogo);
                            Glide.with(mContext).load(tags.getStudioLogosLargeFileF().get(position))
                                    .fitCenter().into(mLogo);
                            ScraperImage clickedImage = (ScraperImage) scraperImage.get(position);
                            new VideoInfoActivityFragment.ClearLogoSaver(mContext, new VideoInfoActivityFragment()).execute(clickedImage);
                        }
                    };
  • Related