我正在更改我的ListView以使用RecylerView
,我想为每个项目添加内容说明。我需要补充一点,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);
}
}
}
答案 0 :(得分:0)
在ViewHolder中添加以下代码中的按钮: -
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewHead;
public TextView textViewDesc;
public Button button;
public LinearLayout linearLayout;
public ViewHolder(View itemView) {
super(itemView);
textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);
button = (Button) itemView.findViewById(R.id.button);
linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
}
}
在onBindViewHolder中访问此添加按钮,如下所示: -
holder.textViewHead.setText(listItem.getHead());
holder.textViewDesc.setText(listItem.getDesc());
holder.button.setOnClickListner(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "You clicked " + listItem.getHead(), Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
通常,视图必须是可聚焦的,才能被辅助功能工具解析。
因此,您必须使视图具有可聚焦性。此外,您必须使用View#setContentDescription(CharSequence)
提供内容说明。
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
ViewHolder holder = new ViewHolder(v);
holder.itemView.setFocusable(true);
return holder;
}
@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.textViewHead.setContentDescription(listItem.getHead());
holder.textViewDesc.setContentDescription(listItem.getDesc());
...
}