Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问,可以用我自己写的ViewHolder么? #82

Open
wuzhuohan7788 opened this issue Feb 24, 2022 · 4 comments
Open

请问,可以用我自己写的ViewHolder么? #82

wuzhuohan7788 opened this issue Feb 24, 2022 · 4 comments

Comments

@wuzhuohan7788
Copy link

我在用你的adapter时发现,只能用你写好的BaseViewHolder,但是我需要用自己的ViewHolder,请问有没有什么办法做到?

@donkingliang
Copy link
Owner

因为GroupedRecyclerViewAdapter跟BaseViewHolder是强依赖关系,所以你必须使用BaseViewHolder。如果你想要自定义ViewHolder,可以让你的ViewHolder继承BaseViewHolder。

@wangrui-spiderNet
Copy link

继承之后,通过哪个方法绑定到Adapter呢?
@OverRide
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 1) {
return new ChildViewHolder(parent);
} else {
return new BasicViewHolder(parent);
}
}
这个吗?

@wuzhuohan7788
Copy link
Author

我这边解决了,我重写了onCreateViewHolder方法
@OverRide
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_HEADER) {
return new HeadViewHolder(LayoutInflater.from(context).inflate(getHeaderLayout(viewType), parent, false));
} else if (viewType == TYPE_CHILD) {
return new ChildViewHolder(LayoutInflater.from(context).inflate(getChildLayout(viewType), parent, false));
}
return super.onCreateViewHolder(parent, viewType);
}
这样在 onBindViewHolder里,传进来的就是我自定义的viewHolder

@wangrui-spiderNet
Copy link

好的,谢谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants