Create views programmatically in a RecyclerView.Vi

2019-07-24 23:34发布

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条回答
闹够了就滚
2楼-- · 2019-07-24 23:56

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);
查看更多
登录 后发表回答