当我点击RecyclerView的任何项目时。它会告诉我这个项目的细节。我怎样才能做到这一点?我使用过Retrofit库。我为这个RecyclerView创建了一个适配器。我已经提到了它。
的活动:
List<TargetFile> targetfiles=response.body().getTargetfileslist();
Log.d(TAG,"Number of targetfileslist: "+targetfiles.size());
recyclerView.setAdapter(new TargetFileAdapter(targetfiles,R.layout.list_item_targetfile,getApplicationContext())); //RecyclerView
适配器:
public class TargetFileAdapter extends RecyclerView.Adapter<TargetFileAdapter.TargetFileViewHolder> {
private List<TargetFile> targetfiles;
private int rowLayout;
private Context context;
public TargetFileAdapter(List<TargetFile> targetfiles, int rowLayout, Context context) {
this.targetfiles = targetfiles;
this.rowLayout = rowLayout;
this.context = context;
}
public static class TargetFileViewHolder extends RecyclerView.ViewHolder{
LinearLayout targetFilesLayout;
TextView fileno,partyname,mobile,address;
public TargetFileViewHolder(View v){
super(v);
targetFilesLayout=(LinearLayout)v.findViewById(R.id.targetfile_layout);
fileno=(TextView)v.findViewById(R.id.fileno);
partyname=(TextView)v.findViewById(R.id.partyname);
address=(TextView)v.findViewById(R.id.adrs);
}
}
@Override
public TargetFileAdapter.TargetFileViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view= LayoutInflater.from(parent.getContext()).inflate(rowLayout,parent,false);
return new TargetFileViewHolder(view);
}
@Override
public void onBindViewHolder(TargetFileViewHolder holder,final int position){
holder.fileno.setText(targetfiles.get(position).getFileno());
holder.partyname.setText(targetfiles.get(position).getPartyname());
holder.address.setText(targetfiles.get(position).getAddress());
}
@Override
public int getItemCount() {
return targetfiles.size();
}
}
答案 0 :(得分:1)
iimport android.support.v7.widget.RecyclerView; import android.view.View;
public class TargetFileAdapter extends RecyclerView.Adapter<TargetFileAdapter.TargetFileViewHolder> {
private List<TargetFile> targetfiles;
private int rowLayout;
private Context context;
private OnTargetFileListener listener;
public TargetFileAdapter(List<TargetFile> targetfiles, int rowLayout, Context context, OnTargetFileListener listener) {
this.targetfiles = targetfiles;
this.rowLayout = rowLayout;
this.context = context;
this.listener = listener;
}
@Override
public TargetFileAdapter.TargetFileViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new TargetFileViewHolder(view);
}
@Override
public void onBindViewHolder(TargetFileViewHolder holder, final int position) {
holder.fileno.setText(targetfiles.get(position).getFileno());
holder.partyname.setText(targetfiles.get(position).getPartyname());
holder.address.setText(targetfiles.get(position).getAddress());
}
@Override
public int getItemCount() {
return targetfiles.size();
}
public static class TargetFileViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
LinearLayout targetFilesLayout;
TextView fileno, partyname, mobile, address;
public TargetFileViewHolder(View v) {
super(v);
targetFilesLayout = (LinearLayout) v.findViewById(R.id.targetfile_layout);
fileno = (TextView) v.findViewById(R.id.fileno);
partyname = (TextView) v.findViewById(R.id.partyname);
address = (TextView) v.findViewById(R.id.adrs);
v.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (listener == null){
return;
}
TargetFile file = targetfiles.get(getAdapterPosition());
listener.onFileTargetClicked(file);
}
}
public interface OnTargetFileListener {
void onTargetFileClicked(TargetFile file);
}
}
此代码应该适合您。您需要在活动中或使用适配器的位置实现OnTargetFileListener。
祝你好运!埃姆雷。