Create views programmatically in a RecyclerView.ViewHolder and passing arguments to it

落花浮王杯 提交于 2019-12-13 02:25:57

问题


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?
    }
}

回答1:


Did u try create View itemView via code?

for ex.:

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);

and

ImagePreviewViewHolder holder = new ImagePreviewViewHolder(ll);


来源:https://stackoverflow.com/questions/37652332/create-views-programmatically-in-a-recyclerview-viewholder-and-passing-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!