简体   繁体   English

长按选择回收站查看项目

[英]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.我有一个 recyclerview,当用户点击它时播放曲目。 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在您的onBindViewHolder方法中使用它并删除以下代码表单onCreateViewHolder并尝试

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

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

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