0

在 kotlin 中定义了一个类,其中包含成员

@JvmField protected val clickHandler: BaseClickHandler
@JvmField protected var layoutId : Int

但是当在java中试图扩展这个类并访问它的成员时会出错。

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
    }
}
4

1 回答 1

0

一定是 IDE 问题,无论其他什么(同步、清理、重建)尝试,它都只能在删除项目并重新克隆以获取新的代码库并重新添加这些代码后才有效。

于 2018-08-22T12:12:48.830 回答