构建应用程序时RecyclerView适配器出错

时间:2016-06-28 19:16:45

标签: android android-recyclerview recycler-adapter

我添加了RecyclerView及其适配器,但是当我构建应用程序时,它显示我在Android Studio的消息视图中构建错误。它指出了我创建的RecycleAdapter中的错误。

Error:(63, 12) error: cannot access ScrollingView
class file for android.support.v4.view.ScrollingView not found

Error:(20, 8) error: cannot access NestedScrollingChild
class file for android.support.v4.view.NestedScrollingChild not found

Error:(20, 88) error: type argument CategoryRecycleAdapter.ViewHolder is not within bounds of type-variable VH

where VH is a type-variable:
VH extends android.support.v7.widget.RecyclerView.ViewHolder declared in class Adapter
Error:(30, 9) error: cannot find symbol variable this

Error:(31, 9) error: cannot find symbol variable this

Error:(34, 5) error: method does not override or implement a method from a supertype

Error:(37, 16) error: an enclosing instance that contains CategoryRecycleAdapter.ViewHolder is required

Error:(40, 5) error: method does not override or implement a method from a supertype
Error:(55, 9) error: cannot find symbol method notifyDataSetChanged()

Error:(58, 5) error: method does not override or implement a method from a supertype

以下是适配器代码:

public class CategoryRecycleAdapter extends RecyclerView.Adapter<CategoryRecycleAdapter.ViewHolder> {

Context mContext;
ArrayList<Category> alData;


public CategoryRecycleAdapter() {
}

public CategoryRecycleAdapter(Context mContext, ArrayList<Category> alData) {
    this.mContext = mContext;
    this.alData = alData;
}

@Override
public CategoryRecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.row_category, parent, false);
    return new CategoryRecycleAdapter.ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    holder.tvName.setText("" + alData.get(position).displayName);
    Picasso.with(mContext)
            .load(alData.get(position).iconUrl)
            .into(holder.ivCategory);

}

public void addAll(ArrayList<Category> data) {
    if (data != null) {
        alData.clear();
        alData.addAll(data);
    }
    notifyDataSetChanged();
}

@Override
public int getItemCount() {
    return alData.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView tvName;
    ImageView ivCategory;

    public ViewHolder(View itemView) {
        super(itemView);
        tvName = (TextView) itemView.findViewById(R.id.tvCategory);
        ivCategory = (ImageView) itemView.findViewById(R.id.ivCategory);
    }
}}

2 个答案:

答案 0 :(得分:0)

我相信这条线是你的罪魁祸首:

public class CategoryRecycleAdapter extends RecyclerView.Adapter<CategoryRecycleAdapter.ViewHolder>

您正在引用类CategoryRecycleAdapter的基础ViewHolder。尝试扩展您声明为私有内部类的ViewHolder。

public class ViewHolder extends RecyclerView.ViewHolder

进行此修订并让我知道它是否无效:

public class CategoryRecycleAdapter extends RecyclerView.Adapter<ViewHolder>

答案 1 :(得分:0)

对您的代码进行以下更改:

public class CategoryRecycleAdapter extends RecyclerView.Adapter< RecyclerView.ViewHolder>

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) 

onCreateViewHolder 的返回类型从CategoryRecycleAdapter.ViewHolder更改为RecyclerView.ViewHolder

这应该可以解决问题。