我试图实现的基本目标是,使在回收视图中显示的对象可点击到特定的TextView ID,因为我正在制作一个程序,它会在列表中显示专辑封面及其标题。我需要能够单击回收视图创建的每个方框,并在单击时弹出一个TextView以显示其他信息(作者、发布日期、流行曲目等),然后有一个返回按钮(如果可能的话)返回到专辑列表。我已经看了几个小时,无法弄清楚如何使OnclickListener起作用。如果您知道如何做或有任何建议,我很乐意听取。谢谢!
package com.albumlist.albumlist;
import android.content.Context;
import android.content.Intent;
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 android.widget.Toast;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private AlbumData[] itemsData;
public MyAdapter(AlbumData[] itemsData){
this.itemsData = itemsData;
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView txtViewTitle;
private ImageView imgViewIcon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
itemLayoutView.setOnClickListener(this);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.album_title);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.album_icon);
}
@Override
public void onClick(View v) {
}
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.data_layout, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
}
@Override
public int getItemCount() {
return itemsData.length;
}
}