how to define class in kotlin with member can be accessed in java derived class
how to define class in kotlin with member can be accessed in java derived class
在 kotlin 中定义了一个 class,成员
@JvmField protected val clickHandler: BaseClickHandler
@JvmField protected var layoutId : Int
但是在 java 中尝试扩展此 class 并访问该成员时出现错误。
interface ViewDelegate {
fun onCreateViewHolder(parent: ViewGroup): RecyclerView.ViewHolder
fun onBindViewHolder(holder: RecyclerView.ViewHolder, item: Data, position: Int)
}
open class ViewDelegateImp(@JvmField protected val clickHandler: BaseClickHandler) : ViewDelegate {
@JvmField protected var layoutId = R.layout.default
override fun onCreateViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
return ItemViewHolder(parent.inflate(layoutId, false), clickHandler)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, item: Data, position: Int) {
(holder as? ItemViewHolder)?.bindView(item, position)
}
}
//extension:
fun ViewGroup.inflate(layoutId: Int, attachToRoot: Boolean = false): View {
return LayoutInflater.from(context).inflate(layoutId, this, attachToRoot)
}
在 java 中想要派生自 ViewDelegateImp,并覆盖 var layoutId 并使用受保护的 val clickHandler
class DerivedDelegateImp extends ViewDelegateImp {
public DerivedDelegateImp(BaseClickHandler clickHandler) {
super(clickHandler);
layoutId = R.layout.derived; // want to use different layout, but get error "cannot resolve layoutId"
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, Data item, int position) {
super.onBindViewHolder(holder, item, position);
}
@NotNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
View vw = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
return new StoryItemViewHolder(vw, clickHandler); // here got error clickHandler has private access in ViewDelegateImp
//return super.onCreateViewHolder(parent); //<== this is fine
}
}
这一定是 IDE 问题,无论其他什么(同步、清理、重建)都尝试过它只在删除项目并重新克隆以获取新的代码库并将这些代码添加回去后才有效。
在 kotlin 中定义了一个 class,成员
@JvmField protected val clickHandler: BaseClickHandler
@JvmField protected var layoutId : Int
但是在 java 中尝试扩展此 class 并访问该成员时出现错误。
interface ViewDelegate {
fun onCreateViewHolder(parent: ViewGroup): RecyclerView.ViewHolder
fun onBindViewHolder(holder: RecyclerView.ViewHolder, item: Data, position: Int)
}
open class ViewDelegateImp(@JvmField protected val clickHandler: BaseClickHandler) : ViewDelegate {
@JvmField protected var layoutId = R.layout.default
override fun onCreateViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
return ItemViewHolder(parent.inflate(layoutId, false), clickHandler)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, item: Data, position: Int) {
(holder as? ItemViewHolder)?.bindView(item, position)
}
}
//extension:
fun ViewGroup.inflate(layoutId: Int, attachToRoot: Boolean = false): View {
return LayoutInflater.from(context).inflate(layoutId, this, attachToRoot)
}
在 java 中想要派生自 ViewDelegateImp,并覆盖 var layoutId 并使用受保护的 val clickHandler
class DerivedDelegateImp extends ViewDelegateImp {
public DerivedDelegateImp(BaseClickHandler clickHandler) {
super(clickHandler);
layoutId = R.layout.derived; // want to use different layout, but get error "cannot resolve layoutId"
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, Data item, int position) {
super.onBindViewHolder(holder, item, position);
}
@NotNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
View vw = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
return new StoryItemViewHolder(vw, clickHandler); // here got error clickHandler has private access in ViewDelegateImp
//return super.onCreateViewHolder(parent); //<== this is fine
}
}
这一定是 IDE 问题,无论其他什么(同步、清理、重建)都尝试过它只在删除项目并重新克隆以获取新的代码库并将这些代码添加回去后才有效。