问题
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