如何点击其中的项目时获取recyclerView Id?

时间:2018-06-08 13:57:34

标签: android view android-recyclerview id

基本上我有4个回收商,每个都有不同的ID,我不想过多地了解我希望这种情况发生的原因,所以我需要的是当点击回收器的任何项目时我可以获得RECYCLER的ID,而不是点击的项目。 这可能吗?我想这可以通过适配器完成,但我不知道如何。 这是我到目前为止所得到的,但itemView是单元格的视图,而不是回收器。

编辑:我的问题以另一种方式解决了(所以不要深入研究),我只是好奇是否可以点击回收者的ID,而不是点击该项目。< / p>
public class AlbumAdapter extends RecyclerView.Adapter {

    private List<Album> albumLista;
    private NotificadorAlbumCelda notificadorAlbumCelda;

    public AlbumAdapter(List<Album> albumLista, NotificadorAlbumCelda notificadorAlbumCelda) {
        this.albumLista = albumLista;
        this.notificadorAlbumCelda = notificadorAlbumCelda;
    }

    public void setAlbumLista(List<Album> albumLista) {
        this.albumLista = albumLista;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.celda_recycler_scroll, parent, false);
        ViewHolderAlbum viewHolderAlbum = new ViewHolderAlbum(view);
        return viewHolderAlbum;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Album album = albumLista.get(position);
        ViewHolderAlbum viewHolderAlbum = (ViewHolderAlbum) holder;
        viewHolderAlbum.cargarAlbum(album);
    }

    @Override
    public int getItemCount() {
        if (albumLista != null) {
            return albumLista.size();
        } else {
            return 0;
        }
    }

    public class ViewHolderAlbum extends RecyclerView.ViewHolder {

        private TextView textViewNombreAlbum;
        private TextView textViewNombreArtista;
        private ImageView imagenAlbum;

        public ViewHolderAlbum(final View itemView) {
            super(itemView);
            textViewNombreAlbum = itemView.findViewById(R.id.titulo_fragment_album);
            textViewNombreArtista = itemView.findViewById(R.id.titulo_fragment_artista);
            imagenAlbum = itemView.findViewById(R.id.imagen_fragment_album);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //here is where I need to get the id
                    int idRecycler = itemView.getId();
                    int posicionAlbumClickeado = getAdapterPosition();
                    Album album = albumLista.get(posicionAlbumClickeado);
                    notificadorAlbumCelda.notificarCeldaClickeada(album, posicionAlbumClickeado, idRecycler);
                }
            });
        }

        public void cargarAlbum(Album album) {
            textViewNombreAlbum.setText(album.getTitulo());
            textViewNombreArtista.setText(album.getArtista().getNombre());
            imagenAlbum.setImageResource(album.getImagenAlbum());
        }
    }

    public interface NotificadorAlbumCelda {
        public void notificarCeldaClickeada(Album album, int posicion, int idRecycler);
    }
}

1 个答案:

答案 0 :(得分:0)

为什么需要身份证?可能你想知道,点击什么回收查看项,如果是,你可以用四种方法创建界面,将这个界面设置为你所有的回收,在RecyclerView项目中点击只需调用相应的界面方法。

public interface RecycleViewClickListener {

    void firstRecycleClick();
    void secondRecycleClick();
    void thirdRecycleClick();
    void fourthRecycleClick();

}

对于每个回收视图,只需处理点击

private RecycleViewClickListener recycleViewClickListener;

public void setRecycleViewClickListener(RecycleViewClickListener recycleViewClickListener) {
        this.recycleViewClickListener= recycleViewClickListener;
    }

 itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //here is where I need to get the id
                    recycleViewClickListener.firstRecycleClick();
                }
            });