1

我正在更改我的 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);
        }
    }
}
4

2 回答 2

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();
        }
    });
于 2017-09-19T13:16:50.547 回答
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());

        ...
    }

于 2017-09-19T13:28:37.903 回答