我正在更改我的 ListView 以使用 aRecylerView
并且我想为每个项目添加内容描述。我需要补充一点,listItem 具有按钮的作用,所以它会显示为“head”+“desc”+“button”。我想我知道如何做那部分,但经过几次尝试后无法弄清楚到底该把它放在哪里。我已经阅读了一些关于此的问题,但对于我将其放在哪里有点困惑。任何帮助,将不胜感激。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<ListItem> listItems;
private Context context;
// need to initialize variables above
public MyAdapter(List<ListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final ListItem listItem = listItems.get(position);
holder.textViewHead.setText(listItem.getHead());
holder.textViewDesc.setText(listItem.getDesc());
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "You clicked " + listItem.getHead(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewHead;
public TextView textViewDesc;
public ImageView imageview;
public LinearLayout linearLayout;
public ViewHolder(View itemView) {
super(itemView);
textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);
//imageView = (ImageView) itemView.findViewById(R.id.imageView);
linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
}
}
}