本文介绍了在RecyclerView.ViewHolder中以编程方式创建视图并将参数传递给它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在ViewHolder中以编程方式创建视图,而不是像在所有示例中那样以经典方式从XML绑定视图吗?另外,我的视图需要一个图像文件路径才能创建,如何将其传递给ViewHolder

Can I create views programmatically in a ViewHolder instead of binding them from XML in the classic way as in all examples?Also, my views need an image filepath in order to be created, how do I pass that to the ViewHolder

protected static class ImagePreviewViewHolder extends RecyclerView.ViewHolder {
    public ImageView imageView;
    public LinearLayout page;

    public ImagePreviewViewHolder(View itemView) {
        super(itemView);
        page = createPage(filePath); // How do I pass the filepath?
    }
}

推荐答案

您是否尝试创建 查看itemView通过代码?

Did u try create View itemViewvia code?

例如:

ll = new LinearLayout(this);
ll.setOrientation(android.widget.LinearLayout.VERTICAL);
ll.setLayoutParams(new ViewGroup.LayoutParams(-1,-1));
ll.setBackgroundColor(0x88ff0000);

tv = new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(-1,-2));
tv.setText("sample text goes here");
tv.setBackgroundColor(0x5500ff00);
ll.addView(tv);

ImagePreviewViewHolder holder = new ImagePreviewViewHolder(ll);

这篇关于在RecyclerView.ViewHolder中以编程方式创建视图并将参数传递给它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 12:18