简体   繁体   中英

select recyclerview items with long click

I know there are tons of topics talking about this subject, but I didn't see they talk about it properly. I have a recyclerview which plays the tracks when user clicks on it. I want to implement a function that with long click, the selection operation replaces the playing operation and even checkboxes appear on the side of the items.

this implementation exists in almost every app and it seems easy to implement. but in other topics, they only talk about how to select and deselect items and the problem is, That part is easy. But how do you actually replace the existing operation when long clicking on an item with selection operation?

my adapter:

private Context context;
private ArrayList<MusicSample> collection;
private Uri playing;
private Uri chosen;
private OnMusicChange mListener;

public MusicAdapter(Context context, ArrayList<MusicSample> collection, OnMusicChange mListener){
    this.context = context;
    this.collection = collection;
    this.mListener = mListener;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.rv_song_item,parent,false);
    final ViewHolder viewHolder = new ViewHolder(view);
    viewHolder.relativeLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MusicSample currentMusic = collection.get(viewHolder.getAdapterPosition());
            chosen = currentMusic.getLocation();
            mListener.enable(true);
            MusicController.Controller(context,mListener,currentMusic,collection);
        }
    });
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
            holder.title.setText(collection.get(position).getTitle());
            holder.singer.setText(collection.get(position).getSinger());
            holder.coverArt.setImageBitmap(collection.get(position).getCoverArt());
}

@Override
public int getItemCount() {
    return collection.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{

    public ImageView coverArt;
    public TextView title;
    public TextView singer;
    public RelativeLayout relativeLayout;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        coverArt = itemView.findViewById(R.id.cover_art);
        title = itemView.findViewById(R.id.tv_title);
        singer = itemView.findViewById(R.id.tv_singer);
        relativeLayout = itemView.findViewById(R.id.rv_layout);
    }

}

You can use this

  holder.relativeLayout.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            //WRITE YOUR CODE HERE
            Toast.makeText(MainActivity.this, "LONG CLICK", Toast.LENGTH_SHORT).show();
            return true;
        }
    });  

Use this in your onBindViewHolder method and remove below code form onCreateViewHolder and try

   viewHolder.relativeLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        MusicSample currentMusic = collection.get(viewHolder.getAdapterPosition());
        chosen = currentMusic.getLocation();
        mListener.enable(true);
        MusicController.Controller(context,mListener,currentMusic,collection);
    }
});

you can add the code to your adapter :

 holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        Toast.makeText(MainActivity.this, "click item", Toast.LENGTH_SHORT).show();
        return true;
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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