有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java类DrawerItem需要一个类型参数

我有一个JAVA类:

public abstract class DrawerItem<T extends DrawerAdapter.ViewHolder> {

    protected boolean isChecked;
    public abstract T createViewHolder(ViewGroup parent);
    public abstract void bindViewHolder(T holder);

    public DrawerItem<T>setChecked(boolean isChecked) {
        this.isChecked = isChecked;
        return this;
    }

    public boolean isChecked() {
        return isChecked();
    }

    public boolean isSelectable(){
        return true;
    }
}

KOTLIN文件中,我这样称呼它:

private fun createItemFor(position :Int): DrawerItem {
    return SimpleItem(screenIcons[position], screenTitles[position])
        .withIconTint(R.color.bottom_menu_unselected)
        .withTextTint(R.color.black)
        .withSelectedIconTint(R.color.bottom_menu_unselected)
        .withSelectedTextTint(R.color.bottom_menu_unselected)
}

我得到一个错误:One type argument expected for class DrawerItem<T : DrawerAdapter.ViewHolder!>

我错过了什么?我不确定我应该具体说明什么才能使它起作用


共 (1) 个答案

  1. # 1 楼答案

    DroperItem有一个类型,Kotlin不允许原始类型。在大多数可以在Java中使用原始类型的地方,可以在Kotlin中使用星形投影,如下所示:

    private fun createItemFor(position :Int): DrawerItem<*> { //...
    

    如果需要以某种方式使用它,即需要知道SimpleItem.createViewHolder()返回的ViewHolder的类型,则需要专门指定该类型(SimpleItem使用的类型是什么),或者可以使用=来隐式指定返回类型,这样就不必考虑它:

    private fun createItemFor(position :Int) =
        SimpleItem(screenIcons[position], screenTitles[position])
            .withIconTint(R.color.bottom_menu_unselected)
            .withTextTint(R.color.black)
            .withSelectedIconTint(R.color.bottom_menu_unselected)
            .withSelectedTextTint(R.color.bottom_menu_unselected)