gpt4 book ai didi

java - 如何从自定义 View java类中膨胀布局?不是来自 XML

转载 作者:行者123 更新时间:2023-12-01 23:12:47 25 4
gpt4 key购买 nike

我正在尝试以编程方式设置 Recyclerview 请帮忙。

public RecyclerViewViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item /- how to do it here from custom view class?/, parent, false);
RecyclerViewViewHolder recyclerViewViewHolder = new RecyclerViewViewHolder(view);
return recyclerViewViewHolder;
}

最佳答案

调用自定义 View 的构造函数:

public RecyclerViewViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = new MyCustomView(parent.getContext());
RecyclerViewViewHolder recyclerViewViewHolder = new RecyclerViewViewHolder(view);
return recyclerViewViewHolder;
}

关于java - 如何从自定义 View java类中膨胀布局?不是来自 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58366084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com